Skip to content
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

Fix regression with parsing of scalar value documents #90

Merged
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
1 change: 1 addition & 0 deletions jsonexamples/scalars/bool.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
true
1 change: 1 addition & 0 deletions jsonexamples/scalars/null.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
null
1 change: 1 addition & 0 deletions jsonexamples/scalars/number.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10
1 change: 1 addition & 0 deletions jsonexamples/scalars/string.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"hello world"
6 changes: 5 additions & 1 deletion spec/compile_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ local files = {
"repeat.json",
"twitter_timeline.json",
"update-center.json",
"scalars/bool.json",
"scalars/null.json",
"scalars/number.json",
"scalars/string.json",
"small/adversarial.json",
"small/demo.json",
"small/flatadversarial.json",
Expand Down Expand Up @@ -100,4 +104,4 @@ if tonumber(major) >= 5 and tonumber(minor) >= 3 then

end)
end)
end
end
21 changes: 9 additions & 12 deletions src/luasimdjson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,20 @@ static void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
ondemand::parser ondemand_parser;
simdjson::padded_string jsonbuffer;

void convert_ondemand_element_to_table(lua_State *L, ondemand::value element) {
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");

switch (element.type()) {

case ondemand::json_type::array:
{
int count = 1;
lua_newtable(L);

for (auto child : element.get_array()) {
for (ondemand::value child : element.get_array()) {
lua_pushinteger(L, count);
// We need the call to value() to get
// an ondemand::value type.
convert_ondemand_element_to_table(L, child.value());
convert_ondemand_element_to_table(L, child);
lua_settable(L, -3);
count = count + 1;
}
Expand All @@ -63,7 +64,7 @@ void convert_ondemand_element_to_table(lua_State *L, ondemand::value element) {

case ondemand::json_type::object:
lua_newtable(L);
for (auto field : element.get_object()) {
for (ondemand::field field : element.get_object()) {
std::string_view s = field.unescaped_key();
lua_pushlstring(L, s.data(), s.size());
convert_ondemand_element_to_table(L, field.value());
Expand Down Expand Up @@ -163,13 +164,11 @@ static int parse(lua_State *L)
const char *json_str = luaL_checklstring(L, 1, &json_str_len);

ondemand::document doc;
ondemand::value element;

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));
element = doc;
convert_ondemand_element_to_table(L, element);
convert_ondemand_element_to_table(L, ondemand::document_reference(doc));
} catch (simdjson::simdjson_error &error) {
luaL_error(L, error.what());
}
Expand All @@ -183,13 +182,11 @@ static int parse_file(lua_State *L)

padded_string json_string;
ondemand::document doc;
ondemand::value element;

try {
json_string = padded_string::load(json_file);
doc = ondemand_parser.iterate(json_string);
element = doc;
convert_ondemand_element_to_table(L, element);
convert_ondemand_element_to_table(L, ondemand::document_reference(doc));
} catch (simdjson::simdjson_error &error) {
luaL_error(L, error.what());
}
Expand Down