Skip to content

Support nil in table / null in array #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 32 additions & 25 deletions json.lua
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ local function encode_nil(val)
return "null"
end

local function encode_string(val)
return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
end

local function encode_table(val, stack)
local res = {}
Expand All @@ -65,44 +68,48 @@ local function encode_table(val, stack)

stack[val] = true

if rawget(val, 1) ~= nil or next(val) == nil then
-- Treat as array -- check keys are valid and it is not sparse
local n = 0
if next(val) == nil then
return '[]'
end

local types = {}

for k in pairs(val) do
types[type(k)] = true
end

if #types > 1 then
error("invalid table: mixed or invalid key types")
elseif types["number"] then
-- Treat as array
local max_key = 0
for k in pairs(val) do
if type(k) ~= "number" then
error("invalid table: mixed or invalid key types")
if k > max_key then
max_key = k
end
n = n + 1
end
if n ~= #val then
error("invalid table: sparse array")
end
-- Encode
for i, v in ipairs(val) do
table.insert(res, encode(v, stack))
for i = 1, max_key do
if val[i] == nil then
table.insert(res, "null")
else
local v = encode(val[i], stack)
table.insert(res, v)
end
end
stack[val] = nil
return "[" .. table.concat(res, ",") .. "]"

else
-- Treat as an object
elseif types["string"] then
-- Treat as object
for k, v in pairs(val) do
if type(k) ~= "string" then
error("invalid table: mixed or invalid key types")
end
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
table.insert(res, encode_string(k) .. ":" .. encode(v, stack))
end
stack[val] = nil
return "{" .. table.concat(res, ",") .. "}"
else
error( string.format("invalid table: unsupported key type %s", types[1]) )
end
end


local function encode_string(val)
return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
end


local function encode_number(val)
-- Check for NaN, -inf and inf
if val ~= val or val <= -math.huge or val >= math.huge then
Expand Down
28 changes: 25 additions & 3 deletions test/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,37 @@ test("arrays", function()
end)


test("arrays with nil / null", function()
local t = {
{ [1000] = "b" },
{ nil, 2, 3, 4 },
{ nil, "test" },
{ [1] = "a", [3] = "b" },
}
for i, v in ipairs(t) do
assert( equal( v, json.decode( json.encode(v) ) ) )
end
end)


test("objects", function()
local t = { x = 10, y = 20, z = 30 }
assert( equal( t, json.decode( json.encode(t) ) ) )
end)


test("encode empty", function()
local t = {
[ '[]' ] = {},
[ '""' ] = "",
}
for k, v in pairs(t) do
local res = json.encode(v)
assert( equal(res, k), fmt("'%s' did not equal expected %s", res, k) )
end
end)


--test("strict decode", function()
-- local t = {
-- '{x : 1}',
Expand Down Expand Up @@ -203,11 +228,8 @@ end)

test("encode invalid", function()
local t = {
{ [1000] = "b" },
{ [ function() end ] = 12 },
{ nil, 2, 3, 4 },
{ x = 10, [1] = 2 },
{ [1] = "a", [3] = "b" },
{ x = 10, [4] = 5 },
}
for i, v in ipairs(t) do
Expand Down