Skip to content

Commit 3c6ef83

Browse files
authored
Trying to correct the documentation so that it actually describes how the code behaves. (Attempt two) (simdjson#712)
* Trying to correct the documentation so that it actually describes how the code behaves. * tweaking the wording. * Improving. * Removing confusing sentence. * Fixing formatting. * Now with working example, tested. * Added a smaller piece of code
1 parent b9ac0a7 commit 3c6ef83

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

doc/basics.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,17 @@ Once you have an element, you can navigate it with idiomatic C++ iterators, oper
5959

6060
* **Extracting Values:** You can cast a JSON element to a native type: `double(element)` or
6161
`double x = json_element`. This works for double, uint64_t, int64_t, bool,
62-
dom::object and dom::array. You can also use is_*typename*()` to test if it is a
63-
given type, and as_*typename*() to do the cast and return an error code on failure instead of an
64-
exception.
62+
dom::object and dom::array. An exception is thrown if the cast is not possible. You can also use is<*typename*>() to test if it is a
63+
given type, or use the `type()` method: e.g., `element.type() == dom::element_type::DOUBLE`. Instead of casting, you can use get<*typename*>() to get the value: casts and get<*typename*>() can be used interchangeably. You can use a variant usage of get<*typename*>() with error codes to avoid exceptions: e.g.,
64+
```c++
65+
simdjson::error_code error;
66+
double value; // variable where we store the value to be parsed
67+
simdjson::padded_string numberstring = "1.2"_padded; // our JSON input ("1.2")
68+
simdjson::dom::parser parser;
69+
parser.parse(numberstring).get<double>().tie(value,error);
70+
if (error) { std::cerr << error << std::endl; return EXIT_FAILURE; }
71+
std::cout << "I parsed " << value << " from " << numberstring.data() << std::endl;
72+
```
6573
* **Field Access:** To get the value of the "foo" field in an object, use `object["foo"]`.
6674
* **Array Iteration:** To iterate through an array, use `for (auto value : array) { ... }`. If you
6775
know the type of the value, you can cast it right there, too! `for (double value : array) { ... }`
@@ -173,7 +181,7 @@ behavior.
173181
> use it. If your project treats aliased, this means you can't use the same names in `auto [x, error]`
174182
> without triggering warnings or error (and particularly can't use the word "error" every time). To
175183
> circumvent this, you can use this instead:
176-
>
184+
>
177185
> ```c++
178186
> dom::element doc;
179187
> simdjson::error_code error;
@@ -343,4 +351,3 @@ The parsed results (`dom::document`, `dom::element`, `array`, `object`) depend o
343351

344352
The CPU detection, which runs the first time parsing is attempted and switches to the fastest
345353
parser for your CPU, is transparent and thread-safe.
346-

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ add_cpp_test(integer_tests integer_tests.cpp)
2626
add_cpp_test(jsoncheck jsoncheck.cpp)
2727
add_cpp_test(parse_many_test parse_many_test.cpp)
2828
add_cpp_test(pointercheck pointercheck.cpp quicktests)
29+
add_cpp_test(extracting_values_example extracting_values_example.cpp quicktests)
2930

3031
set_property(
3132
TEST basictests errortests integer_tests jsoncheck parse_many_test pointercheck
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <iostream>
2+
#include "simdjson.h"
3+
4+
int main() {
5+
simdjson::error_code error;
6+
double value; // variable where we store the value to be parsed
7+
simdjson::padded_string numberstring = "1.2"_padded; // our JSON input ("1.2")
8+
simdjson::dom::parser parser;
9+
parser.parse(numberstring).get<double>().tie(value,error);
10+
if (error) { std::cerr << error << std::endl; return EXIT_FAILURE; }
11+
std::cout << "I parsed " << value << " from " << numberstring.data() << std::endl;
12+
}

0 commit comments

Comments
 (0)