elseif tp == "function" then
return string.format("loadstring(%q)", string.dump(x))
elseif tp == "number" then
- -- Serialize integers with string.format to prevent
- -- scientific notation, which doesn't preserve
- -- 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
+ -- Serialize numbers reversibly with string.format
+ return string.format("%.17g", x)
elseif tp == "table" then
local vals = {}
local idx_dumped = {}
assert.same(test_in, test_out)
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()
local test_in = { hello = "world" }
test_in.foo = test_in