Merge pull request #936 from simdjson/dlemire/new_examples
New examples.
This commit is contained in:
commit
5ccdbef7d5
|
@ -399,6 +399,35 @@ And another one:
|
||||||
|
|
||||||
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*.
|
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*.
|
||||||
|
|
||||||
|
|
||||||
|
The next two functions will take as input a JSON document containing an array with a single element, either a string or a number. They return true upon success.
|
||||||
|
|
||||||
|
```C++
|
||||||
|
simdjson::dom::parser parser{};
|
||||||
|
|
||||||
|
bool parse_double(const char *j, double &d) {
|
||||||
|
simdjson::error_code error;
|
||||||
|
parser.parse(j, std::strlen(j))
|
||||||
|
.at(0)
|
||||||
|
.get<double>()
|
||||||
|
.tie(d, error);
|
||||||
|
if (error) { return false; }
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool parse_string(const char *j, std::string &s) {
|
||||||
|
simdjson::error_code error;
|
||||||
|
std::string_view answer;
|
||||||
|
parser.parse(j,strlen(j))
|
||||||
|
.at(0)
|
||||||
|
.get<std::string_view>()
|
||||||
|
.tie(answer, error);
|
||||||
|
if (error) { return false; }
|
||||||
|
s.assign(answer.data(), answer.size());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Exceptions
|
### Exceptions
|
||||||
|
|
||||||
Users more comfortable with an exception flow may choose to directly cast the `simdjson_result<T>` to the desired type:
|
Users more comfortable with an exception flow may choose to directly cast the `simdjson_result<T>` to the desired type:
|
||||||
|
|
|
@ -131,6 +131,8 @@ void basics_error_5() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef SIMDJSON_CPLUSPLUS17
|
#ifdef SIMDJSON_CPLUSPLUS17
|
||||||
void basics_error_3_cpp17() {
|
void basics_error_3_cpp17() {
|
||||||
auto cars_json = R"( [
|
auto cars_json = R"( [
|
||||||
|
@ -183,10 +185,49 @@ void basics_error_3_cpp17() {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
simdjson::dom::parser parser;
|
||||||
|
|
||||||
|
// See https://github.com/miloyip/nativejson-benchmark/blob/master/src/tests/simdjsontest.cpp
|
||||||
|
bool ParseDouble(const char *j, double &d) {
|
||||||
|
simdjson::error_code error;
|
||||||
|
parser.parse(j, std::strlen(j))
|
||||||
|
.at(0)
|
||||||
|
.get<double>()
|
||||||
|
.tie(d, error);
|
||||||
|
if (error) { return false; }
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// See https://github.com/miloyip/nativejson-benchmark/blob/master/src/tests/simdjsontest.cpp
|
||||||
|
bool ParseString(const char *j, std::string &s) {
|
||||||
|
simdjson::error_code error;
|
||||||
|
std::string_view answer;
|
||||||
|
parser.parse(j,strlen(j))
|
||||||
|
.at(0)
|
||||||
|
.get<std::string_view>()
|
||||||
|
.tie(answer, error);
|
||||||
|
if (error) { return false; }
|
||||||
|
s.assign(answer.data(), answer.size());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
double x{};
|
||||||
|
ParseDouble("[1.1]",x);
|
||||||
|
if(x != 1.1) {
|
||||||
|
std::cerr << "bug in ParseDouble!" << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
std::string s{};
|
||||||
|
ParseString("[\"my string\"]", s);
|
||||||
|
if(s != "my string") {
|
||||||
|
std::cerr << "bug in ParseString!" << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
basics_error_2();
|
basics_error_2();
|
||||||
basics_error_3();
|
basics_error_3();
|
||||||
basics_error_4();
|
basics_error_4();
|
||||||
basics_error_5();
|
basics_error_5();
|
||||||
return 0;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue