Module:SMW JSON

This is the current revision of this page, as edited by Alex (talk | contribs) at 00:13, 17 October 2024 (Created page with "local p = {} function p.main(frame) local field = frame.args.field local parent_args = frame:getParent().args p._main(field, parent_args) end function p._main(field, parent_args) 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 mw.logObject(args) local json = mw.text.jsonEncode(args) mw.smw.set({[field] = json}) end end function p.parse(data,..."). The present address (URL) is a permanent link to this version.

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local p = {}

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

function p._main(field, parent_args)
	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
		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(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