handle encoding of number based key properties

i was unable to encode a table due to some number based key properties.

i modified the encode function to check for number based keys in addition to the already present string based keys.

an error will still be returned if the key is neither string nor number based.

it works in my cases


Please see this link:
https://github.com/rxi/json.lua/pull/43#issue-1446161604
pull/44/head
alexandro-rezakhani 2022-11-20 01:21:55 -05:00 committed by GitHub
parent 7b07dc4c6e
commit 83b301510b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 6 deletions

View File

@ -90,20 +90,22 @@ local function encode_table(val, stack)
end end
-- Encode -- Encode
for i=1,length do for i=1,length do
table.insert(res, encode(val[i], stack)) res[#res + 1] = encode(val[i], stack)
end end
stack[val] = nil stack[val] = nil
return "[" .. table.concat(res, ",") .. "]" return "[" .. table.concat(res, ",") .. "]"
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 res[#res + 1] = encode(k, stack) .. ":" .. encode(v, stack)
error("invalid table: mixed or invalid key types") 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
]]
if k ~= "_" then if k ~= "_" then
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack)) res[#res + 1] = encode(k, stack) .. ":" .. encode(v, stack)
end end
end end
stack[val] = nil stack[val] = nil