From 954d6c326daba18b3a3a4a0a6747cd61a79c4812 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 15 Jun 2020 17:45:15 -0400 Subject: [PATCH 1/5] New examples. --- doc/basics.md | 38 +++++++++++++++++++--- tests/readme_examples_noexceptions.cpp | 44 +++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/doc/basics.md b/doc/basics.md index c56eb6a7..9c37d884 100644 --- a/doc/basics.md +++ b/doc/basics.md @@ -39,9 +39,9 @@ You can compile with: c++ myproject.cpp simdjson.cpp ``` -Note: +Note: - 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`). -- 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`. +- 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`. 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 * **Array Index:** To get at an array value by index, use the at() method: `array.at(0)` gets the first element. > Note that array[0] does not compile, because implementing [] gives the impression indexing is a - > O(1) operation, which it is not presently in simdjson. Instead, you should iterate over the elements - > using a for-loop, as in our examples. + > O(1) operation, which it is not presently in simdjson. Instead, you should iterate over the elements + > using a for-loop, as in our examples. * **Array and Object size** Given an array or an object, you can get its size (number of elements or keys) with the `size()` method. * **Checking an Element Type:** You can check an element's type with `element.type()`. It @@ -399,6 +399,36 @@ And another one: Notice how we can string several operation (`parser.parse(abstract_json)["str"]["123"]["abc"].get()`) 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 ParseDouble(const char *j, double &d) { + simdjson::error_code error; + parser.parse(j, std::strlen(j)).at(0).get().tie(d, error); + if (error) { + return false; + } + return true; +} + +bool ParseString(const char *j, std::string &s) { + simdjson::error_code error; + std::string_view answer; + parser.parse(j,strlen(j)) + .at(0) + .get() + .tie(answer, error); + if (error) { + return false; + } + s = answer; + return true; +} +``` + ### Exceptions Users more comfortable with an exception flow may choose to directly cast the `simdjson_result` to the desired type: diff --git a/tests/readme_examples_noexceptions.cpp b/tests/readme_examples_noexceptions.cpp index 5c5372aa..92e91f75 100644 --- a/tests/readme_examples_noexceptions.cpp +++ b/tests/readme_examples_noexceptions.cpp @@ -131,6 +131,8 @@ void basics_error_5() { } + + #ifdef SIMDJSON_CPLUSPLUS17 void basics_error_3_cpp17() { auto cars_json = R"( [ @@ -183,10 +185,50 @@ void basics_error_3_cpp17() { } #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().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() + .tie(answer, error); + if (error) { + return false; + } + s = answer; + return true; +} + + 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_3(); basics_error_4(); basics_error_5(); - return 0; + return EXIT_SUCCESS; } From 27a75a9085107a0bf942804bec214095e5a70aab Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 15 Jun 2020 17:54:34 -0400 Subject: [PATCH 2/5] Tweaking. --- doc/basics.md | 2 +- tests/readme_examples_noexceptions.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/basics.md b/doc/basics.md index 9c37d884..f36c100f 100644 --- a/doc/basics.md +++ b/doc/basics.md @@ -424,7 +424,7 @@ bool ParseString(const char *j, std::string &s) { if (error) { return false; } - s = answer; + s.assign(answer.data(), answer.size()); return true; } ``` diff --git a/tests/readme_examples_noexceptions.cpp b/tests/readme_examples_noexceptions.cpp index 92e91f75..4a5211f4 100644 --- a/tests/readme_examples_noexceptions.cpp +++ b/tests/readme_examples_noexceptions.cpp @@ -208,7 +208,7 @@ bool ParseString(const char *j, std::string &s) { if (error) { return false; } - s = answer; + s.assign(answer.data(), answer.size()); return true; } From 4474f8ef18e49ee46cde3904157faa3c09886039 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Wed, 17 Jun 2020 16:24:55 +0000 Subject: [PATCH 3/5] Cleaning a bit the examples. --- doc/basics.md | 28 +++++++++++--------------- tests/readme_examples_noexceptions.cpp | 28 +++++++++++--------------- 2 files changed, 24 insertions(+), 32 deletions(-) diff --git a/doc/basics.md b/doc/basics.md index f36c100f..184b92e2 100644 --- a/doc/basics.md +++ b/doc/basics.md @@ -406,26 +406,22 @@ The next two functions will take as input a JSON document containing an array wi simdjson::dom::parser parser{}; bool ParseDouble(const char *j, double &d) { - simdjson::error_code error; - parser.parse(j, std::strlen(j)).at(0).get().tie(d, error); - if (error) { - return false; - } - return true; + simdjson::error_code error; + parser.parse(j, std::strlen(j)) + .at(0) + .get() + .tie(d, error); + if (error) { return false; } + return true; } bool ParseString(const char *j, std::string &s) { - simdjson::error_code error; - std::string_view answer; - parser.parse(j,strlen(j)) + auto [answer, error] = parser.parse(j,strlen(j)) .at(0) - .get() - .tie(answer, error); - if (error) { - return false; - } - s.assign(answer.data(), answer.size()); - return true; + .get(); + if (error) { return false; } + s.assign(answer.data(), answer.size()); + return true; } ``` diff --git a/tests/readme_examples_noexceptions.cpp b/tests/readme_examples_noexceptions.cpp index 4a5211f4..1aabea22 100644 --- a/tests/readme_examples_noexceptions.cpp +++ b/tests/readme_examples_noexceptions.cpp @@ -189,27 +189,23 @@ 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().tie(d, error); - if (error) { - return false; - } - return true; + simdjson::error_code error; + parser.parse(j, std::strlen(j)) + .at(0) + .get() + .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)) + auto [answer, error] = parser.parse(j,strlen(j)) .at(0) - .get() - .tie(answer, error); - if (error) { - return false; - } - s.assign(answer.data(), answer.size()); - return true; + .get(); + if (error) { return false; } + s.assign(answer.data(), answer.size()); + return true; } From 0655a135e691a63a20a5f0a77a05ff391c791ace Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Wed, 17 Jun 2020 17:52:07 +0000 Subject: [PATCH 4/5] Reverting. --- tests/readme_examples_noexceptions.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/readme_examples_noexceptions.cpp b/tests/readme_examples_noexceptions.cpp index 1aabea22..d669f6ef 100644 --- a/tests/readme_examples_noexceptions.cpp +++ b/tests/readme_examples_noexceptions.cpp @@ -199,6 +199,7 @@ bool ParseDouble(const char *j, double &d) { } // See https://github.com/miloyip/nativejson-benchmark/blob/master/src/tests/simdjsontest.cpp +#ifdef SIMDJSON_CPLUSPLUS17 bool ParseString(const char *j, std::string &s) { auto [answer, error] = parser.parse(j,strlen(j)) .at(0) @@ -207,6 +208,19 @@ bool ParseString(const char *j, std::string &s) { s.assign(answer.data(), answer.size()); return true; } +#else +bool ParseString(const char *j, std::string &s) { + simdjson::error_code error; + std::string_view answer; + parser.parse(j,strlen(j)) + .at(0) + .get() + .tie(answer, error); + if (error) { return false; } + s.assign(answer.data(), answer.size()); + return true; +} +#endif int main() { From f632e7c0435e91e165d699887d0196569399261c Mon Sep 17 00:00:00 2001 From: John Keiser Date: Thu, 18 Jun 2020 12:50:49 -0700 Subject: [PATCH 5/5] Put C++11 capable version back, change name to readme style --- doc/basics.md | 11 +++++++---- tests/readme_examples_noexceptions.cpp | 11 ----------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/doc/basics.md b/doc/basics.md index 184b92e2..f3457b5a 100644 --- a/doc/basics.md +++ b/doc/basics.md @@ -405,7 +405,7 @@ The next two functions will take as input a JSON document containing an array wi ```C++ simdjson::dom::parser parser{}; -bool ParseDouble(const char *j, double &d) { +bool parse_double(const char *j, double &d) { simdjson::error_code error; parser.parse(j, std::strlen(j)) .at(0) @@ -415,10 +415,13 @@ bool ParseDouble(const char *j, double &d) { return true; } -bool ParseString(const char *j, std::string &s) { - auto [answer, error] = parser.parse(j,strlen(j)) +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(); + .get() + .tie(answer, error); if (error) { return false; } s.assign(answer.data(), answer.size()); return true; diff --git a/tests/readme_examples_noexceptions.cpp b/tests/readme_examples_noexceptions.cpp index d669f6ef..0f3ba442 100644 --- a/tests/readme_examples_noexceptions.cpp +++ b/tests/readme_examples_noexceptions.cpp @@ -199,16 +199,6 @@ bool ParseDouble(const char *j, double &d) { } // See https://github.com/miloyip/nativejson-benchmark/blob/master/src/tests/simdjsontest.cpp -#ifdef SIMDJSON_CPLUSPLUS17 -bool ParseString(const char *j, std::string &s) { - auto [answer, error] = parser.parse(j,strlen(j)) - .at(0) - .get(); - if (error) { return false; } - s.assign(answer.data(), answer.size()); - return true; -} -#else bool ParseString(const char *j, std::string &s) { simdjson::error_code error; std::string_view answer; @@ -220,7 +210,6 @@ bool ParseString(const char *j, std::string &s) { s.assign(answer.data(), answer.size()); return true; } -#endif int main() {