Skip to content

Commit f397b6f

Browse files
authored
Another example. (simdjson#790)
* Another example. * Adding a reference to error chaining.
1 parent c5684a6 commit f397b6f

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

doc/basics.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ for (dom::object obj : parser.parse(abstract_json)) {
145145
}
146146
```
147147

148+
And another one:
149+
150+
151+
```C++
152+
auto abstract_json = R"(
153+
{ "str" : { "123" : {"abc" : 3.14 } } } )"_padded;
154+
dom::parser parser;
155+
double v = parser.parse(abstract_json)["str"]["123"]["abc"].get<double>();
156+
cout << "number: " << v << endl;
157+
```
158+
159+
148160
C++17 Support
149161
-------------
150162

@@ -341,6 +353,21 @@ for (dom::element elem : rootarray) {
341353

342354
```
343355

356+
And another one:
357+
358+
```C++
359+
auto abstract_json = R"(
360+
{ "str" : { "123" : {"abc" : 3.14 } } } )"_padded;
361+
dom::parser parser;
362+
double v;
363+
simdjson::error_code error;
364+
parser.parse(abstract_json)["str"]["123"]["abc"].get<double>().tie(v, error);
365+
if (error) { cerr << error << endl; exit(1); }
366+
cout << "number: " << v << endl;
367+
```
368+
369+
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*.
370+
344371
### Exceptions
345372
346373
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ void basics_dom_3() {
8484
}
8585
}
8686

87+
void basics_dom_4() {
88+
auto abstract_json = R"(
89+
{ "str" : { "123" : {"abc" : 3.14 } } } )"_padded;
90+
dom::parser parser;
91+
double v = parser.parse(abstract_json)["str"]["123"]["abc"].get<double>();
92+
cout << "number: " << v << endl;
93+
}
8794

8895

8996
namespace treewalk_1 {
@@ -236,5 +243,6 @@ int main() {
236243
basics_dom_1();
237244
basics_dom_2();
238245
basics_dom_3();
246+
basics_dom_4();
239247
return 0;
240248
}

tests/readme_examples_noexceptions.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ void basics_error_3() {
7777
}
7878
}
7979
}
80+
81+
8082
void basics_error_4() {
8183
auto abstract_json = R"( [
8284
{ "12345" : {"a":12.34, "b":56.78, "c": 9998877} },
@@ -117,6 +119,17 @@ void basics_error_4() {
117119
}
118120
}
119121

122+
void basics_error_5() {
123+
auto abstract_json = R"(
124+
{ "str" : { "123" : {"abc" : 3.14 } } } )"_padded;
125+
dom::parser parser;
126+
double v;
127+
simdjson::error_code error;
128+
parser.parse(abstract_json)["str"]["123"]["abc"].get<double>().tie(v, error);
129+
if (error) { cerr << error << endl; exit(1); }
130+
cout << "number: " << v << endl;
131+
}
132+
120133

121134
#ifdef SIMDJSON_CPLUSPLUS17
122135
void basics_error_3_cpp17() {
@@ -174,5 +187,6 @@ int main() {
174187
basics_error_2();
175188
basics_error_3();
176189
basics_error_4();
190+
basics_error_5();
177191
return 0;
178192
}

0 commit comments

Comments
 (0)