Skip to content
Merged
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
2 changes: 1 addition & 1 deletion lua/qjson/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ function _M.decode(json_str)
elseif rt == T_ARR then
return setmetatable(view, LazyArray)
else
error("qjson: top-level JSON value is not an object or array")
return decode_cursor(view, root_box)
end
end

Expand Down
26 changes: 26 additions & 0 deletions tests/lua/lazy_table_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
local qjson = require("qjson")

describe("qjson.decode — top-level scalars", function()
it("decodes top-level numbers", function()
assert.are.equal(42, qjson.decode("42"))
assert.are.equal(3.14, qjson.decode("3.14"))
end)

it("decodes top-level strings (including escapes)", function()
assert.are.equal("hello", qjson.decode('"hello"'))
assert.are.equal('line\n"break"', qjson.decode('"line\\n\\"break\\""'))
end)

it("decodes top-level booleans", function()
assert.is_true(qjson.decode("true"))
assert.is_false(qjson.decode("false"))
end)

it("decodes top-level null as qjson.null", function()
assert.are.equal(qjson.null, qjson.decode("null"))
end)

it("still errors for invalid JSON and empty input", function()
assert.has_error(function() qjson.decode("not-json") end)
assert.has_error(function() qjson.decode("") end)
end)
end)

describe("LazyObject __index — scalars", function()
it("reads a string field", function()
local t = qjson.decode('{"k":"hello"}')
Expand Down
Loading