diff --git a/jsonexamples/scalars/bool.json b/jsonexamples/scalars/bool.json new file mode 100644 index 0000000..27ba77d --- /dev/null +++ b/jsonexamples/scalars/bool.json @@ -0,0 +1 @@ +true diff --git a/jsonexamples/scalars/null.json b/jsonexamples/scalars/null.json new file mode 100644 index 0000000..19765bd --- /dev/null +++ b/jsonexamples/scalars/null.json @@ -0,0 +1 @@ +null diff --git a/jsonexamples/scalars/number.json b/jsonexamples/scalars/number.json new file mode 100644 index 0000000..f599e28 --- /dev/null +++ b/jsonexamples/scalars/number.json @@ -0,0 +1 @@ +10 diff --git a/jsonexamples/scalars/string.json b/jsonexamples/scalars/string.json new file mode 100644 index 0000000..cd4bc1a --- /dev/null +++ b/jsonexamples/scalars/string.json @@ -0,0 +1 @@ +"hello world" diff --git a/spec/compile_spec.lua b/spec/compile_spec.lua index 1a612fa..13a5bbd 100644 --- a/spec/compile_spec.lua +++ b/spec/compile_spec.lua @@ -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", @@ -100,4 +104,4 @@ if tonumber(major) >= 5 and tonumber(minor) >= 3 then end) end) -end \ No newline at end of file +end diff --git a/src/luasimdjson.cpp b/src/luasimdjson.cpp index d415e34..8b97b0d 100644 --- a/src/luasimdjson.cpp +++ b/src/luasimdjson.cpp @@ -42,7 +42,10 @@ 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 +void convert_ondemand_element_to_table(lua_State *L, T element) { + static_assert(std::is_base_of::value || std::is_base_of::value, "type parameter must be document_reference or value"); + switch (element.type()) { case ondemand::json_type::array: @@ -50,11 +53,9 @@ void convert_ondemand_element_to_table(lua_State *L, ondemand::value element) { 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; } @@ -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()); @@ -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()); } @@ -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()); }