mirror of
https://github.com/TangentFoxy/rxi-json.lua.git
synced 2025-07-27 18:42:17 +00:00
Improved error messages when encoding invalid table
This commit is contained in:
12
json.lua
12
json.lua
@@ -54,14 +54,13 @@ local function encode_table(val, stack)
|
||||
-- Treat as array -- check keys are valid and it is not sparse
|
||||
local n = 0
|
||||
for k in pairs(val) do
|
||||
local t = type(k)
|
||||
if t ~= "number" then
|
||||
error("unexpected key of type '" .. t .. "' in array")
|
||||
if type(k) ~= "number" then
|
||||
error("invalid table: mixed key types")
|
||||
end
|
||||
n = n + 1
|
||||
end
|
||||
if n ~= #val then
|
||||
error("unexpected sparse array")
|
||||
error("invalid table: sparse array")
|
||||
end
|
||||
-- Encode
|
||||
for i, v in ipairs(val) do
|
||||
@@ -73,9 +72,8 @@ local function encode_table(val, stack)
|
||||
else
|
||||
-- Treat as an object
|
||||
for k, v in pairs(val) do
|
||||
local t = type(k)
|
||||
if t ~= "string" then
|
||||
error("unexpected key of type '" .. t .. "' in object")
|
||||
if type(k) ~= "string" then
|
||||
error("invalid table: mixed key types")
|
||||
end
|
||||
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
|
||||
end
|
||||
|
Reference in New Issue
Block a user