Minor tweak.

This commit is contained in:
Daniel Lemire 2021-07-27 10:56:05 -04:00
parent e681234e45
commit c6ef2105ab
2 changed files with 8 additions and 4 deletions

View File

@ -64,7 +64,7 @@ Once you have an element, you can navigate it with idiomatic C++ iterators, oper
* **Extracting Values (with exceptions):** You can cast a JSON element to a native type: `double(element)` or * **Extracting Values (with exceptions):** You can cast a JSON element to a native type: `double(element)` or
`double x = json_element`. This works for double, uint64_t, int64_t, bool, `double x = json_element`. This works for double, uint64_t, int64_t, bool,
dom::object and dom::array. An exception is thrown if the cast is not possible. dom::object and dom::array. An exception (`simdjson::simdjson_error`) is thrown if the cast is not possible.
* **Extracting Values (without exceptions):** You can use a variant usage of `get()` with error codes to avoid exceptions. You first declare the variable of the appropriate type (`double`, `uint64_t`, `int64_t`, `bool`, * **Extracting Values (without exceptions):** You can use a variant usage of `get()` with error codes to avoid exceptions. You first declare the variable of the appropriate type (`double`, `uint64_t`, `int64_t`, `bool`,
`dom::object` and `dom::array`) and pass it by reference to `get()` which gives you back an error code: e.g., `dom::object` and `dom::array`) and pass it by reference to `get()` which gives you back an error code: e.g.,
```c++ ```c++

View File

@ -246,11 +246,15 @@ bool using_the_parsed_json_4() {
// Parse and iterate through an array of objects // Parse and iterate through an array of objects
for (ondemand::object points : parser.iterate(points_json)) { for (ondemand::object points : parser.iterate(points_json)) {
// Iterating through an object, you iterate through key-value pairs (a 'field').
for (auto point : points) { for (auto point : points) {
// Get the key corresponding the the field 'point'.
cout << "id: " << std::string_view(point.unescaped_key()) << ": ("; cout << "id: " << std::string_view(point.unescaped_key()) << ": (";
cout << point.value()["x"].get_double() << ", "; // Get the value corresponding the the field 'point'.
cout << point.value()["y"].get_double() << ", "; ondemand::object xyz = point.value();
cout << point.value()["z"].get_int64() << endl; cout << xyz["x"].get_double() << ", ";
cout << xyz["y"].get_double() << ", ";
cout << xyz["z"].get_int64() << endl;
} }
} }