mirror of https://github.com/rxi/json.lua.git
Merge 176068b423
into dbf4b2dd2e
commit
a9a9876773
|
@ -3,7 +3,7 @@ A lightweight JSON library for Lua
|
||||||
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
* Implemented in pure Lua: works with 5.1, 5.2, 5.3 and JIT
|
* Implemented in pure Lua: works with 5.1, 5.2, 5.3, 5.4 and JIT
|
||||||
* Fast: generally outperforms other pure Lua JSON implementations
|
* Fast: generally outperforms other pure Lua JSON implementations
|
||||||
([benchmark scripts](bench/))
|
([benchmark scripts](bench/))
|
||||||
* Tiny: around 280sloc, 9kb
|
* Tiny: around 280sloc, 9kb
|
||||||
|
|
57
json.lua
57
json.lua
|
@ -2,6 +2,7 @@
|
||||||
-- json.lua
|
-- json.lua
|
||||||
--
|
--
|
||||||
-- Copyright (c) 2020 rxi
|
-- Copyright (c) 2020 rxi
|
||||||
|
-- Copyright (c) 2020 alexandro-rezakhani
|
||||||
--
|
--
|
||||||
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
|
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
-- this software and associated documentation files (the "Software"), to deal in
|
-- this software and associated documentation files (the "Software"), to deal in
|
||||||
|
@ -22,7 +23,7 @@
|
||||||
-- SOFTWARE.
|
-- SOFTWARE.
|
||||||
--
|
--
|
||||||
|
|
||||||
local json = { _version = "0.1.2" }
|
local json = { _version = "0.1.2fix" }
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
-- Encode
|
-- Encode
|
||||||
|
@ -64,22 +65,30 @@ local function encode_table(val, stack)
|
||||||
if stack[val] then error("circular reference") end
|
if stack[val] then error("circular reference") end
|
||||||
|
|
||||||
stack[val] = true
|
stack[val] = true
|
||||||
|
-- Check whether to treat as a array or object
|
||||||
if rawget(val, 1) ~= nil or next(val) == nil then
|
local array = true
|
||||||
-- Treat as array -- check keys are valid and it is not sparse
|
local length = 0
|
||||||
local n = 0
|
local nLen = 0
|
||||||
for k in pairs(val) do
|
for k,v in pairs(val) do
|
||||||
if type(k) ~= "number" then
|
if (type(k) ~= "number" or k<=0) and not (k == "n" and type(v) == "number") then
|
||||||
error("invalid table: mixed or invalid key types")
|
array = nil
|
||||||
end
|
break -- Treat as object
|
||||||
n = n + 1
|
else
|
||||||
end
|
if k > length then
|
||||||
if n ~= #val then
|
length = k
|
||||||
error("invalid table: sparse array")
|
end
|
||||||
end
|
if k == "n" and type(v) == "number" then
|
||||||
|
nLen = v
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if array then
|
||||||
|
if nLen > length then
|
||||||
|
length = nLen
|
||||||
|
end
|
||||||
-- Encode
|
-- Encode
|
||||||
for i, v in ipairs(val) do
|
for i=1,length do
|
||||||
table.insert(res, encode(v, stack))
|
table.insert(res, encode(val[i], stack))
|
||||||
end
|
end
|
||||||
stack[val] = nil
|
stack[val] = nil
|
||||||
return "[" .. table.concat(res, ",") .. "]"
|
return "[" .. table.concat(res, ",") .. "]"
|
||||||
|
@ -87,10 +96,13 @@ local function encode_table(val, stack)
|
||||||
else
|
else
|
||||||
-- Treat as an object
|
-- Treat as an object
|
||||||
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")
|
res[#res + 1] = encode(k, stack) .. ":" .. encode(v, stack)
|
||||||
|
elseif type(k) == "number" then
|
||||||
|
res[#res + 1] = encode(string.format(k), stack) .. ":" .. encode(v, stack)
|
||||||
|
else
|
||||||
|
error("invalid table: mixed or invalid key types");
|
||||||
end
|
end
|
||||||
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
|
|
||||||
end
|
end
|
||||||
stack[val] = nil
|
stack[val] = nil
|
||||||
return "{" .. table.concat(res, ",") .. "}"
|
return "{" .. table.concat(res, ",") .. "}"
|
||||||
|
@ -108,7 +120,12 @@ local function encode_number(val)
|
||||||
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)
|
local intVal = math.tointeger(val)
|
||||||
|
if intVal == val then
|
||||||
|
return string.format("%d", intVal)
|
||||||
|
else
|
||||||
|
return string.format("%.14g", val)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue