Added support for NaN, inf and -inf to lume.serialize; updated tests

This commit is contained in:
rxi 2015-09-23 19:32:25 +01:00
parent 1559803c70
commit 8627638db0
2 changed files with 10 additions and 1 deletions

View File

@ -539,10 +539,15 @@ end
local serialize local serialize
local serialize_map = { local serialize_map = {
[ "number" ] = tostring,
[ "boolean" ] = tostring, [ "boolean" ] = tostring,
[ "nil" ] = tostring, [ "nil" ] = tostring,
[ "string" ] = function(v) return string.format("%q", v) end, [ "string" ] = function(v) return string.format("%q", v) end,
[ "number" ] = function(v)
if v ~= v then return "0/0" -- nan
elseif v == 1 / 0 then return "1/0" -- inf
elseif v == -1 / 0 then return "-1/0" end -- -inf
return tostring(v)
end,
[ "table" ] = function(t, stk) [ "table" ] = function(t, stk)
stk = stk or {} stk = stk or {}
if stk[t] then error("circular reference") end if stk[t] then error("circular reference") end

View File

@ -484,6 +484,10 @@ tests["lume.serialize, lume.deserialize"] = function()
local t = { 1, 2, 3, 4, true, false, "cat", "dog", {1, 2, 3} } local t = { 1, 2, 3, 4, true, false, "cat", "dog", {1, 2, 3} }
local s = lume.serialize(t) local s = lume.serialize(t)
testeq( lume.deserialize(s), t ) testeq( lume.deserialize(s), t )
testeq( lume.deserialize(lume.serialize(math.huge)), math.huge )
testeq( lume.deserialize(lume.serialize(-math.huge)), -math.huge )
local x = lume.deserialize(lume.serialize(0 / 0)) -- nan
testeq( x ~= x, true )
end end
-- lume.split -- lume.split