From 293cd6b7940cc7ceab3ad3617fb9f47b3988826a Mon Sep 17 00:00:00 2001 From: Macr0Nerd Date: Thu, 25 Apr 2019 22:12:15 -0500 Subject: [PATCH 1/2] Added to_string and added basic tests --- include/nlohmann/json.hpp | 14 ++++++++++++++ single_include/nlohmann/json.hpp | 14 ++++++++++++++ test/src/unit-serialization.cpp | 15 +++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 017ce929..43d7eac4 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -7965,6 +7965,20 @@ class basic_json /// @} }; + +/*! +@brief user-defined to_string function for JSON values + +This function implements a user-defined to_string for JSON objects. + +@param[in] j a JSON object +@return a std::string object +*/ + +template +std::string to_string(const T& j){ + return j.dump(); +} } // namespace nlohmann /////////////////////// diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 1f82da44..c13473e6 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20766,6 +20766,20 @@ class basic_json /// @} }; + +/*! +@brief user-defined to_string function for JSON values + +This function implements a user-defined to_string for JSON objects. + +@param[in] j a JSON object +@return a std::string object +*/ + +template +std::string to_string(const T& j){ + return j.dump(); +} } // namespace nlohmann /////////////////////// diff --git a/test/src/unit-serialization.cpp b/test/src/unit-serialization.cpp index 785373f0..7df2bf9c 100644 --- a/test/src/unit-serialization.cpp +++ b/test/src/unit-serialization.cpp @@ -173,4 +173,19 @@ TEST_CASE("serialization") test("\xE1\x80\xE2\xF0\x91\x92\xF1\xBF\x41", "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\x41"); } } + + SECTION("to_string") + { + auto test = [&](std::string const & input, std::string const & expected) + { + using std::to_string; + json j = input; + CHECK(to_string(j) == "\"" + expected + "\""); + }; + + test("{\"x\":5,\"y\":6}", "{\\\"x\\\":5,\\\"y\\\":6}"); + test("{\"x\":[10,null,null,null]}", "{\\\"x\\\":[10,null,null,null]}"); + test("test", "test"); + test("[3,\"false\",false]", "[3,\\\"false\\\",false]"); + } } From aa4c45ee4d3842452843a1ea728049b745751dc1 Mon Sep 17 00:00:00 2001 From: Macr0Nerd Date: Thu, 25 Apr 2019 22:12:15 -0500 Subject: [PATCH 2/2] Added to_string (with ugly macro) and tests --- include/nlohmann/json.hpp | 15 +++++++++++++++ single_include/nlohmann/json.hpp | 15 +++++++++++++++ test/src/unit-serialization.cpp | 15 +++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index 017ce929..daaf2d66 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -7965,6 +7965,21 @@ class basic_json /// @} }; + +/*! +@brief user-defined to_string function for JSON values + +This function implements a user-defined to_string for JSON objects. + +@param[in] j a JSON object +@return a std::string object +*/ + +NLOHMANN_BASIC_JSON_TPL_DECLARATION +std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j) +{ + return j.dump(); +} } // namespace nlohmann /////////////////////// diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 1f82da44..08ac166b 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -20766,6 +20766,21 @@ class basic_json /// @} }; + +/*! +@brief user-defined to_string function for JSON values + +This function implements a user-defined to_string for JSON objects. + +@param[in] j a JSON object +@return a std::string object +*/ + +NLOHMANN_BASIC_JSON_TPL_DECLARATION +std::string to_string(const NLOHMANN_BASIC_JSON_TPL& j) +{ + return j.dump(); +} } // namespace nlohmann /////////////////////// diff --git a/test/src/unit-serialization.cpp b/test/src/unit-serialization.cpp index 785373f0..7df2bf9c 100644 --- a/test/src/unit-serialization.cpp +++ b/test/src/unit-serialization.cpp @@ -173,4 +173,19 @@ TEST_CASE("serialization") test("\xE1\x80\xE2\xF0\x91\x92\xF1\xBF\x41", "\\ufffd" "\\ufffd" "\\ufffd" "\\ufffd" "\x41"); } } + + SECTION("to_string") + { + auto test = [&](std::string const & input, std::string const & expected) + { + using std::to_string; + json j = input; + CHECK(to_string(j) == "\"" + expected + "\""); + }; + + test("{\"x\":5,\"y\":6}", "{\\\"x\\\":5,\\\"y\\\":6}"); + test("{\"x\":[10,null,null,null]}", "{\\\"x\\\":[10,null,null,null]}"); + test("test", "test"); + test("[3,\"false\",false]", "[3,\\\"false\\\",false]"); + } }