mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-29 11:32:20 +00:00
Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
27278fb887 | ||
|
8627638db0 | ||
|
1559803c70 |
@@ -329,7 +329,7 @@ f(10, 5) -- Returns 25
|
||||
### lume.serialize(x)
|
||||
Serializes the argument `x` into a string which can be loaded again using
|
||||
`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.
|
||||
```lua
|
||||
lume.serialize({a = "test", b = {1, 2, 3}, false})
|
||||
|
9
lume.lua
9
lume.lua
@@ -7,7 +7,7 @@
|
||||
-- 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 type, assert, unpack = type, assert, unpack or table.unpack
|
||||
@@ -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
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user