Skip to content

Commit 5ccdbef

Browse files
authored
Merge pull request simdjson#936 from simdjson/dlemire/new_examples
New examples.
2 parents c13c265 + f632e7c commit 5ccdbef

File tree

2 files changed

+75
-5
lines changed

2 files changed

+75
-5
lines changed

doc/basics.md

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ You can compile with:
3939
c++ myproject.cpp simdjson.cpp
4040
```
4141

42-
Note:
42+
Note:
4343
- Users on macOS and other platforms were default compilers do not provide C++11 compliant by default should request it with the appropriate flag (e.g., `c++ myproject.cpp simdjson.cpp`).
44-
- Visual Studio users should compile with the `_CRT_SECURE_NO_WARNINGS` flag to avoid warnings with respect to our use of standard C functions such as `fopen`.
44+
- Visual Studio users should compile with the `_CRT_SECURE_NO_WARNINGS` flag to avoid warnings with respect to our use of standard C functions such as `fopen`.
4545

4646

4747
The Basics: Loading and Parsing JSON Documents
@@ -95,8 +95,8 @@ Once you have an element, you can navigate it with idiomatic C++ iterators, oper
9595
* **Array Index:** To get at an array value by index, use the at() method: `array.at(0)` gets the
9696
first element.
9797
> Note that array[0] does not compile, because implementing [] gives the impression indexing is a
98-
> O(1) operation, which it is not presently in simdjson. Instead, you should iterate over the elements
99-
> using a for-loop, as in our examples.
98+
> O(1) operation, which it is not presently in simdjson. Instead, you should iterate over the elements
99+
> using a for-loop, as in our examples.
100100
* **Array and Object size** Given an array or an object, you can get its size (number of elements or keys)
101101
with the `size()` method.
102102
* **Checking an Element Type:** You can check an element's type with `element.type()`. It
@@ -399,6 +399,35 @@ And another one:
399399
400400
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*.
401401
402+
403+
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.
404+
405+
```C++
406+
simdjson::dom::parser parser{};
407+
408+
bool parse_double(const char *j, double &d) {
409+
simdjson::error_code error;
410+
parser.parse(j, std::strlen(j))
411+
.at(0)
412+
.get<double>()
413+
.tie(d, error);
414+
if (error) { return false; }
415+
return true;
416+
}
417+
418+
bool parse_string(const char *j, std::string &s) {
419+
simdjson::error_code error;
420+
std::string_view answer;
421+
parser.parse(j,strlen(j))
422+
.at(0)
423+
.get<std::string_view>()
424+
.tie(answer, error);
425+
if (error) { return false; }
426+
s.assign(answer.data(), answer.size());
427+
return true;
428+
}
429+
```
430+
402431
### Exceptions
403432

404433
Users more comfortable with an exception flow may choose to directly cast the `simdjson_result<T>` to the desired type:

tests/readme_examples_noexceptions.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ void basics_error_5() {
131131
}
132132

133133

134+
135+
134136
#ifdef SIMDJSON_CPLUSPLUS17
135137
void basics_error_3_cpp17() {
136138
auto cars_json = R"( [
@@ -183,10 +185,49 @@ void basics_error_3_cpp17() {
183185
}
184186
#endif
185187

188+
simdjson::dom::parser parser;
189+
190+
// See https://github.com/miloyip/nativejson-benchmark/blob/master/src/tests/simdjsontest.cpp
191+
bool ParseDouble(const char *j, double &d) {
192+
simdjson::error_code error;
193+
parser.parse(j, std::strlen(j))
194+
.at(0)
195+
.get<double>()
196+
.tie(d, error);
197+
if (error) { return false; }
198+
return true;
199+
}
200+
201+
// See https://github.com/miloyip/nativejson-benchmark/blob/master/src/tests/simdjsontest.cpp
202+
bool ParseString(const char *j, std::string &s) {
203+
simdjson::error_code error;
204+
std::string_view answer;
205+
parser.parse(j,strlen(j))
206+
.at(0)
207+
.get<std::string_view>()
208+
.tie(answer, error);
209+
if (error) { return false; }
210+
s.assign(answer.data(), answer.size());
211+
return true;
212+
}
213+
214+
186215
int main() {
216+
double x{};
217+
ParseDouble("[1.1]",x);
218+
if(x != 1.1) {
219+
std::cerr << "bug in ParseDouble!" << std::endl;
220+
return EXIT_FAILURE;
221+
}
222+
std::string s{};
223+
ParseString("[\"my string\"]", s);
224+
if(s != "my string") {
225+
std::cerr << "bug in ParseString!" << std::endl;
226+
return EXIT_FAILURE;
227+
}
187228
basics_error_2();
188229
basics_error_3();
189230
basics_error_4();
190231
basics_error_5();
191-
return 0;
232+
return EXIT_SUCCESS;
192233
}

0 commit comments

Comments
 (0)