3 Commits

Author SHA1 Message Date
rxi
27278fb887 Version 2.2.2 2015-09-28 21:45:14 +01:00
rxi
8627638db0 Added support for NaN, inf and -inf to lume.serialize; updated tests 2015-09-23 19:32:25 +01:00
rxi
1559803c70 Updated README for lume.serialize changes 2015-09-04 18:59:59 +01:00
3 changed files with 12 additions and 3 deletions

View File

@@ -329,7 +329,7 @@ f(10, 5) -- Returns 25
### lume.serialize(x) ### lume.serialize(x)
Serializes the argument `x` into a string which can be loaded again using Serializes the argument `x` into a string which can be loaded again using
`lume.deserialize()`. Only booleans, numbers, tables and strings can be `lume.deserialize()`. Only booleans, numbers, tables and strings can be
serialized. Circular references are not handled; all nested tables are serialized. Circular references will result in an error; all nested tables are
serialized as unique tables. serialized as unique tables.
```lua ```lua
lume.serialize({a = "test", b = {1, 2, 3}, false}) lume.serialize({a = "test", b = {1, 2, 3}, false})

View File

@@ -7,7 +7,7 @@
-- under the terms of the MIT license. See LICENSE for details. -- under the terms of the MIT license. See LICENSE for details.
-- --
local lume = { _version = "2.2.1" } local lume = { _version = "2.2.2" }
local pairs, ipairs = pairs, ipairs local pairs, ipairs = pairs, ipairs
local type, assert, unpack = type, assert, unpack or table.unpack local type, assert, unpack = type, assert, unpack or table.unpack
@@ -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