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
|
-- Check whether to treat as a array or object
|
||||||
local array = true
|
local array = true
|
||||||
local length = 0
|
local length = 0
|
||||||
for k in pairs(val) do
|
local nLen
|
||||||
if type(k) ~= "number" or k<=0 then
|
for k,v in pairs(val) do
|
||||||
array = nil
|
if (type(k) ~= "number" or k<=0) and not (k == "n" and type(v) == "number") then
|
||||||
break -- Treat as object
|
array = nil
|
||||||
else
|
break -- Treat as object
|
||||||
if k > length then
|
else
|
||||||
length = k
|
if k > length then
|
||||||
|
length = k
|
||||||
|
end
|
||||||
|
if k == "n" and type(v) == "number" then
|
||||||
|
nLen = v
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if array then
|
if array then
|
||||||
|
if nLen > length then
|
||||||
|
length = nLen
|
||||||
|
end
|
||||||
-- Encode
|
-- Encode
|
||||||
for i=1,length do
|
for i=1,length do
|
||||||
table.insert(res, encode(val[i], stack))
|
table.insert(res, encode(val[i], stack))
|
||||||
|
|
Loading…
Reference in New Issue