Skip to content

Commit a2efaf4

Browse files
added atPointer method
1 parent 7fb13e1 commit a2efaf4

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,18 @@ local response = simdjson.open([[
7171
}
7272
}
7373
]])
74-
print(response:at("Image/Width"))
74+
print(response:atPointer("/Image/Width"))
7575

7676
-- OR to parse a file from disk
7777
local fileResponse = simdjson.open("jsonexamples/twitter.json")
78-
print(fileResponse:at("statuses/0/id")) --using a JSON pointer
78+
print(fileResponse:atPointer("/statuses/0/id")) --using a JSON pointer
7979

8080
```
81+
Starting with version 0.5.0, the the `atPointer` method is JSON pointer compliant. The previous pointer implementation is considered deprecated, but is still available with the `at` method.
82+
8183
The `open` and `parse` codeblocks should print out the same values. It's worth noting that the JSON pointer indexes from 0.
8284

83-
This lazy style of using the simdjson data structure could also be used with array access in the future, and would result in ultra-fast JSON parsing.
85+
This lazy style of using the simdjson data structure could also be used with array access in the future, and would result in ultra-fast JSON "parsing".
8486

8587
## Error Handling
8688
lua-simdjson will error out with any errors from simdjson encountered while parsing. They are very good at helping identify what has gone wrong during parsing.

src/luasimdjson.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,13 +210,34 @@ static int ParsedObject_at(lua_State *L) {
210210
return 1;
211211
}
212212

213+
static int ParsedObject_atPointer(lua_State *L) {
214+
dom::document* document = (*reinterpret_cast<ParsedObject**>(luaL_checkudata(L, 1, LUA_MYOBJECT)))->get();
215+
const char *pointer = luaL_checkstring(L, 2);
216+
217+
dom::element returned_element;
218+
simdjson::error_code error;
219+
220+
dom::element element = document->root();
221+
222+
element.at_pointer(pointer).tie(returned_element, error);
223+
if (error) {
224+
luaL_error(L, error_message(error));
225+
return 1;
226+
}
227+
228+
convert_element_to_table(L, returned_element);
229+
230+
return 1;
231+
}
232+
213233
static int ParsedObject_newindex(lua_State *L) {
214234
luaL_error(L, "This should be treated as a read-only table. We may one day add array access for the elements, and it'll likely not be modifiable.");
215235
return 1;
216236
}
217237

218238
static const struct luaL_Reg arraylib_m [] = {
219239
{"at", ParsedObject_at},
240+
{"atPointer", ParsedObject_atPointer},
220241
{"__newindex", ParsedObject_newindex},
221242
{"__gc", ParsedObject_delete},
222243
{NULL, NULL}
@@ -242,4 +263,4 @@ int luaopen_simdjson (lua_State *L) {
242263
lua_setfield(L, -2, "_VERSION");
243264

244265
return 1;
245-
}
266+
}

0 commit comments

Comments
 (0)