You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
double v = parser.parse(abstract_json)["str"]["123"]["abc"].get<double>();
190
+
double v = parser.parse(abstract_json)["str"]["123"]["abc"];
191
191
cout << "number: " << v << endl;
192
192
```
193
193
194
194
C++17 Support
195
195
-------------
196
196
197
-
While the simdjson library can be used in any project using C++ 11 and above, it has special support
198
-
for C++ 17. The APIs for field iteration and error handling in particular are designed to work
199
-
nicely with C++17's destructuring syntax. For example:
197
+
While the simdjson library can be used in any project using C++ 11 and above, field iteration has special support C++ 17's destructuring syntax. For example:
@@ -259,7 +257,6 @@ The UTF-8 validation function merely checks that the input is valid UTF-8: it wo
259
257
260
258
Your input string does not need any padding. Any string will do. The `validate_utf8` function does not do any memory allocation on the heap, and it does not throw exceptions.
261
259
262
-
263
260
JSON Pointer
264
261
------------
265
262
@@ -281,41 +278,31 @@ Error Handling
281
278
--------------
282
279
283
280
All simdjson APIs that can fail return `simdjson_result<T>`, which is a <value, error_code>
284
-
pair. The error codes and values can be accessed directly, reading the error like so:
281
+
pair. You can retrieve the value with .get(), like so:
285
282
286
283
```c++
287
-
auto [doc, error] = parser.parse(json); // doc is a dom::element
284
+
dom::element doc;
285
+
auto error = parser.parse(json).get(doc);
288
286
if (error) { cerr << error << endl; exit(1); }
289
-
// Use document here now that we've checked for the error
290
287
```
291
288
292
289
When you use the code this way, it is your responsibility to check for error before using the
293
290
result: if there is an error, the result value will not be valid and using it will caused undefined
294
291
behavior.
295
292
296
-
> Note: because of the way `auto [x, y]` works in C++, you have to define new variables each time you
297
-
> use it. If your project treats aliased, this means you can't use the same names in `auto [x, error]`
298
-
> without triggering warnings or error (and particularly can't use the word "error" every time). To
299
-
> circumvent this, you can use this instead:
300
-
>
301
-
> ```c++
302
-
> dom::element doc;
303
-
> auto error = parser.parse(json).get(doc); // <-- Assigns to doc and error just like "auto [doc, error]"
304
-
> ```
305
-
306
-
307
293
We can write a "quick start" example where we attempt to parse a file and access some data, without triggering exceptions:
308
294
309
295
```C++
310
296
#include "simdjson.h"
311
297
312
298
int main(void) {
313
299
simdjson::dom::parser parser;
300
+
314
301
simdjson::dom::element tweets;
315
302
auto error = parser.load("twitter.json").get(tweets);
if ((error = tweets["search_metadata"]["count"].get(res))) {
320
307
std::cerr << "could not access keys" << std::endl;
321
308
return EXIT_FAILURE;
@@ -418,8 +405,7 @@ And another one:
418
405
cout << "number: " << v << endl;
419
406
```
420
407
421
-
Notice how we can string several operation (`parser.parse(abstract_json)["str"]["123"]["abc"].get<double>()`) and only check for the error once, a strategy we call *error chaining*.
422
-
408
+
Notice how we can string several operations (`parser.parse(abstract_json)["str"]["123"]["abc"].get(v)`) and only check for the error once, a strategy we call *error chaining*.
423
409
424
410
The next two functions will take as input a JSON document containing an array with a single element, either a string or a number. They return true upon success.
425
411
@@ -529,7 +515,8 @@ Here is a simple example, given "x.json" with this content:
529
515
530
516
```c++
531
517
dom::parser parser;
532
-
for (dom::element doc : parser.load_many(filename)) {
0 commit comments