Module:Json: Difference between revisions

From TwogPedia
No edit summary
Tag: Reverted
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 4: Line 4:


function Json.fromArgs(frame)
function Json.fromArgs(frame)
local args = Arguments.getArgs(frame)
local args = Arguments.getArgs(frame)
mw.log(args)
return Json.stringify(args)
return Json.stringify(args)
end
end
Line 13: Line 15:


function Json.parse(obj)
function Json.parse(obj)
local parse = function(object) return mw.text.jsonDecode(object, mw.text.JSON_TRY_FIXING) end
local parse = function(object) return mw.text.jsonDecode(object, mw.text.JSON_TRY_FIXING) end
local status, res = pcall(parse, obj);
local status, res = pcall(parse, obj);
Line 19: Line 22:
else
else
mw.log('Error: could not parse Json:')
mw.log('Error: could not parse Json:')
error(obj)
mw.logObject(obj)
mw.logObject(obj)
mw.log(debug.traceback())
mw.log(debug.traceback())

Latest revision as of 15:09, 22 August 2022

Documentation for this module may be created at Module:Json/doc

local Json = {}

local Arguments = require('Module:Arguments')

function Json.fromArgs(frame)

	local args = Arguments.getArgs(frame)
	mw.log(args)
	return Json.stringify(args)
end

function Json.stringify(obj, pretty)
	return mw.text.jsonEncode(obj, pretty == true and mw.text.JSON_PRETTY or nil)
end

function Json.parse(obj)
	
	local parse = function(object) return mw.text.jsonDecode(object, mw.text.JSON_TRY_FIXING) end
	local status, res = pcall(parse, obj);
	if status then
		return res, false
	else
		mw.log('Error: could not parse Json:')
		mw.logObject(obj)
		mw.log(debug.traceback())
		return {}, true
	end
end

function Json.parseIfString(obj)
	if type(obj) == 'string' then
		return Json.parse(obj)
	else
		return obj
	end
end

--[[
Attempts to parse a JSON encoded table. Returns nil if unsuccessful.

Example:

JsonExt.parseIfTable('{"a" = 3}')
-- Returns {a = 3}

]]
function Json.parseIfTable(any)
	if type(any) == 'string' then
		local firstChar = any:sub(1, 1)
		if firstChar == '{' or firstChar == '[' then
			local result, hasError = Json.parse(any)
			if not hasError then
				return result
			end
		end
	end
	return nil
end

return Json