mirror of https://github.com/rxi/json.lua.git
Fix number encoding & quadratic complexity
parent
dbf4b2dd2e
commit
aab4f644b6
63
json.lua
63
json.lua
|
@ -51,20 +51,16 @@ local function escape_char(c)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function encode_nil(val)
|
local function encode_nil(rope)
|
||||||
return "null"
|
rope[#rope + 1] = "null"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function encode_table(val, stack)
|
local function encode_table(rope, val, stack)
|
||||||
local res = {}
|
|
||||||
stack = stack or {}
|
|
||||||
|
|
||||||
-- Circular reference?
|
-- Circular reference?
|
||||||
if stack[val] then error("circular reference") end
|
if stack[val] then error("circular reference") end
|
||||||
|
|
||||||
stack[val] = true
|
stack[val] = true
|
||||||
|
|
||||||
if rawget(val, 1) ~= nil or next(val) == nil then
|
if rawget(val, 1) ~= nil or next(val) == nil then
|
||||||
-- Treat as array -- check keys are valid and it is not sparse
|
-- Treat as array -- check keys are valid and it is not sparse
|
||||||
local n = 0
|
local n = 0
|
||||||
|
@ -78,61 +74,80 @@ local function encode_table(val, stack)
|
||||||
error("invalid table: sparse array")
|
error("invalid table: sparse array")
|
||||||
end
|
end
|
||||||
-- Encode
|
-- Encode
|
||||||
|
rope[#rope + 1] = "["
|
||||||
for i, v in ipairs(val) do
|
for i, v in ipairs(val) do
|
||||||
table.insert(res, encode(v, stack))
|
if i > 1 then
|
||||||
|
rope[#rope + 1] = ","
|
||||||
|
end
|
||||||
|
encode(rope, v, stack)
|
||||||
end
|
end
|
||||||
stack[val] = nil
|
rope[#rope + 1] = "]"
|
||||||
return "[" .. table.concat(res, ",") .. "]"
|
|
||||||
|
|
||||||
else
|
else
|
||||||
-- Treat as an object
|
-- Treat as an object
|
||||||
|
rope[#rope + 1] = "{"
|
||||||
|
local first = true
|
||||||
for k, v in pairs(val) do
|
for k, v in pairs(val) do
|
||||||
if type(k) ~= "string" then
|
if type(k) ~= "string" then
|
||||||
error("invalid table: mixed or invalid key types")
|
error("invalid table: mixed or invalid key types")
|
||||||
end
|
end
|
||||||
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
|
if not first then
|
||||||
|
rope[#rope + 1] = ","
|
||||||
|
end
|
||||||
|
encode(rope, k, stack)
|
||||||
|
rope[#rope + 1] = ":"
|
||||||
|
encode(rope, v, stack)
|
||||||
|
first = false
|
||||||
end
|
end
|
||||||
stack[val] = nil
|
rope[#rope + 1] = "}"
|
||||||
return "{" .. table.concat(res, ",") .. "}"
|
|
||||||
end
|
end
|
||||||
|
stack[val] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function encode_string(val)
|
local function encode_string(rope, val)
|
||||||
return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
|
rope[#rope + 1] = '"'
|
||||||
|
rope[#rope + 1] = val:gsub('[%z\1-\31\\"]', escape_char)
|
||||||
|
rope[#rope + 1] = '"'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function encode_number(val)
|
local function encode_number(rope, val)
|
||||||
-- Check for NaN, -inf and inf
|
-- Check for NaN, -inf and inf
|
||||||
if val ~= val or val <= -math.huge or val >= math.huge then
|
if val ~= val or val <= -math.huge or val >= math.huge then
|
||||||
error("unexpected number value '" .. tostring(val) .. "'")
|
error("unexpected number value '" .. tostring(val) .. "'")
|
||||||
end
|
end
|
||||||
return string.format("%.14g", val)
|
-- See www.cs.berkeley.edu/~wkahan/ieee754status/IEEE754.PDF
|
||||||
|
-- 17 digits suffice to losslessly represent 64-bit IEEE754 floats
|
||||||
|
rope[#rope + 1] = ("%.17g"):format(val)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function encode_boolean(rope, val)
|
||||||
|
rope[#rope + 1] = val and "true" or "false"
|
||||||
|
end
|
||||||
|
|
||||||
local type_func_map = {
|
local type_func_map = {
|
||||||
[ "nil" ] = encode_nil,
|
[ "nil" ] = encode_nil,
|
||||||
[ "table" ] = encode_table,
|
[ "table" ] = encode_table,
|
||||||
[ "string" ] = encode_string,
|
[ "string" ] = encode_string,
|
||||||
[ "number" ] = encode_number,
|
[ "number" ] = encode_number,
|
||||||
[ "boolean" ] = tostring,
|
[ "boolean" ] = encode_boolean,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
encode = function(val, stack)
|
function encode(rope, val, stack)
|
||||||
local t = type(val)
|
local t = type(val)
|
||||||
local f = type_func_map[t]
|
local encoder = type_func_map[t]
|
||||||
if f then
|
if encoder then
|
||||||
return f(val, stack)
|
return encoder(rope, val, stack)
|
||||||
end
|
end
|
||||||
error("unexpected type '" .. t .. "'")
|
error("unexpected type '" .. t .. "'")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function json.encode(val)
|
function json.encode(val)
|
||||||
return ( encode(val) )
|
local rope = {}
|
||||||
|
encode(rope, val, {})
|
||||||
|
return table.concat(rope)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue