Skip to content

Commit 83b3015

Browse files
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: rxi#43 (comment)
1 parent 7b07dc4 commit 83b3015

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

json.lua

+8-6
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,22 @@ local function encode_table(val, stack)
9090
end
9191
-- Encode
9292
for i=1,length do
93-
table.insert(res, encode(val[i], stack))
93+
res[#res + 1] = encode(val[i], stack)
9494
end
9595
stack[val] = nil
9696
return "[" .. table.concat(res, ",") .. "]"
9797
else
9898
-- Treat as an object
9999
for k, v in pairs(val) do
100-
--[[
101-
if type(k) ~= "string" then
102-
error("invalid table: mixed or invalid key types")
100+
if type(k) == "string" then
101+
res[#res + 1] = encode(k, stack) .. ":" .. encode(v, stack)
102+
elseif type(k) == "number" then
103+
res[#res + 1] = encode(string.format(k), stack) .. ":" .. encode(v, stack)
104+
else
105+
error("invalid table: mixed or invalid key types");
103106
end
104-
]]
105107
if k ~= "_" then
106-
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
108+
res[#res + 1] = encode(k, stack) .. ":" .. encode(v, stack)
107109
end
108110
end
109111
stack[val] = nil

0 commit comments

Comments
 (0)