From 83b301510b1b8191879f066865812af760ac34d0 Mon Sep 17 00:00:00 2001 From: alexandro-rezakhani <85354050+alexandro-rezakhani@users.noreply.github.com> Date: Sun, 20 Nov 2022 01:21:55 -0500 Subject: [PATCH] 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 --- json.lua | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/json.lua b/json.lua index f65c88d..dd7c8a9 100644 --- a/json.lua +++ b/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