Skip to content

Commit

Permalink
Fix incorrect handling of scalar documents with trailing content (#92)
Browse files Browse the repository at this point in the history
* Remove use of document_reference type

It is not suitable for use here since the behaviour deviates from the
regular document type when it comes to trailing content.

* Add tests for scalar value with trailing content

* Move invalid test case to invalid sub folder
  • Loading branch information
tobil4sk authored Sep 24, 2024
1 parent eab3aab commit 8e18887
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
1 change: 1 addition & 0 deletions jsonexamples/invalid/bool_trailing.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true trailing
18 changes: 18 additions & 0 deletions spec/compile_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,21 @@ if tonumber(major) >= 5 and tonumber(minor) >= 3 then
end)
end)
end

local invalid_files = {
"bool_trailing.json"
}

describe("Make sure invalid files are not accepted", function()
for _, file in ipairs(invalid_files) do
it("should fail to parse: " .. file, function()
local fileContents = loadFile("jsonexamples/invalid/" .. file)
local cjsonValue, cjsonError = pcall(function() cjson.decode(fileContents) end)
local simdjsonValue, simdjsonError = pcall(function() simdjson.parse(fileContents) end)
assert.is.False(cjsonValue)
assert.is.False(simdjsonValue)
assert(cjsonError)
assert(simdjsonError)
end)
end
end)
8 changes: 4 additions & 4 deletions src/luasimdjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ ondemand::parser ondemand_parser;
simdjson::padded_string jsonbuffer;

template<typename T>
void convert_ondemand_element_to_table(lua_State *L, T element) {
static_assert(std::is_base_of<ondemand::document_reference, T>::value || std::is_base_of<ondemand::value, T>::value, "type parameter must be document_reference or value");
void convert_ondemand_element_to_table(lua_State *L, T& element) {
static_assert(std::is_base_of<ondemand::document, T>::value || std::is_base_of<ondemand::value, T>::value, "type parameter must be document or value");

switch (element.type()) {

Expand Down Expand Up @@ -168,7 +168,7 @@ static int parse(lua_State *L)
try {
// makes a padded_string_view for a bit of quickness!
doc = ondemand_parser.iterate(get_padded_string_view(json_str, json_str_len, jsonbuffer));
convert_ondemand_element_to_table(L, ondemand::document_reference(doc));
convert_ondemand_element_to_table(L, doc);
} catch (simdjson::simdjson_error &error) {
luaL_error(L, error.what());
}
Expand All @@ -186,7 +186,7 @@ static int parse_file(lua_State *L)
try {
json_string = padded_string::load(json_file);
doc = ondemand_parser.iterate(json_string);
convert_ondemand_element_to_table(L, ondemand::document_reference(doc));
convert_ondemand_element_to_table(L, doc);
} catch (simdjson::simdjson_error &error) {
luaL_error(L, error.what());
}
Expand Down

0 comments on commit 8e18887

Please sign in to comment.