Module:SMW JSON

From the Tesseract Wiki, the wiki for all things Marvel Cinematic Universe
Jump to navigation Jump to search

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

local p = {}

function p.main(frame)
	local field = frame.args.field
	if field ~= nil then
		local args = {}
		for k, v in pairs(frame:getParent().args) do
			if v == '' then
				args[k] = nil
			else
				args[k] = mw.text.unstrip(v)
			end
		end
		mw.logObject(args)
		local json = mw.text.jsonEncode(args)
		mw.smw.set({[field] = json})
	end
end

function p.parse(data, field)
	if data == nil then
		return {}
	end
	
	local out = {}
	for _,task in ipairs(data) do
		local raw = task[field]
		if type(raw) == "string" then
			local json = mw.text.jsonDecode(raw)
			json['_page'] = task[1]
			table.insert(out, json)
		elseif type(raw) == "table" then
			for _,v in ipairs(raw) do
				local json = mw.text.jsonDecode(v)
				json['_page'] = task[1]
				table.insert(out, json)
			end
		end
	end
	return out
end

return p