@@ -102,8 +102,6 @@ for (dom::object car : cars) {
102
102
}
103
103
```
104
104
105
-
106
-
107
105
JSON Pointer
108
106
------------
109
107
@@ -144,7 +142,7 @@ behavior.
144
142
>
145
143
> ```c++
146
144
> dom::element doc;
147
- > error_code error;
145
+ > simdjson:: error_code error;
148
146
> parser.parse(json).tie(doc, error); // <-- Assigns to doc and error just like "auto [doc, error]"
149
147
> ```
150
148
@@ -154,48 +152,50 @@ This is how the example in "Using the Parsed JSON" could be written using only e
154
152
155
153
```c++
156
154
auto cars_json = R"( [
157
- { "make": "Toyota", "model": "Camry", "year": 2018, "tire_pressure": [ 40.1, 39.9, 37.7, 40.4 ] },
158
- { "make": "Kia", "model": "Soul", "year": 2012, "tire_pressure": [ 30.1, 31.0, 28.6, 28.7 ] },
159
- { "make": "Toyota", "model": "Tercel", "year": 1999, "tire_pressure": [ 29.8, 30.0, 30.2, 30.5 ] }
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 ] }
160
158
] )"_padded;
161
159
dom::parser parser;
162
- auto [doc , error] = parser.parse(cars_json);
160
+ auto [cars , error] = parser.parse(cars_json).get<dom::array>( );
163
161
if (error) { cerr << error << endl; exit(1); }
164
162
165
163
// Iterating through an array of objects
166
164
for (dom::element car_element : cars) {
167
- dom::object car;
168
- car_element.get<dom::object>().tie(car, error);
169
- if (error) { cerr << error << endl; exit(1); }
165
+ dom::object car;
166
+ car_element.get<dom::object>().tie(car, error);
167
+ if (error) { cerr << error << endl; exit(1); }
170
168
171
- // Accessing a field by name
172
- dom::element make, model;
173
- car["make"].tie(make, error);
174
- if (error) { cerr << error << endl; exit(1); }
175
- car["model"].tie(model, error);
176
- if (error) { cerr << error << endl; exit(1); }
177
- cout << "Make/Model: " << make << "/" << model << endl;
169
+ // Accessing a field by name
170
+ dom::element make, model;
171
+ car["make"].tie(make, error);
172
+ if (error) { cerr << error << endl; exit(1); }
173
+ car["model"].tie(model, error);
174
+ if (error) { cerr << error << endl; exit(1); }
175
+ cout << "Make/Model: " << make << "/" << model << endl;
178
176
179
- // Casting a JSON element to an integer
180
- uint64_t year;
181
- car["year"].get<uint64_t>().tie(year, error);
182
- if (error) { cerr << error << endl; exit(1); }
183
- cout << "- This car is " << 2020 - year << "years old." << endl;
177
+ // Casting a JSON element to an integer
178
+ uint64_t year;
179
+ car["year"].get<uint64_t>().tie(year, error);
180
+ if (error) { cerr << error << endl; exit(1); }
181
+ cout << "- This car is " << 2020 - year << "years old." << endl;
184
182
185
- // Iterating through an array of floats
186
- double total_tire_pressure = 0;
187
- for (dom::element tire_pressure_element : car["tire_pressure"]) {
183
+ // Iterating through an array of floats
184
+ double total_tire_pressure = 0;
185
+ dom::array tire_pressure_array;
186
+ car["tire_pressure"].get<dom::array>().tie(tire_pressure_array, error);
187
+ if (error) { cerr << error << endl; exit(1); }
188
+ for (dom::element tire_pressure_element : tire_pressure_array) {
188
189
double tire_pressure;
189
- tire_pressure_element.get<uint64_t >().tie(tire_pressure, error);
190
+ tire_pressure_element.get<double >().tie(tire_pressure, error);
190
191
if (error) { cerr << error << endl; exit(1); }
191
192
total_tire_pressure += tire_pressure;
192
- }
193
- cout << "- Average tire pressure: " << (total_tire_pressure / 4) << endl;
193
+ }
194
+ cout << "- Average tire pressure: " << (total_tire_pressure / 4) << endl;
194
195
195
- // Writing out all the information about the car
196
- for (auto [key, value] : car) {
196
+ // Writing out all the information about the car
197
+ for (auto [key, value] : car) {
197
198
cout << "- " << key << ": " << value << endl;
198
- }
199
199
}
200
200
```
201
201
@@ -215,14 +215,15 @@ Newline-Delimited JSON (ndjson) and JSON lines
215
215
216
216
The simdjson library also support multithreaded JSON streaming through a large file containing many smaller JSON documents in either [ ndjson] ( http://ndjson.org ) or [ JSON lines] ( http://jsonlines.org ) format. If your JSON documents all contain arrays or objects, we even support direct file concatenation without whitespace. The concatenated file has no size restrictions (including larger than 4GB), though each individual document must be less than 4GB.
217
217
218
- Here is a simple example:
218
+ Here is a simple example, given "x.json" with this content :
219
219
220
- ``` cpp
221
- auto ndjson = R"(
220
+ ``` json
222
221
{ "foo" : 1 }
223
222
{ "foo" : 2 }
224
223
{ "foo" : 3 }
225
- )" _padded;
224
+ ```
225
+
226
+ ``` c++
226
227
dom::parser parser;
227
228
for (dom::element doc : parser.load_many(filename)) {
228
229
cout << doc[ "foo"] << endl;
0 commit comments