mirror of https://github.com/rxi/json.lua.git
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-1446161604pull/44/head
parent
7b07dc4c6e
commit
83b301510b
14
json.lua
14
json.lua
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue