diff --git a/lume.lua b/lume.lua index 5cf7fc3..cddeae9 100644 --- a/lume.lua +++ b/lume.lua @@ -539,10 +539,15 @@ end local serialize local serialize_map = { - [ "number" ] = tostring, [ "boolean" ] = tostring, [ "nil" ] = tostring, [ "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) stk = stk or {} if stk[t] then error("circular reference") end diff --git a/test/test.lua b/test/test.lua index 263224e..9ed3ad3 100644 --- a/test/test.lua +++ b/test/test.lua @@ -484,6 +484,10 @@ tests["lume.serialize, lume.deserialize"] = function() local t = { 1, 2, 3, 4, true, false, "cat", "dog", {1, 2, 3} } local s = lume.serialize(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 -- lume.split