mirror of https://github.com/rxi/json.lua.git
Improved error messages when encoding invalid table
parent
a674e3ec0f
commit
9fbfcde195
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
|
-- Treat as array -- check keys are valid and it is not sparse
|
||||||
local n = 0
|
local n = 0
|
||||||
for k in pairs(val) do
|
for k in pairs(val) do
|
||||||
local t = type(k)
|
if type(k) ~= "number" then
|
||||||
if t ~= "number" then
|
error("invalid table: mixed key types")
|
||||||
error("unexpected key of type '" .. t .. "' in array")
|
|
||||||
end
|
end
|
||||||
n = n + 1
|
n = n + 1
|
||||||
end
|
end
|
||||||
if n ~= #val then
|
if n ~= #val then
|
||||||
error("unexpected sparse array")
|
error("invalid table: sparse array")
|
||||||
end
|
end
|
||||||
-- Encode
|
-- Encode
|
||||||
for i, v in ipairs(val) do
|
for i, v in ipairs(val) do
|
||||||
|
@ -73,9 +72,8 @@ local function encode_table(val, stack)
|
||||||
else
|
else
|
||||||
-- Treat as an object
|
-- Treat as an object
|
||||||
for k, v in pairs(val) do
|
for k, v in pairs(val) do
|
||||||
local t = type(k)
|
if type(k) ~= "string" then
|
||||||
if t ~= "string" then
|
error("invalid table: mixed key types")
|
||||||
error("unexpected key of type '" .. t .. "' in object")
|
|
||||||
end
|
end
|
||||||
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
|
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue