Added numeric format to preserve floating point precision (closes #17).

This commit is contained in:
Paul Kulchenko 2015-05-07 22:53:47 -07:00
parent dca4b8c1d8
commit 06a575ed69
3 changed files with 16 additions and 2 deletions

View File

@ -73,6 +73,7 @@ Similar to `pcall` and `loadstring` calls, `load` returns status as the first va
* nohuge (true/False) -- disable checking numbers against undefined and huge values * nohuge (true/False) -- disable checking numbers against undefined and huge values
* maxlevel (number) -- specify max level up to which to expand nested tables * maxlevel (number) -- specify max level up to which to expand nested tables
* maxnum (number) -- specify max number of elements in a table * maxnum (number) -- specify max number of elements in a table
* numformat (string; "%.17g") -- specify format for numeric values (shortest possible round-trippable double)
* valignore (table) -- allows to specify a list of values to ignore (as keys) * valignore (table) -- allows to specify a list of values to ignore (as keys)
* keyallow (table) -- allows to specify the list of keys to be serialized. Any keys not in this list are not included in final output (as keys) * keyallow (table) -- allows to specify the list of keys to be serialized. Any keys not in this list are not included in final output (as keys)
* valtypeignore (table) -- allows to specify a list of value *types* to ignore (as keys) * valtypeignore (table) -- allows to specify a list of value *types* to ignore (as keys)

View File

@ -1,4 +1,4 @@
local n, v = "serpent", 0.282 -- (C) 2012-15 Paul Kulchenko; MIT License local n, v = "serpent", 0.283 -- (C) 2012-15 Paul Kulchenko; MIT License
local c, d = "Paul Kulchenko", "Lua serializer and pretty printer" local c, d = "Paul Kulchenko", "Lua serializer and pretty printer"
local snum = {[tostring(1/0)]='1/0 --[[math.huge]]',[tostring(-1/0)]='-1/0 --[[-math.huge]]',[tostring(0/0)]='0/0'} local snum = {[tostring(1/0)]='1/0 --[[math.huge]]',[tostring(-1/0)]='-1/0 --[[-math.huge]]',[tostring(0/0)]='0/0'}
local badtype = {thread = true, userdata = true, cdata = true} local badtype = {thread = true, userdata = true, cdata = true}
@ -16,11 +16,12 @@ local function s(t, opts)
local sparse, custom, huge = opts.sparse, opts.custom, not opts.nohuge local sparse, custom, huge = opts.sparse, opts.custom, not opts.nohuge
local space, maxl = (opts.compact and '' or ' '), (opts.maxlevel or math.huge) local space, maxl = (opts.compact and '' or ' '), (opts.maxlevel or math.huge)
local iname, comm = '_'..(name or ''), opts.comment and (tonumber(opts.comment) or math.huge) local iname, comm = '_'..(name or ''), opts.comment and (tonumber(opts.comment) or math.huge)
local numformat = opts.numformat or "%.17g"
local seen, sref, syms, symn = {}, {'local '..iname..'={}'}, {}, 0 local seen, sref, syms, symn = {}, {'local '..iname..'={}'}, {}, 0
local function gensym(val) return '_'..(tostring(tostring(val)):gsub("[^%w]",""):gsub("(%d%w+)", local function gensym(val) return '_'..(tostring(tostring(val)):gsub("[^%w]",""):gsub("(%d%w+)",
-- tostring(val) is needed because __tostring may return a non-string value -- tostring(val) is needed because __tostring may return a non-string value
function(s) if not syms[s] then symn = symn+1; syms[s] = symn end return tostring(syms[s]) end)) end function(s) if not syms[s] then symn = symn+1; syms[s] = symn end return tostring(syms[s]) end)) end
local function safestr(s) return type(s) == "number" and tostring(huge and snum[tostring(s)] or s) local function safestr(s) return type(s) == "number" and tostring(huge and snum[tostring(s)] or numformat:format(s))
or type(s) ~= "string" and tostring(s) -- escape NEWLINE/010 and EOF/026 or type(s) ~= "string" and tostring(s) -- escape NEWLINE/010 and EOF/026
or ("%q"):format(s):gsub("\010","n"):gsub("\026","\\026") end or ("%q"):format(s):gsub("\010","n"):gsub("\026","\\026") end
local function comment(s,l) return comm and (l or 0) < comm and ' --[['..select(2, pcall(tostring, s))..']]' or '' end local function comment(s,l) return comm and (l or 0) < comm and ' --[['..select(2, pcall(tostring, s))..']]' or '' end

View File

@ -308,6 +308,18 @@ do
assert(pcall(serpent.line, a) == true, "`__tostring` method that returns an error is still serialized correctly.") assert(pcall(serpent.line, a) == true, "`__tostring` method that returns an error is still serialized correctly.")
end end
do
local a = serpent.line({ pi = math.pi })
local ok, t = serpent.load(a)
assert(ok, "pi is serialized and deserialized (1/2): failed")
assert(math.pi == t.pi, "pi is deserialized with the same value as the original (1/2): failed")
a = serpent.line({pi = math.pi}, {numformat = "%1.16e"})
ok, t = serpent.load(a)
assert(ok, "pi is serialized and deserialized (2/2): failed")
assert(math.pi == t.pi, "pi is deserialized with the same value as the original with '%1.16e' format (2/2): failed")
end
-- based on https://gist.github.com/mpeterv/8360307 -- based on https://gist.github.com/mpeterv/8360307
local function random_var(is_key, deep) local function random_var(is_key, deep)
local key = math.random(1000) local key = math.random(1000)