From efe9761f80e4f76edb06bb526d3f6bf036075f35 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Wed, 19 May 2021 12:23:17 -0400 Subject: [PATCH] Fixing issue 1579. (#1580) --- .../simdjson/generic/ondemand/serialization.h | 22 +++++++++++++++++++ tests/ondemand/ondemand_tostring_tests.cpp | 16 +++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/include/simdjson/generic/ondemand/serialization.h b/include/simdjson/generic/ondemand/serialization.h index df968e03..1e17782e 100644 --- a/include/simdjson/generic/ondemand/serialization.h +++ b/include/simdjson/generic/ondemand/serialization.h @@ -201,4 +201,26 @@ inline simdjson::simdjson_result to_string(simdjson::SIMDJSON_IMPLE return std::string(answer.data(), answer.size()); } +#if SIMDJSON_EXCEPTIONS + +inline std::string to_string(simdjson_result x) { + if (x.error()) { throw simdjson_error(x.error()); } + return to_string(x.value()); +} + +inline std::string to_string(simdjson_result x) { + if (x.error()) { throw simdjson_error(x.error()); } + return to_string(x.value()); +} + +inline std::string to_string(simdjson_result x) { + if (x.error()) { throw simdjson_error(x.error()); } + return to_string(x.value()); +} + +inline std::string to_string(simdjson_result x) { + if (x.error()) { throw simdjson_error(x.error()); } + return to_string(x.value()); +} +#endif } // namespace simdjson diff --git a/tests/ondemand/ondemand_tostring_tests.cpp b/tests/ondemand/ondemand_tostring_tests.cpp index 16c33abb..b4d82b8c 100644 --- a/tests/ondemand/ondemand_tostring_tests.cpp +++ b/tests/ondemand/ondemand_tostring_tests.cpp @@ -13,6 +13,8 @@ #include #include "simdjson.h" +using namespace simdjson; + #include "test_ondemand.h" namespace tostring_tests { const char *test_files[] = { @@ -22,6 +24,16 @@ const char *test_files[] = { #if SIMDJSON_EXCEPTIONS +bool minify_demo() { + TEST_START(); + ondemand::parser parser; + auto cars_json = R"( { "test": "result" } )"_padded; + ondemand::document doc; + ASSERT_SUCCESS( parser.iterate(cars_json).get(doc) ); + std::cout << simdjson::to_string(doc["test"]) << std::endl; + TEST_SUCCEED(); +} + /** * The general idea of these tests if that if you take a JSON file, * load it, then convert it into a string, then parse that, and @@ -73,8 +85,9 @@ bool minify_test() { return false; } } - return true; + TEST_SUCCEED(); } + #endif // SIMDJSON_EXCEPTIONS bool load_to_string_exceptionless(const char *filename) { @@ -136,6 +149,7 @@ bool minify_exceptionless_test() { bool run() { return #if SIMDJSON_EXCEPTIONS + minify_demo() && minify_test() && #endif // SIMDJSON_EXCEPTIONS minify_exceptionless_test() &&