From 8627638db003471aded37187bdc923689186e94a Mon Sep 17 00:00:00 2001 From: rxi Date: Wed, 23 Sep 2015 19:32:25 +0100 Subject: [PATCH] Added support for NaN, inf and -inf to lume.serialize; updated tests --- lume.lua | 7 ++++++- test/test.lua | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) 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