From df30a0ea07ff9b922dc3ea49047f996446cf38c6 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 23 Nov 2019 00:21:33 +0100 Subject: [PATCH 1/7] :construction: conversions for std::optional --- .../nlohmann/detail/conversions/from_json.hpp | 19 ++++++ .../nlohmann/detail/conversions/to_json.hpp | 20 ++++++ single_include/nlohmann/json.hpp | 39 ++++++++++++ test/src/unit-conversions.cpp | 62 ++++++++++++++++++- 4 files changed, 139 insertions(+), 1 deletion(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index c389dca7ad..34a9c327ed 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -13,6 +13,10 @@ #include // pair, declval #include // valarray +#ifdef JSON_HAS_CPP_17 + #include // optional +#endif + #include #include #include @@ -33,6 +37,21 @@ void from_json(const BasicJsonType& j, typename std::nullptr_t& n) n = nullptr; } +#ifdef JSON_HAS_CPP_17 +template +void from_json(const BasicJsonType& j, std::optional& opt) +{ + if (j.is_null()) + { + opt = std::nullopt; + } + else + { + opt = j.template get(); + } +} +#endif + // overloads for basic_json template parameters template::value and diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index a1def699fd..040ba795f9 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -10,6 +10,10 @@ #include // valarray #include // vector +#ifdef JSON_HAS_CPP_17 + #include // optional +#endif + #include #include #include @@ -198,6 +202,22 @@ struct external_constructor // to_json // ///////////// +#ifdef JSON_HAS_CPP_17 +template::value, int> = 0> +void to_json(BasicJsonType& j, const std::optional& opt) +{ + if (opt.has_value()) + { + j = *opt; + } + else + { + j = nullptr; + } +} +#endif + template::value, int> = 0> void to_json(BasicJsonType& j, T b) noexcept diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index e2a68e6f58..8cfebd0254 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -69,6 +69,10 @@ SOFTWARE. #include // pair, declval #include // valarray +#ifdef JSON_HAS_CPP_17 + #include // optional +#endif + // #include @@ -2904,6 +2908,21 @@ void from_json(const BasicJsonType& j, typename std::nullptr_t& n) n = nullptr; } +#ifdef JSON_HAS_CPP_17 +template +void from_json(const BasicJsonType& j, std::optional& opt) +{ + if (j.is_null()) + { + opt = std::nullopt; + } + else + { + opt = j.template get(); + } +} +#endif + // overloads for basic_json template parameters template::value and @@ -3272,6 +3291,10 @@ constexpr const auto& from_json = detail::static_const::va #include // valarray #include // vector +#ifdef JSON_HAS_CPP_17 + #include // optional +#endif + // #include @@ -3642,6 +3665,22 @@ struct external_constructor // to_json // ///////////// +#ifdef JSON_HAS_CPP_17 +template::value, int> = 0> +void to_json(BasicJsonType& j, const std::optional& opt) +{ + if (opt.has_value()) + { + j = *opt; + } + else + { + j = nullptr; + } +} +#endif + template::value, int> = 0> void to_json(BasicJsonType& j, T b) noexcept diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index 8477cf6392..c0c52f605b 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -50,6 +50,7 @@ using nlohmann::json; #endif #if defined(JSON_HAS_CPP_17) + #include #include #endif @@ -189,7 +190,6 @@ TEST_CASE("value conversion") } } - SECTION("get an object (implicit)") { json::object_t o_reference = {{"object", json::object()}, @@ -1575,3 +1575,63 @@ TEST_CASE("JSON to enum mapping") CHECK(TS_INVALID == json("what?").get()); } } + +#ifdef JSON_HAS_CPP_17 +TEST_CASE("std::optional") +{ + SECTION("null") + { + json j_null; + std::optional opt_null; + + CHECK(json(opt_null) == j_null); + //CHECK(std::optional(j_null) == std::nullopt); + } + + SECTION("string") + { + json j_string = "string"; + std::optional opt_string = "string"; + + CHECK(json(opt_string) == j_string); + CHECK(std::optional(j_string) == opt_string); + } + + SECTION("bool") + { + json j_bool = true; + std::optional opt_bool = true; + + CHECK(json(opt_bool) == j_bool); + CHECK(std::optional(j_bool) == opt_bool); + } + + SECTION("number") + { + json j_number = 1; + std::optional opt_int = 1; + + CHECK(json(opt_int) == j_number); + CHECK(std::optional(j_number) == opt_int); + } + + SECTION("array") + { + json j_array = {1, 2, 3}; + std::optional> opt_array = {{1, 2, 3}}; + + CHECK(json(opt_array) == j_array); + CHECK(std::optional>(j_array) == opt_array); + } + + SECTION("object") + { + json j_object = {{"one", 1}, {"two", 2}}; + std::map m {{"one", 1}, {"two", 2}}; + std::optional> opt_object = m; + + CHECK(json(opt_object) == j_object); + CHECK(std::optional>(j_object) == opt_object); + } +} +#endif From 1359c56137363fa3ca9eb91fa8ba648ab22fa84f Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 23 Nov 2019 13:29:47 +0100 Subject: [PATCH 2/7] :checkered_flag: fix inclusion --- include/nlohmann/detail/conversions/from_json.hpp | 6 +++++- include/nlohmann/detail/conversions/to_json.hpp | 6 +++++- single_include/nlohmann/json.hpp | 12 ++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 34a9c327ed..ca16c71122 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -14,7 +14,11 @@ #include // valarray #ifdef JSON_HAS_CPP_17 - #include // optional + #if __has_include() + #include + #elif __has_include() + #include + #endif #endif #include diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index 040ba795f9..e9a0f6471c 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -11,7 +11,11 @@ #include // vector #ifdef JSON_HAS_CPP_17 - #include // optional + #if __has_include() + #include + #elif __has_include() + #include + #endif #endif #include diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 8cfebd0254..ba2575b621 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -70,7 +70,11 @@ SOFTWARE. #include // valarray #ifdef JSON_HAS_CPP_17 - #include // optional + #if __has_include() + #include + #elif __has_include() + #include + #endif #endif // #include @@ -3292,7 +3296,11 @@ constexpr const auto& from_json = detail::static_const::va #include // vector #ifdef JSON_HAS_CPP_17 - #include // optional + #if __has_include() + #include + #elif __has_include() + #include + #endif #endif // #include From 713038fa278243dfdb244d1faffce60b16fccf50 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 23 Nov 2019 14:40:15 +0100 Subject: [PATCH 3/7] :green_heart: overwork tests --- test/src/unit-conversions.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index c0c52f605b..0cdecdd79e 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -1617,21 +1617,20 @@ TEST_CASE("std::optional") SECTION("array") { - json j_array = {1, 2, 3}; - std::optional> opt_array = {{1, 2, 3}}; + json j_array = {1, 2, nullptr}; + std::vector> opt_array = {{1, 2, std::nullopt}}; CHECK(json(opt_array) == j_array); - CHECK(std::optional>(j_array) == opt_array); + CHECK(std::vector>(j_array) == opt_array); } SECTION("object") { - json j_object = {{"one", 1}, {"two", 2}}; - std::map m {{"one", 1}, {"two", 2}}; - std::optional> opt_object = m; + json j_object = {{"one", 1}, {"two", 2}, {"zero", nullptr}}; + std::map> opt_object {{"one", 1}, {"two", 2}, {"zero", std::nullopt}}; CHECK(json(opt_object) == j_object); - CHECK(std::optional>(j_object) == opt_object); + CHECK(std::map>(j_object) == opt_object); } } #endif From 1b846c9d8191a50c30d63f88a4ae597419ae5735 Mon Sep 17 00:00:00 2001 From: Markus Palonen Date: Tue, 17 Dec 2019 16:25:35 +0200 Subject: [PATCH 4/7] Use JSON_HAS_CPP_17 only after it has been defined --- .../nlohmann/detail/conversions/from_json.hpp | 8 ------- .../nlohmann/detail/conversions/to_json.hpp | 8 ------- include/nlohmann/detail/macro_scope.hpp | 8 +++++++ single_include/nlohmann/json.hpp | 24 +++++++------------ 4 files changed, 16 insertions(+), 32 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index ca16c71122..28b10486ae 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -13,14 +13,6 @@ #include // pair, declval #include // valarray -#ifdef JSON_HAS_CPP_17 - #if __has_include() - #include - #elif __has_include() - #include - #endif -#endif - #include #include #include diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index e9a0f6471c..4666833635 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -10,14 +10,6 @@ #include // valarray #include // vector -#ifdef JSON_HAS_CPP_17 - #if __has_include() - #include - #elif __has_include() - #include - #endif -#endif - #include #include #include diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 27dddc6be7..16200502bf 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -27,6 +27,14 @@ #define JSON_HAS_CPP_14 #endif +#ifdef JSON_HAS_CPP_17 + #if __has_include() + #include + #elif __has_include() + #include + #endif +#endif + // disable float-equal warnings on GCC/clang #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic push diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ba2575b621..4cd8ed9fcb 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -69,14 +69,6 @@ SOFTWARE. #include // pair, declval #include // valarray -#ifdef JSON_HAS_CPP_17 - #if __has_include() - #include - #elif __has_include() - #include - #endif -#endif - // #include @@ -1739,6 +1731,14 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_HAS_CPP_14 #endif +#ifdef JSON_HAS_CPP_17 + #if __has_include() + #include + #elif __has_include() + #include + #endif +#endif + // disable float-equal warnings on GCC/clang #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic push @@ -3295,14 +3295,6 @@ constexpr const auto& from_json = detail::static_const::va #include // valarray #include // vector -#ifdef JSON_HAS_CPP_17 - #if __has_include() - #include - #elif __has_include() - #include - #endif -#endif - // #include From 6b31375c7c489f4a9ba9910c0eda281e20e9fbce Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Thu, 19 Dec 2019 12:25:27 +0100 Subject: [PATCH 5/7] :alembic: set CXXFLAGS --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 76e1e5aba6..987538aaa0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -329,6 +329,7 @@ script: # make sure CXX is correctly set - if [[ "${COMPILER}" != "" ]]; then export CXX=${COMPILER}; fi + - if [[ "${CXXFLAGS}" != "" ]]; then export CXXFLAGS=${CXXFLAGS}; fi # by default, use the single-header version - if [[ "${MULTIPLE_HEADERS}" == "" ]]; then export MULTIPLE_HEADERS=OFF; fi From 007c6c4bc537042d117298edc8c4008d1da1dec0 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 16 May 2020 19:37:18 +0200 Subject: [PATCH 6/7] :white_check_mark: update tests --- test/src/unit-conversions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index 0cdecdd79e..1850126214 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -1585,7 +1585,7 @@ TEST_CASE("std::optional") std::optional opt_null; CHECK(json(opt_null) == j_null); - //CHECK(std::optional(j_null) == std::nullopt); + CHECK(std::optional(j_null) == std::nullopt); } SECTION("string") From f25bf312fc4ee49f1bc38ef1ccd27b499261fc57 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 17 May 2020 13:58:09 +0200 Subject: [PATCH 7/7] :checkered_flag: include right header --- test/src/unit-conversions.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/src/unit-conversions.cpp b/test/src/unit-conversions.cpp index 1850126214..624b3898d4 100644 --- a/test/src/unit-conversions.cpp +++ b/test/src/unit-conversions.cpp @@ -49,8 +49,15 @@ using nlohmann::json; #define JSON_HAS_CPP_14 #endif +#ifdef JSON_HAS_CPP_17 + #if __has_include() + #include + #elif __has_include() + #include + #endif +#endif + #if defined(JSON_HAS_CPP_17) - #include #include #endif