@@ -8,8 +8,9 @@ An overview of what you need to know to use simdjson, with examples.
8
8
* [ Using the Parsed JSON] ( #using-the-parsed-json )
9
9
* [ JSON Pointer] ( #json-pointer )
10
10
* [ Error Handling] ( #error-handling )
11
- * [ Error Handling Example] ( #error-handling-example )
12
- * [ Exceptions] ( #exceptions )
11
+ * [ Error Handling Example] ( #error-handling-example )
12
+ * [ Exceptions] ( #exceptions )
13
+ * [ Tree Walking and JSON Element Types] ( #tree-walking-and-json-element-types )
13
14
* [ Newline-Delimited JSON (ndjson) and JSON lines] ( #newline-delimited-json-ndjson-and-json-lines )
14
15
* [ Thread Safety] ( #thread-safety )
15
16
@@ -67,6 +68,8 @@ Once you have an element, you can navigate it with idiomatic C++ iterators, oper
67
68
first element.
68
69
> Note that array[ 0] does not compile, because implementing [ ] gives the impression indexing is a
69
70
> O(1) operation, which it is not presently in simdjson.
71
+ * ** Checking an Element Type:** You can check an element's type with ` element.type() ` . It
72
+ returns an ` element_type ` .
70
73
71
74
Here are some examples of all of the above:
72
75
@@ -152,9 +155,9 @@ This is how the example in "Using the Parsed JSON" could be written using only e
152
155
153
156
```c++
154
157
auto cars_json = R"( [
155
- { "make": "Toyota", "model": "Camry", "year": 2018, "tire_pressure": [ 40.1, 39.9, 37.7, 40.4 ] },
156
- { "make": "Kia", "model": "Soul", "year": 2012, "tire_pressure": [ 30.1, 31.0, 28.6, 28.7 ] },
157
- { "make": "Toyota", "model": "Tercel", "year": 1999, "tire_pressure": [ 29.8, 30.0, 30.2, 30.5 ] }
158
+ { "make": "Toyota", "model": "Camry", "year": 2018, "tire_pressure": [ 40.1, 39.9, 37.7, 40.4 ] },
159
+ { "make": "Kia", "model": "Soul", "year": 2012, "tire_pressure": [ 30.1, 31.0, 28.6, 28.7 ] },
160
+ { "make": "Toyota", "model": "Tercel", "year": 1999, "tire_pressure": [ 29.8, 30.0, 30.2, 30.5 ] }
158
161
] )"_padded;
159
162
dom::parser parser;
160
163
auto [cars, error] = parser.parse(cars_json).get<dom::array>();
@@ -210,6 +213,60 @@ dom::element doc = parser.parse(json); // Throws an exception if there was an er
210
213
When used this way, a ` simdjson_error ` exception will be thrown if an error occurs, preventing the
211
214
program from continuing if there was an error.
212
215
216
+ Tree Walking and JSON Element Types
217
+ -----------------------------------
218
+
219
+ Sometimes you don't necessarily have a document with a known type, and are trying to generically
220
+ inspect or walk over JSON elements. To do that, you can use iterators and the type() method. For
221
+ example, here's a quick and dirty recursive function that verbosely prints the JSON document as JSON
222
+ (* ignoring nuances like trailing commas and escaping strings, for brevity's sake):
223
+
224
+ ``` c++
225
+ void print_json (dom::element element) {
226
+ switch (element.type()) {
227
+ case dom::element_type::ARRAY:
228
+ cout << "[ ";
229
+ for (dom::element child : dom::array(element)) {
230
+ print_json(child);
231
+ cout << ",";
232
+ }
233
+ cout << "] ";
234
+ break;
235
+ case dom::element_type::OBJECT:
236
+ cout << "{";
237
+ for (dom::key_value_pair field : dom::object(element)) {
238
+ cout << "\" " << field.key << "\" : ";
239
+ print_json(field.value);
240
+ }
241
+ cout << "}";
242
+ break;
243
+ case dom::element_type::INT64:
244
+ cout << int64_t(element) << endl;
245
+ break;
246
+ case dom::element_type::UINT64:
247
+ cout << uint64_t(element) << endl;
248
+ break;
249
+ case dom::element_type::DOUBLE:
250
+ cout << double(element) << endl;
251
+ break;
252
+ case dom::element_type::STRING:
253
+ cout << std::string_view(element) << endl;
254
+ break;
255
+ case dom::element_type::BOOL:
256
+ cout << bool(element) << endl;
257
+ break;
258
+ case dom::element_type::NULL_VALUE:
259
+ cout << "null" << endl;
260
+ break;
261
+ }
262
+ }
263
+
264
+ void basics_treewalk_1() {
265
+ dom::parser parser;
266
+ print_json(parser.load("twitter.json"));
267
+ }
268
+ ```
269
+
213
270
Newline-Delimited JSON (ndjson) and JSON lines
214
271
----------------------------------------------
215
272
0 commit comments