mirror of https://github.com/rxi/json.lua.git
Added check to prevent sparse arrays > 100 getting through
parent
f521e4ac03
commit
75127d2c7d
5
json.lua
5
json.lua
|
@ -68,6 +68,7 @@ local function encode_table(val, stack)
|
|||
local array = true
|
||||
local length = 0
|
||||
local nLen = 0
|
||||
local count = 0
|
||||
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
|
||||
|
@ -79,20 +80,20 @@ local function encode_table(val, stack)
|
|||
if k == "n" and type(v) == "number" then
|
||||
nLen = v
|
||||
end
|
||||
count = count + 1
|
||||
end
|
||||
end
|
||||
|
||||
if array then
|
||||
if nLen > length then
|
||||
length = nLen
|
||||
end
|
||||
if array and not (length > 100 and count ~= length) then -- Check Array detected but sparse > 100 length then treat as object
|
||||
-- Encode
|
||||
for i=1,length do
|
||||
table.insert(res, encode(val[i], stack))
|
||||
end
|
||||
stack[val] = nil
|
||||
return "[" .. table.concat(res, ",") .. "]"
|
||||
|
||||
else
|
||||
-- Treat as an object
|
||||
for k, v in pairs(val) do
|
||||
|
|
Loading…
Reference in New Issue