Skip to content

Commit

Permalink
Adjust Json.stringify (#3373)
Browse files Browse the repository at this point in the history
* adjust json.stringify

* adjust testcases
- add some additional ones for parsing
- adjust the stringify ones according to changes in json module

* adjust standings storage to use array for matches

* handle match2game.scores to be array

* add annos for the lpdb json functions

* Update definitions/mw.lua

Co-authored-by: Rikard Blixt <[email protected]>

* Update definitions/mw.lua

Co-authored-by: Rikard Blixt <[email protected]>

* catch non tables & adjust consumers

* adjust consumers & workaround for 2 non git modules

* simplify and add tests

* Update standard/json.lua

Co-authored-by: mbergen <[email protected]>

---------

Co-authored-by: Rikard Blixt <[email protected]>
Co-authored-by: mbergen <[email protected]>
  • Loading branch information
3 people authored Nov 6, 2023
1 parent 5419d0a commit 55b00f1
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 25 deletions.
22 changes: 9 additions & 13 deletions components/match2/commons/match.lua
Original file line number Diff line number Diff line change
Expand Up @@ -243,26 +243,22 @@ function Match.copyRecords(matchRecord)
})
end

local function stringifyIfTable(tbl)
return type(tbl) == 'table' and Json.stringify(tbl) or nil
end

function Match.encodeJson(matchRecord)
matchRecord.match2bracketdata = stringifyIfTable(matchRecord.match2bracketdata)
matchRecord.stream = stringifyIfTable(matchRecord.stream)
matchRecord.links = stringifyIfTable(matchRecord.links)
matchRecord.extradata = stringifyIfTable(matchRecord.extradata)
matchRecord.match2bracketdata = Json.stringify(matchRecord.match2bracketdata)
matchRecord.stream = Json.stringify(matchRecord.stream)
matchRecord.links = Json.stringify(matchRecord.links)
matchRecord.extradata = Json.stringify(matchRecord.extradata)

for _, opponentRecord in ipairs(matchRecord.match2opponents) do
opponentRecord.extradata = stringifyIfTable(opponentRecord.extradata)
opponentRecord.extradata = Json.stringify(opponentRecord.extradata)
for _, playerRecord in ipairs(opponentRecord.match2players) do
playerRecord.extradata = stringifyIfTable(playerRecord.extradata)
playerRecord.extradata = Json.stringify(playerRecord.extradata)
end
end
for _, gameRecord in ipairs(matchRecord.match2games) do
gameRecord.extradata = stringifyIfTable(gameRecord.extradata)
gameRecord.participants = stringifyIfTable(gameRecord.participants)
gameRecord.scores = stringifyIfTable(gameRecord.scores)
gameRecord.extradata = Json.stringify(gameRecord.extradata)
gameRecord.participants = Json.stringify(gameRecord.participants)
gameRecord.scores = Json.stringify(gameRecord.scores, {asArray = true})
end
end

Expand Down
2 changes: 1 addition & 1 deletion components/match2/commons/match_group_legacy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function Legacy.getTemplate(frame)

local mapping = Legacy._getMapping(templateid)

local out = json.stringify(mapping, true)
local out = json.stringify(mapping, {pretty = true})
:gsub('"([^\n:"]-)":', '%1 = ')
:gsub('type =', '["type"] =')
:gsub(' = %[(.-)%]', ' = { %1 }')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function Legacy.getTemplate(frame)

local mapping = Legacy._getMapping(templateid)

local out = json.stringify(mapping, true)
local out = json.stringify(mapping, {pretty = true})
:gsub('"([^\n:"]-)":', '%1 = ')
:gsub('type =', '["type"] =')
:gsub(' = %[(.-)%]', ' = { %1 }')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function Legacy.getTemplate(frame)

local mapping = Legacy._getMapping(templateid)

local out = json.stringify(mapping, true)
local out = json.stringify(mapping, {pretty = true})
:gsub('"([^\n:"]-)":', '%1 = ')
:gsub('type =', '["type"] =')
:gsub(' = %[(.-)%]', ' = { %1 }')
Expand Down
2 changes: 1 addition & 1 deletion components/standings/commons/standings_storage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function StandingsStorage.table(data)
title = mw.text.trim(cleanedTitle),
section = Variables.varDefault('last_heading', ''):gsub('<.->', ''),
type = data.type,
matches = Json.stringify(data.matches or {}),
matches = Json.stringify(data.matches or {}, {asArray = true}),
config = mw.ext.LiquipediaDB.lpdb_create_json(config),
extradata = mw.ext.LiquipediaDB.lpdb_create_json(Table.merge(extradata, data.extradata)),
}
Expand Down
13 changes: 13 additions & 0 deletions definitions/mw.lua
Original file line number Diff line number Diff line change
Expand Up @@ -652,4 +652,17 @@ function mw.title:canonicalUrl(query) end
---@return string?
function mw.title:getContent() end

mw.ext = {}
mw.ext.LiquipediaDB = {}

---@param obj table
---@return string
---Encode a table to a JSON object. Errors are raised if the passed value cannot be encoded in JSON.
function mw.ext.LiquipediaDB.lpdb_create_json(obj) end

---@param obj any[]
---@return string
---Encode an Array to a JSON array. Errors are raised if the passed value cannot be encoded in JSON.
function mw.ext.LiquipediaDB.lpdb_create_array(obj) end

return mw
23 changes: 17 additions & 6 deletions standard/json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,22 @@ function Json.fromArgs(frame)
end

---Json-stringifies a given table.
---@param obj table
---@param pretty boolean?
---@return string
function Json.stringify(obj, pretty)
return mw.text.jsonEncode(obj, pretty == true and mw.text.JSON_PRETTY or nil)
---@param obj table|string
---@param options {pretty: boolean?, asArray: boolean?}
---@return string?
---@overload fun(any: any): any
function Json.stringify(obj, options)
if type(obj) ~= 'table' then
return obj
end

options = options or {}

if options.pretty or options.asArray then
return mw.text.jsonEncode(obj, options.pretty and mw.text.JSON_PRETTY or nil)
end

return mw.ext.LiquipediaDB.lpdb_create_json(obj)
end

---Json-stringifies subtables of a given table.
Expand All @@ -34,7 +45,7 @@ function Json.stringifySubTables(obj, pretty)
local objectWithStringifiedSubtables = {}
for key, item in pairs(obj) do
if type(item) == 'table' then
objectWithStringifiedSubtables[key] = Json.stringify(item, pretty)
objectWithStringifiedSubtables[key] = Json.stringify(item, {pretty = pretty})
else
objectWithStringifiedSubtables[key] = item
end
Expand Down
14 changes: 12 additions & 2 deletions standard/test/json_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,48 @@ local suite = ScribuntoUnit:new()
function suite:testStringify()
self:assertEquals('[]', Json.stringify{})
self:assertEquals('{"abc":"def"}', Json.stringify{abc = 'def'})
self:assertEquals('{"abc":["b","c"]}', Json.stringify{abc = {'b', 'c'}})
self:assertEquals('{"abc":{"1":"b","2":"c"}}', Json.stringify{abc = {'b', 'c'}})
self:assertTrue(string.len(Json.stringify(mw.loadData('Module:Flags/MasterData'))) > 3)
self:assertEquals(nil, Json.stringify())
self:assertEquals('string', Json.stringify('string'))
self:assertEquals('[1,2,3]', Json.stringify({1, 2, 3}, {asArray = true}))
self:assertEquals('{"1":1,"2":2,"3":3}', Json.stringify{1, 2, 3})
end

function suite:testParse()
self:assertDeepEquals({}, (Json.parse('[]')))
self:assertDeepEquals({}, (Json.parse('{}')))
self:assertDeepEquals({abc = 'def'}, (Json.parse('{"abc":"def"}')))
self:assertDeepEquals({abc = {'b', 'c'}}, (Json.parse('{"abc":["b","c"]}')))
self:assertDeepEquals({abc = {'b', 'c'}}, (Json.parse('{"abc":{"1":"b","2":"c"}}')))
self:assertDeepEquals({}, (Json.parse{a = 1}))
self:assertDeepEquals({}, (Json.parse('banana')))
end

function suite:testParseIfString()
self:assertDeepEquals({}, (Json.parseIfString('[]')))
self:assertDeepEquals({}, (Json.parseIfString('{}')))
self:assertDeepEquals({abc = 'def'}, (Json.parseIfString('{"abc":"def"}')))
self:assertDeepEquals({abc = {'b', 'c'}}, (Json.parseIfString('{"abc":["b","c"]}')))
self:assertDeepEquals({abc = {'b', 'c'}}, (Json.parseIfString('{"abc":{"1":"b","2":"c"}}')))
self:assertDeepEquals({a = 1}, (Json.parseIfString{a = 1}))
self:assertDeepEquals({}, (Json.parseIfString('banana')))
end

function suite:testParseIfTable()
self:assertDeepEquals({}, (Json.parseIfTable('[]')))
self:assertDeepEquals({}, (Json.parseIfTable('{}')))
self:assertDeepEquals({abc = 'def'}, (Json.parseIfTable('{"abc":"def"}')))
self:assertDeepEquals({abc = {'b', 'c'}}, (Json.parseIfTable('{"abc":["b","c"]}')))
self:assertDeepEquals({abc = {'b', 'c'}}, (Json.parseIfTable('{"abc":{"1":"b","2":"c"}}')))
self:assertDeepEquals(nil, (Json.parseIfTable{a = 1}))
self:assertDeepEquals(nil, (Json.parseIfTable('banana')))
end

function suite:testStringifySubTables()
self:assertDeepEquals({}, Json.stringifySubTables{})
self:assertDeepEquals({abc = 'def'}, Json.stringifySubTables{abc = 'def'})
self:assertDeepEquals({abc = '["b","c"]'}, Json.stringifySubTables{abc = {'b', 'c'}})
self:assertDeepEquals({abc = '{"1":"b","2":"c"}'}, Json.stringifySubTables{abc = {'b', 'c'}})
self:assertDeepEquals({a = '{"d":1,"b":"c"}', e = 'f'}, Json.stringifySubTables{a = {b = 'c', d = 1}, e = 'f'})
end

Expand Down

0 comments on commit 55b00f1

Please sign in to comment.