Skip to content

Commit 789d08a

Browse files
authored
Fix regression with parsing of scalar value documents (#90)
* Add tests for scalar value jsons * Fix parsing of scalar value documents With the ondemand parser, the document to value conversion fails with the error SCALAR_DOCUMENT_AS_VALUE if the document is a single null, string, bool, or number value. * Add explicit types in for loop variables Since we are now using a template type, this is no longer inferable by an IDE. Adding explicit types also reduces the number of `.value()` conversions required.
1 parent f7b3a76 commit 789d08a

File tree

6 files changed

+18
-13
lines changed

6 files changed

+18
-13
lines changed

jsonexamples/scalars/bool.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
true

jsonexamples/scalars/null.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
null

jsonexamples/scalars/number.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10

jsonexamples/scalars/string.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
"hello world"

spec/compile_spec.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ local files = {
2727
"repeat.json",
2828
"twitter_timeline.json",
2929
"update-center.json",
30+
"scalars/bool.json",
31+
"scalars/null.json",
32+
"scalars/number.json",
33+
"scalars/string.json",
3034
"small/adversarial.json",
3135
"small/demo.json",
3236
"small/flatadversarial.json",
@@ -100,4 +104,4 @@ if tonumber(major) >= 5 and tonumber(minor) >= 3 then
100104

101105
end)
102106
end)
103-
end
107+
end

src/luasimdjson.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,20 @@ static void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
4242
ondemand::parser ondemand_parser;
4343
simdjson::padded_string jsonbuffer;
4444

45-
void convert_ondemand_element_to_table(lua_State *L, ondemand::value element) {
45+
template<typename T>
46+
void convert_ondemand_element_to_table(lua_State *L, T element) {
47+
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");
48+
4649
switch (element.type()) {
4750

4851
case ondemand::json_type::array:
4952
{
5053
int count = 1;
5154
lua_newtable(L);
5255

53-
for (auto child : element.get_array()) {
56+
for (ondemand::value child : element.get_array()) {
5457
lua_pushinteger(L, count);
55-
// We need the call to value() to get
56-
// an ondemand::value type.
57-
convert_ondemand_element_to_table(L, child.value());
58+
convert_ondemand_element_to_table(L, child);
5859
lua_settable(L, -3);
5960
count = count + 1;
6061
}
@@ -63,7 +64,7 @@ void convert_ondemand_element_to_table(lua_State *L, ondemand::value element) {
6364

6465
case ondemand::json_type::object:
6566
lua_newtable(L);
66-
for (auto field : element.get_object()) {
67+
for (ondemand::field field : element.get_object()) {
6768
std::string_view s = field.unescaped_key();
6869
lua_pushlstring(L, s.data(), s.size());
6970
convert_ondemand_element_to_table(L, field.value());
@@ -163,13 +164,11 @@ static int parse(lua_State *L)
163164
const char *json_str = luaL_checklstring(L, 1, &json_str_len);
164165

165166
ondemand::document doc;
166-
ondemand::value element;
167167

168168
try {
169169
// makes a padded_string_view for a bit of quickness!
170170
doc = ondemand_parser.iterate(get_padded_string_view(json_str, json_str_len, jsonbuffer));
171-
element = doc;
172-
convert_ondemand_element_to_table(L, element);
171+
convert_ondemand_element_to_table(L, ondemand::document_reference(doc));
173172
} catch (simdjson::simdjson_error &error) {
174173
luaL_error(L, error.what());
175174
}
@@ -183,13 +182,11 @@ static int parse_file(lua_State *L)
183182

184183
padded_string json_string;
185184
ondemand::document doc;
186-
ondemand::value element;
187185

188186
try {
189187
json_string = padded_string::load(json_file);
190188
doc = ondemand_parser.iterate(json_string);
191-
element = doc;
192-
convert_ondemand_element_to_table(L, element);
189+
convert_ondemand_element_to_table(L, ondemand::document_reference(doc));
193190
} catch (simdjson::simdjson_error &error) {
194191
luaL_error(L, error.what());
195192
}

0 commit comments

Comments
 (0)