Module:O

From Wavu Wiki, the 🌊 wavy Tekken wiki
Revision as of 08:44, 5 September 2023 by RogerDodger (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

--[[
	Encode/Decode Lua objects to/from JSON hex.
	
	Intended to use on template args to pass nested data structures in wikitext.
	'O' meaning Object, so we can think of our wikitext args like we're writing JSON.
]]--

local p = {}

p.encode = function(frame)
	return p._encode(frame:getParent().args)
end

p._encode = function(args)
	local json = mw.text.jsonEncode(args)
	local jsonhex = json:gsub(".", function(char)
		return string.format("%02x", char:byte())
	end)
	return jsonhex
end

p.decode = function(jsonhex)
	local json = jsonhex:gsub("%x%x", function(digits)
		return string.char(tonumber(digits, 16))
	end)
	return mw.text.jsonDecode(json)
end

return p