mirror of
https://github.com/TangentFoxy/lume.git
synced 2024-11-19 07:04:24 +00:00
Added support for NaN, inf and -inf to lume.serialize; updated tests
This commit is contained in:
parent
1559803c70
commit
8627638db0
7
lume.lua
7
lume.lua
@ -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
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user