Skip to content

Commit 4f72d5c

Browse files
authored
This adds another example (simdjson#785)
1 parent 9e9e337 commit 4f72d5c

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

doc/basics.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,27 @@ for (dom::object car : parser.parse(cars_json)) {
124124
}
125125
```
126126

127+
Here is a different example illustrating the same ideas:
128+
129+
```C++
130+
auto abstract_json = R"( [
131+
{ "12345" : {"a":12.34, "b":56.78, "c": 9998877} },
132+
{ "12545" : {"a":11.44, "b":12.78, "c": 11111111} }
133+
] )"_padded;
134+
dom::parser parser;
135+
136+
// Parse and iterate through an array of objects
137+
for (dom::object obj : parser.parse(abstract_json)) {
138+
for(const auto& key_value : obj) {
139+
cout << "key: " << key_value.key << " : ";
140+
dom::object innerobj = key_value.value;
141+
cout << "a: " << double(innerobj["a"]) << ", ";
142+
cout << "b: " << double(innerobj["b"]) << ", ";
143+
cout << "c: " << int64_t(innerobj["c"]) << endl;
144+
}
145+
}
146+
```
147+
127148
C++17 Support
128149
-------------
129150

@@ -277,6 +298,49 @@ for (auto field : car) {
277298
}
278299
```
279300

301+
Here is another example:
302+
303+
```C++
304+
auto abstract_json = R"( [
305+
{ "12345" : {"a":12.34, "b":56.78, "c": 9998877} },
306+
{ "12545" : {"a":11.44, "b":12.78, "c": 11111111} }
307+
] )"_padded;
308+
dom::parser parser;
309+
dom::array rootarray;
310+
simdjson::error_code error;
311+
parser.parse(abstract_json).get<dom::array>().tie(rootarray, error);
312+
if (error) { cerr << error << endl; exit(1); }
313+
// Iterate through an array of objects
314+
for (dom::element elem : rootarray) {
315+
dom::object obj;
316+
elem.get<dom::object>().tie(obj, error);
317+
if (error) { cerr << error << endl; exit(1); }
318+
for(auto & key_value : obj) {
319+
cout << "key: " << key_value.key << " : ";
320+
dom::object innerobj;
321+
key_value.value.get<dom::object>().tie(innerobj, error);
322+
if (error) { cerr << error << endl; exit(1); }
323+
324+
double va;
325+
innerobj["a"].get<double>().tie(va, error);
326+
if (error) { cerr << error << endl; exit(1); }
327+
cout << "a: " << va << ", ";
328+
329+
double vb;
330+
innerobj["b"].get<double>().tie(vb, error);
331+
if (error) { cerr << error << endl; exit(1); }
332+
cout << "b: " << vb << ", ";
333+
334+
int64_t vc;
335+
innerobj["c"].get<int64_t>().tie(vc, error);
336+
if (error) { cerr << error << endl; exit(1); }
337+
cout << "c: " << vc << endl;
338+
339+
}
340+
}
341+
342+
```
343+
280344
### Exceptions
281345

282346
Users more comfortable with an exception flow may choose to directly cast the `simdjson_result<T>` to the desired type:

tests/readme_examples.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ void basics_dom_1() {
5252
}
5353
}
5454

55+
56+
5557
void basics_dom_2() {
5658
auto cars_json = R"( [
5759
{ "make": "Toyota", "model": "Camry", "year": 2018, "tire_pressure": [ 40.1, 39.9, 37.7, 40.4 ] },
@@ -63,6 +65,27 @@ void basics_dom_2() {
6365
cout << cars.at("0/tire_pressure/1") << endl; // Prints 39.9
6466
}
6567

68+
void basics_dom_3() {
69+
auto abstract_json = R"( [
70+
{ "12345" : {"a":12.34, "b":56.78, "c": 9998877} },
71+
{ "12545" : {"a":11.44, "b":12.78, "c": 11111111} }
72+
] )"_padded;
73+
dom::parser parser;
74+
75+
// Parse and iterate through an array of objects
76+
for (dom::object obj : parser.parse(abstract_json)) {
77+
for(const auto& key_value : obj) {
78+
cout << "key: " << key_value.key << " : ";
79+
dom::object innerobj = key_value.value;
80+
cout << "a: " << double(innerobj["a"]) << ", ";
81+
cout << "b: " << double(innerobj["b"]) << ", ";
82+
cout << "c: " << int64_t(innerobj["c"]) << endl;
83+
}
84+
}
85+
}
86+
87+
88+
6689
namespace treewalk_1 {
6790
void print_json(dom::element element) {
6891
switch (element.type()) {
@@ -210,5 +233,8 @@ SIMDJSON_POP_DISABLE_WARNINGS
210233
#endif
211234

212235
int main() {
236+
basics_dom_1();
237+
basics_dom_2();
238+
basics_dom_3();
213239
return 0;
214240
}

tests/readme_examples_noexceptions.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,46 @@ void basics_error_3() {
7777
}
7878
}
7979
}
80+
void basics_error_4() {
81+
auto abstract_json = R"( [
82+
{ "12345" : {"a":12.34, "b":56.78, "c": 9998877} },
83+
{ "12545" : {"a":11.44, "b":12.78, "c": 11111111} }
84+
] )"_padded;
85+
dom::parser parser;
86+
dom::array rootarray;
87+
simdjson::error_code error;
88+
parser.parse(abstract_json).get<dom::array>().tie(rootarray, error);
89+
if (error) { cerr << error << endl; exit(1); }
90+
// Iterate through an array of objects
91+
for (dom::element elem : rootarray) {
92+
dom::object obj;
93+
elem.get<dom::object>().tie(obj, error);
94+
if (error) { cerr << error << endl; exit(1); }
95+
for(auto & key_value : obj) {
96+
cout << "key: " << key_value.key << " : ";
97+
dom::object innerobj;
98+
key_value.value.get<dom::object>().tie(innerobj, error);
99+
if (error) { cerr << error << endl; exit(1); }
100+
101+
double va;
102+
innerobj["a"].get<double>().tie(va, error);
103+
if (error) { cerr << error << endl; exit(1); }
104+
cout << "a: " << va << ", ";
105+
106+
double vb;
107+
innerobj["b"].get<double>().tie(vb, error);
108+
if (error) { cerr << error << endl; exit(1); }
109+
cout << "b: " << vb << ", ";
110+
111+
int64_t vc;
112+
innerobj["c"].get<int64_t>().tie(vc, error);
113+
if (error) { cerr << error << endl; exit(1); }
114+
cout << "c: " << vc << endl;
115+
116+
}
117+
}
118+
}
119+
80120

81121
#ifdef SIMDJSON_CPLUSPLUS17
82122
void basics_error_3_cpp17() {
@@ -131,5 +171,8 @@ void basics_error_3_cpp17() {
131171
#endif
132172

133173
int main() {
174+
basics_error_2();
175+
basics_error_3();
176+
basics_error_4();
134177
return 0;
135178
}

0 commit comments

Comments
 (0)