Module:O: Difference between revisions

From Wavu Wiki, the 🌊 wavy Tekken wiki
(Created page with "--[[ 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 w...")
 
No edit summary
 
Line 9: Line 9:


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



Latest revision as of 08:44, 5 September 2023

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