Another example. (#790)
* Another example. * Adding a reference to error chaining.
This commit is contained in:
parent
c5684a6278
commit
f397b6fedf
|
@ -145,6 +145,18 @@ for (dom::object obj : parser.parse(abstract_json)) {
|
|||
}
|
||||
```
|
||||
|
||||
And another one:
|
||||
|
||||
|
||||
```C++
|
||||
auto abstract_json = R"(
|
||||
{ "str" : { "123" : {"abc" : 3.14 } } } )"_padded;
|
||||
dom::parser parser;
|
||||
double v = parser.parse(abstract_json)["str"]["123"]["abc"].get<double>();
|
||||
cout << "number: " << v << endl;
|
||||
```
|
||||
|
||||
|
||||
C++17 Support
|
||||
-------------
|
||||
|
||||
|
@ -341,6 +353,21 @@ for (dom::element elem : rootarray) {
|
|||
|
||||
```
|
||||
|
||||
And another one:
|
||||
|
||||
```C++
|
||||
auto abstract_json = R"(
|
||||
{ "str" : { "123" : {"abc" : 3.14 } } } )"_padded;
|
||||
dom::parser parser;
|
||||
double v;
|
||||
simdjson::error_code error;
|
||||
parser.parse(abstract_json)["str"]["123"]["abc"].get<double>().tie(v, error);
|
||||
if (error) { cerr << error << endl; exit(1); }
|
||||
cout << "number: " << v << endl;
|
||||
```
|
||||
|
||||
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*.
|
||||
|
||||
### Exceptions
|
||||
|
||||
Users more comfortable with an exception flow may choose to directly cast the `simdjson_result<T>` to the desired type:
|
||||
|
|
|
@ -84,6 +84,13 @@ void basics_dom_3() {
|
|||
}
|
||||
}
|
||||
|
||||
void basics_dom_4() {
|
||||
auto abstract_json = R"(
|
||||
{ "str" : { "123" : {"abc" : 3.14 } } } )"_padded;
|
||||
dom::parser parser;
|
||||
double v = parser.parse(abstract_json)["str"]["123"]["abc"].get<double>();
|
||||
cout << "number: " << v << endl;
|
||||
}
|
||||
|
||||
|
||||
namespace treewalk_1 {
|
||||
|
@ -236,5 +243,6 @@ int main() {
|
|||
basics_dom_1();
|
||||
basics_dom_2();
|
||||
basics_dom_3();
|
||||
basics_dom_4();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -77,6 +77,8 @@ void basics_error_3() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void basics_error_4() {
|
||||
auto abstract_json = R"( [
|
||||
{ "12345" : {"a":12.34, "b":56.78, "c": 9998877} },
|
||||
|
@ -117,6 +119,17 @@ void basics_error_4() {
|
|||
}
|
||||
}
|
||||
|
||||
void basics_error_5() {
|
||||
auto abstract_json = R"(
|
||||
{ "str" : { "123" : {"abc" : 3.14 } } } )"_padded;
|
||||
dom::parser parser;
|
||||
double v;
|
||||
simdjson::error_code error;
|
||||
parser.parse(abstract_json)["str"]["123"]["abc"].get<double>().tie(v, error);
|
||||
if (error) { cerr << error << endl; exit(1); }
|
||||
cout << "number: " << v << endl;
|
||||
}
|
||||
|
||||
|
||||
#ifdef SIMDJSON_CPLUSPLUS17
|
||||
void basics_error_3_cpp17() {
|
||||
|
@ -174,5 +187,6 @@ int main() {
|
|||
basics_error_2();
|
||||
basics_error_3();
|
||||
basics_error_4();
|
||||
basics_error_5();
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue