mirror of
https://github.com/minetest/minetest.git
synced 2024-11-23 16:13:46 +01:00
minetest.serialize: Reversible number serialization (#9722)
* minetest.serialize: Reversible number to string conversion The %a format is not supported in Lua 5.1. This commit also adds two tests for number serialization.
This commit is contained in:
parent
4361bfcb4d
commit
5355cb1d87
@ -120,15 +120,8 @@ function core.serialize(x)
|
|||||||
elseif tp == "function" then
|
elseif tp == "function" then
|
||||||
return string.format("loadstring(%q)", string.dump(x))
|
return string.format("loadstring(%q)", string.dump(x))
|
||||||
elseif tp == "number" then
|
elseif tp == "number" then
|
||||||
-- Serialize integers with string.format to prevent
|
-- Serialize numbers reversibly with string.format
|
||||||
-- scientific notation, which doesn't preserve
|
return string.format("%.17g", x)
|
||||||
-- precision and breaks things like node position
|
|
||||||
-- hashes. Serialize floats normally.
|
|
||||||
if math.floor(x) == x then
|
|
||||||
return string.format("%d", x)
|
|
||||||
else
|
|
||||||
return tostring(x)
|
|
||||||
end
|
|
||||||
elseif tp == "table" then
|
elseif tp == "table" then
|
||||||
local vals = {}
|
local vals = {}
|
||||||
local idx_dumped = {}
|
local idx_dumped = {}
|
||||||
|
@ -18,6 +18,18 @@ describe("serialize", function()
|
|||||||
assert.same(test_in, test_out)
|
assert.same(test_in, test_out)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("handles precise numbers", function()
|
||||||
|
local test_in = 0.2695949158945771
|
||||||
|
local test_out = core.deserialize(core.serialize(test_in))
|
||||||
|
assert.same(test_in, test_out)
|
||||||
|
end)
|
||||||
|
|
||||||
|
it("handles big integers", function()
|
||||||
|
local test_in = 269594915894577
|
||||||
|
local test_out = core.deserialize(core.serialize(test_in))
|
||||||
|
assert.same(test_in, test_out)
|
||||||
|
end)
|
||||||
|
|
||||||
it("handles recursive structures", function()
|
it("handles recursive structures", function()
|
||||||
local test_in = { hello = "world" }
|
local test_in = { hello = "world" }
|
||||||
test_in.foo = test_in
|
test_in.foo = test_in
|
||||||
|
Loading…
Reference in New Issue
Block a user