made serialization have an extra param for display

This commit is contained in:
Izaya 2019-12-16 14:35:11 +11:00
parent b370a90618
commit 560cd4e0c7

View File

@ -3,7 +3,7 @@ local local_pairs=function(tbl)
local mt=getmetatable(tbl)
return (mt and mt.__pairs or pairs)(tbl)
end
function serial.serialize(value)
function serial.serialize(value,af) -- serialize *value* into a string. If *af* is true, allow functions. This breaks unserialization.
local kw={["and"]=true,["break"]=true,["do"]=true,["else"]=true,["elseif"]=true,["end"]=true,["false"]=true,["for"]=true,["function"]=true,["goto"]=true,["if"]=true,["in"]=true,["local"]=true,["nil"]=true,["not"]=true,["or"]=true,["repeat"]=true,["return"]=true,["then"]=true,["true"]=true,["until"]=true,["while"]=true}
local id="^[%a_][%w_]*$"
local ts={}
@ -35,10 +35,12 @@ function serial.serialize(value)
r=r.."="..s(v,l+1) end end
ts[v]=nil
return (r or "{").."}"
elseif t=="function" and af then
return tostring(v)
else error("ut "..t) end end
return s(value, 1)
end
function serial.unserialize(data)
function serial.unserialize(data) -- return *data*, but unserialized
checkArg(1, data, "string")
local result, reason = load("return " .. data, "=data", _, {math={huge=math.huge}})
if not result then return nil, reason end