Skip to content

Commit

Permalink
handle encoding of number based key properties
Browse files Browse the repository at this point in the history
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:
rxi#43 (comment)
  • Loading branch information
alexandro-rezakhani authored Nov 20, 2022
1 parent 7b07dc4 commit 83b3015
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,22 @@ local function encode_table(val, stack)
end
-- Encode
for i=1,length do
table.insert(res, encode(val[i], stack))
res[#res + 1] = encode(val[i], stack)
end
stack[val] = nil
return "[" .. table.concat(res, ",") .. "]"
else
-- Treat as an object
for k, v in pairs(val) do
--[[
if type(k) ~= "string" then
error("invalid table: mixed or invalid key types")
if type(k) == "string" then
res[#res + 1] = encode(k, stack) .. ":" .. encode(v, stack)
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
]]
if k ~= "_" then
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
res[#res + 1] = encode(k, stack) .. ":" .. encode(v, stack)
end
end
stack[val] = nil
Expand Down

0 comments on commit 83b3015

Please sign in to comment.