mirror of https://github.com/rxi/json.lua.git
Updated code to allow array specifications using table.pack function. The array detection code now allows for the array to have a key n with a number value to detect the array length.
parent
ad6ac0fd8e
commit
32a59c89b0
23
json.lua
23
json.lua
|
@ -67,18 +67,25 @@ local function encode_table(val, stack)
|
|||
-- Check whether to treat as a array or object
|
||||
local array = true
|
||||
local length = 0
|
||||
for k in pairs(val) do
|
||||
if type(k) ~= "number" or k<=0 then
|
||||
array = nil
|
||||
break -- Treat as object
|
||||
else
|
||||
if k > length then
|
||||
length = k
|
||||
local nLen
|
||||
for k,v in pairs(val) do
|
||||
if (type(k) ~= "number" or k<=0) and not (k == "n" and type(v) == "number") then
|
||||
array = nil
|
||||
break -- Treat as object
|
||||
else
|
||||
if k > length then
|
||||
length = k
|
||||
end
|
||||
if k == "n" and type(v) == "number" then
|
||||
nLen = v
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if array then
|
||||
if nLen > length then
|
||||
length = nLen
|
||||
end
|
||||
-- Encode
|
||||
for i=1,length do
|
||||
table.insert(res, encode(val[i], stack))
|
||||
|
|
Loading…
Reference in New Issue