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.

pull/28/head
aryajur 2020-08-18 21:42:49 -07:00
parent ad6ac0fd8e
commit 32a59c89b0
1 changed files with 15 additions and 8 deletions

View File

@ -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
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
if array then
if nLen > length then
length = nLen
end
-- Encode
for i=1,length do
table.insert(res, encode(val[i], stack))