Module:SMW JSON

Revision as of 13:52, 29 November 2024 by BlackHawk (talk | contribs) (Created page with "local p = {} function p.main(frame) local args = frame.args local field = args.field local subobject = args.subobject local parent_args = frame:getParent().args p._main(field, parent_args, subobject) end function p._main(field, parent_args, subobject) if field ~= nil then local args = {} for k, v in pairs(parent_args) do if v == '' then args[k] = nil else args[k] = mw.text.unstrip(v) end end local data = { [field] = mw.text.jsonEncode(...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Module documentation
This documentation is transcluded from Module:SMW JSON/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:SMW JSON/doc. [edit]
local p = {}

function p.main(frame)
	local args = frame.args
	local field = args.field
	local subobject = args.subobject
	local parent_args = frame:getParent().args
	p._main(field, parent_args, subobject)
end

function p._main(field, parent_args, subobject)
	if field ~= nil then
		local args = {}
		for k, v in pairs(parent_args) do
			if v == '' then
				args[k] = nil
			else
				args[k] = mw.text.unstrip(v)
			end
		end
		
		local data = { [field] = mw.text.jsonEncode(args) }
		if subobject and subobject ~= "" then
			mw.smw.subobject(data, subobject)
		else
			mw.smw.set(data)
		end
	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(mw.text.decode(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(mw.text.decode(v))
				json['_page'] = task[1]
				table.insert(out, json)
			end
		end
	end
	return out
end

return p