From a112a3a600f146bc3f8b3f5538f5dcb7f0560f47 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 23 Nov 2019 00:21:33 +0100 Subject: [PATCH 01/16] :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 ++++++++++ tests/src/unit-conversions.cpp | 78 +++++++++++++++++++ 4 files changed, 156 insertions(+) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index aa2f0cbf4c..95d2ad528b 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -20,6 +20,10 @@ #include // pair, declval #include // valarray +#ifdef JSON_HAS_CPP_17 + #include // optional +#endif + #include #include #include @@ -43,6 +47,21 @@ inline 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 < typename BasicJsonType, typename ArithmeticType, enable_if_t < std::is_arithmetic::value&& diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index 562089c330..a05594db80 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -17,6 +17,10 @@ #include // valarray #include // vector +#ifdef JSON_HAS_CPP_17 + #include // optional +#endif + #include #include #include @@ -260,6 +264,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> inline void to_json(BasicJsonType& j, T b) noexcept diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 14de2792af..1998ba34a6 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -169,6 +169,10 @@ #include // pair, declval #include // valarray +#ifdef JSON_HAS_CPP_17 + #include // optional +#endif + // #include // __ _____ _____ _____ // __| | __| | | | JSON for Modern C++ @@ -4696,6 +4700,21 @@ inline 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 < typename BasicJsonType, typename ArithmeticType, enable_if_t < std::is_arithmetic::value&& @@ -5169,6 +5188,10 @@ NLOHMANN_JSON_NAMESPACE_END #include // valarray #include // vector +#ifdef JSON_HAS_CPP_17 + #include // optional +#endif + // #include // __ _____ _____ _____ // __| | __| | | | JSON for Modern C++ @@ -5663,6 +5686,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> inline void to_json(BasicJsonType& j, T b) noexcept diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index f88fd94cb6..89187a0f80 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -28,10 +28,23 @@ using nlohmann::json; #include #include + // NLOHMANN_JSON_SERIALIZE_ENUM uses a static std::pair DOCTEST_CLANG_SUPPRESS_WARNING_PUSH DOCTEST_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors") +#if (defined(__cplusplus) && __cplusplus >= 201703L) || (defined(_HAS_CXX17) && _HAS_CXX17 == 1) // fix for issue #464 + #define JSON_HAS_CPP_17 + #define JSON_HAS_CPP_14 +#elif (defined(__cplusplus) && __cplusplus >= 201402L) || (defined(_HAS_CXX14) && _HAS_CXX14 == 1) + #define JSON_HAS_CPP_14 +#endif + +#if defined(JSON_HAS_CPP_17) + #include + #include +#endif + TEST_CASE("value conversion") { SECTION("get an object (explicit)") @@ -152,7 +165,10 @@ TEST_CASE("value conversion") } } +<<<<<<< HEAD:tests/src/unit-conversions.cpp #if JSON_USE_IMPLICIT_CONVERSIONS +======= +>>>>>>> df30a0ea (:construction: conversions for std::optional):test/src/unit-conversions.cpp SECTION("get an object (implicit)") { const json::object_t o_reference = {{"object", json::object()}, @@ -1569,4 +1585,66 @@ TEST_CASE("JSON to enum mapping") } } +<<<<<<< HEAD:tests/src/unit-conversions.cpp DOCTEST_CLANG_SUPPRESS_WARNING_POP +======= +#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 +>>>>>>> df30a0ea (:construction: conversions for std::optional):test/src/unit-conversions.cpp From a02f9b90f92b3b03471a4087504cc3cd27a350c7 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 23 Nov 2019 13:29:47 +0100 Subject: [PATCH 02/16] :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 95d2ad528b..eea838eb86 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -21,7 +21,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 a05594db80..9aafdb5d37 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -18,7 +18,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 1998ba34a6..14d7d7e6a7 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -170,7 +170,11 @@ #include // valarray #ifdef JSON_HAS_CPP_17 - #include // optional + #if __has_include() + #include + #elif __has_include() + #include + #endif #endif // #include @@ -5189,7 +5193,11 @@ NLOHMANN_JSON_NAMESPACE_END #include // vector #ifdef JSON_HAS_CPP_17 - #include // optional + #if __has_include() + #include + #elif __has_include() + #include + #endif #endif // #include From 777693975452a1e8acd48b0142a861a8bab5d576 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 23 Nov 2019 14:40:15 +0100 Subject: [PATCH 03/16] :green_heart: overwork tests --- tests/src/unit-conversions.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index 89187a0f80..9ea54c9d63 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -1629,21 +1629,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 f4990ae0cf8e6ecb594ca9d5fb97992723c1d37c Mon Sep 17 00:00:00 2001 From: Markus Palonen Date: Tue, 17 Dec 2019 16:25:35 +0200 Subject: [PATCH 04/16] 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 | 14 +++++++++ single_include/nlohmann/json.hpp | 30 +++++++++---------- 4 files changed, 28 insertions(+), 32 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index eea838eb86..ab3d659790 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -20,14 +20,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 9aafdb5d37..641d5d0258 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -17,14 +17,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 be31fd88cb..65a52f47f5 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -151,6 +151,20 @@ #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]] #else #define JSON_NO_UNIQUE_ADDRESS + +#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 + #pragma GCC diagnostic ignored "-Wfloat-equal" +>>>>>>> 1b846c9d (Use JSON_HAS_CPP_17 only after it has been defined) #endif // disable documentation warnings on clang diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 14d7d7e6a7..f962156aa2 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -169,14 +169,6 @@ #include // pair, declval #include // valarray -#ifdef JSON_HAS_CPP_17 - #if __has_include() - #include - #elif __has_include() - #include - #endif -#endif - // #include // __ _____ _____ _____ // __| | __| | | | JSON for Modern C++ @@ -2511,6 +2503,20 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]] #else #define JSON_NO_UNIQUE_ADDRESS + +#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 + #pragma GCC diagnostic ignored "-Wfloat-equal" +>>>>>>> 1b846c9d (Use JSON_HAS_CPP_17 only after it has been defined) #endif // disable documentation warnings on clang @@ -5192,14 +5198,6 @@ NLOHMANN_JSON_NAMESPACE_END #include // valarray #include // vector -#ifdef JSON_HAS_CPP_17 - #if __has_include() - #include - #elif __has_include() - #include - #endif -#endif - // #include // __ _____ _____ _____ // __| | __| | | | JSON for Modern C++ From 1e9eea58e2020941b349d73fc33e4f3952645a8e Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sat, 16 May 2020 19:37:18 +0200 Subject: [PATCH 05/16] :white_check_mark: update tests --- tests/src/unit-conversions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index 9ea54c9d63..7414ff3c33 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -1597,7 +1597,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 7ffe2c4bbd6c7017f67d168e6123ed5859bae7fc Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 17 May 2020 13:58:09 +0200 Subject: [PATCH 06/16] :checkered_flag: include right header --- tests/src/unit-conversions.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index 7414ff3c33..9e72220ae9 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -40,8 +40,15 @@ DOCTEST_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors") #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 From b432a6123a0ff2468ba148cb20098671447a0c35 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Sun, 3 Jan 2021 20:10:43 +0100 Subject: [PATCH 07/16] :recycle: do not include experimental headers --- include/nlohmann/detail/macro_scope.hpp | 6 +----- single_include/nlohmann/json.hpp | 6 +----- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 65a52f47f5..a598933327 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -153,11 +153,7 @@ #define JSON_NO_UNIQUE_ADDRESS #ifdef JSON_HAS_CPP_17 - #if __has_include() - #include - #elif __has_include() - #include - #endif + #include #endif // disable float-equal warnings on GCC/clang diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f962156aa2..f1e230ff4a 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2505,11 +2505,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_NO_UNIQUE_ADDRESS #ifdef JSON_HAS_CPP_17 - #if __has_include() - #include - #elif __has_include() - #include - #endif + #include #endif // disable float-equal warnings on GCC/clang From e6e9ff9887f09c9a028970f77f3858aeb61c3f45 Mon Sep 17 00:00:00 2001 From: Fredrik Sandhei Date: Sat, 20 May 2023 12:02:44 +0200 Subject: [PATCH 08/16] Add missing #endif after rebase --- include/nlohmann/detail/macro_scope.hpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index a598933327..7929873e4b 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -47,6 +47,12 @@ #define JSON_HAS_CPP_11 #endif +// disable float-equal warnings on GCC/clang +#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) + #pragma GCC diagnostic push + #pragma GCC diagnostic ignored "-Wfloat-equal" +#endif + #ifdef __has_include #if __has_include() #include @@ -156,13 +162,6 @@ #include #endif -// disable float-equal warnings on GCC/clang -#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wfloat-equal" ->>>>>>> 1b846c9d (Use JSON_HAS_CPP_17 only after it has been defined) -#endif - // disable documentation warnings on clang #if defined(__clang__) #pragma clang diagnostic push From 068e3fbd1373a45c03cfdcca5ef8ceb494b35690 Mon Sep 17 00:00:00 2001 From: Fredrik Sandhei Date: Tue, 19 Mar 2024 22:16:04 +0100 Subject: [PATCH 09/16] Fix failing test --- include/nlohmann/detail/conversions/from_json.hpp | 2 +- include/nlohmann/detail/macro_scope.hpp | 8 ++++---- single_include/nlohmann/json.hpp | 3 +-- tests/src/unit-conversions.cpp | 2 +- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index ab3d659790..7c5ea1ad61 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -53,7 +53,7 @@ void from_json(const BasicJsonType& j, std::optional& opt) } else { - opt = j.template get(); + opt.emplace(j.template get()); } } #endif diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 7929873e4b..1b5c54e0ad 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -47,6 +47,10 @@ #define JSON_HAS_CPP_11 #endif +#ifdef JSON_HAS_CPP_17 + #include +#endif + // disable float-equal warnings on GCC/clang #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic push @@ -158,10 +162,6 @@ #else #define JSON_NO_UNIQUE_ADDRESS -#ifdef JSON_HAS_CPP_17 - #include -#endif - // disable documentation warnings on clang #if defined(__clang__) #pragma clang diagnostic push diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f1e230ff4a..59a3ac59f3 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2512,7 +2512,6 @@ JSON_HEDLEY_DIAGNOSTIC_POP #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wfloat-equal" ->>>>>>> 1b846c9d (Use JSON_HAS_CPP_17 only after it has been defined) #endif // disable documentation warnings on clang @@ -4716,7 +4715,7 @@ void from_json(const BasicJsonType& j, std::optional& opt) } else { - opt = j.template get(); + opt.emplace(j.template get()); } } #endif diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index 9e72220ae9..10a1c19380 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -1604,7 +1604,7 @@ TEST_CASE("std::optional") std::optional opt_null; CHECK(json(opt_null) == j_null); - CHECK(std::optional(j_null) == std::nullopt); + CHECK(j_null.get>() == std::nullopt); } SECTION("string") From a37eeab2ebc9da23f5ee04627379e79fc041cc0c Mon Sep 17 00:00:00 2001 From: Fredrik Sandhei Date: Thu, 21 Mar 2024 22:00:28 +0100 Subject: [PATCH 10/16] Only define conversion to std::optional when JSON_USE_IMPLICIT_CONVERSION is disabled. --- .../nlohmann/detail/conversions/from_json.hpp | 5 +++- single_include/nlohmann/json.hpp | 5 +++- tests/src/unit-conversions.cpp | 24 ++++++++++++------- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index 7c5ea1ad61..eb4b2b790b 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -44,6 +44,7 @@ inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n) } #ifdef JSON_HAS_CPP_17 +#ifndef JSON_USE_IMPLICIT_CONVERSIONS template void from_json(const BasicJsonType& j, std::optional& opt) { @@ -56,7 +57,9 @@ void from_json(const BasicJsonType& j, std::optional& opt) opt.emplace(j.template get()); } } -#endif + +#endif // JSON_USE_IMPLICIT_CONVERSIONS +#endif // JSON_HAS_CPP_17 // overloads for basic_json template parameters template < typename BasicJsonType, typename ArithmeticType, diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 59a3ac59f3..4d892bac4a 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -4706,6 +4706,7 @@ inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n) } #ifdef JSON_HAS_CPP_17 +#ifndef JSON_USE_IMPLICIT_CONVERSIONS template void from_json(const BasicJsonType& j, std::optional& opt) { @@ -4718,7 +4719,9 @@ void from_json(const BasicJsonType& j, std::optional& opt) opt.emplace(j.template get()); } } -#endif + +#endif // JSON_USE_IMPLICIT_CONVERSIONS +#endif // JSON_HAS_CPP_17 // overloads for basic_json template parameters template < typename BasicJsonType, typename ArithmeticType, diff --git a/tests/src/unit-conversions.cpp b/tests/src/unit-conversions.cpp index 10a1c19380..911fbcaad8 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -172,10 +172,8 @@ TEST_CASE("value conversion") } } -<<<<<<< HEAD:tests/src/unit-conversions.cpp #if JSON_USE_IMPLICIT_CONVERSIONS -======= ->>>>>>> df30a0ea (:construction: conversions for std::optional):test/src/unit-conversions.cpp + SECTION("get an object (implicit)") { const json::object_t o_reference = {{"object", json::object()}, @@ -1592,10 +1590,9 @@ TEST_CASE("JSON to enum mapping") } } -<<<<<<< HEAD:tests/src/unit-conversions.cpp -DOCTEST_CLANG_SUPPRESS_WARNING_POP -======= + #ifdef JSON_HAS_CPP_17 +#ifndef JSON_USE_IMPLICIT_CONVERSIONS TEST_CASE("std::optional") { SECTION("null") @@ -1631,7 +1628,7 @@ TEST_CASE("std::optional") std::optional opt_int = 1; CHECK(json(opt_int) == j_number); - CHECK(std::optional(j_number) == opt_int); + CHECK(j_number.get>() == opt_int); } SECTION("array") @@ -1640,7 +1637,7 @@ TEST_CASE("std::optional") std::vector> opt_array = {{1, 2, std::nullopt}}; CHECK(json(opt_array) == j_array); - CHECK(std::vector>(j_array) == opt_array); + CHECK(j_array.get>>() == opt_array); } SECTION("object") @@ -1653,4 +1650,13 @@ TEST_CASE("std::optional") } } #endif ->>>>>>> df30a0ea (:construction: conversions for std::optional):test/src/unit-conversions.cpp +#endif + +#ifdef JSON_HAS_CPP_17 + #undef JSON_HAS_CPP_17 +#endif + +#ifdef JSON_HAS_CPP_14 + #undef JSON_HAS_CPP_14 +#endif +DOCTEST_CLANG_SUPPRESS_WARNING_POP From 2edff8eba207498b156e1ca3fbcd3f905e4156a5 Mon Sep 17 00:00:00 2001 From: Fredrik Sandhei Date: Wed, 13 Nov 2024 20:56:12 +0100 Subject: [PATCH 11/16] missing endif --- include/nlohmann/detail/macro_scope.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 1b5c54e0ad..e8f6cf16af 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -161,6 +161,7 @@ #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]] #else #define JSON_NO_UNIQUE_ADDRESS +#endif // disable documentation warnings on clang #if defined(__clang__) From faa61f1b361e44e1ca6a1eb96be687187bde7e59 Mon Sep 17 00:00:00 2001 From: Fredrik Sandhei Date: Wed, 13 Nov 2024 21:53:46 +0100 Subject: [PATCH 12/16] Remove Wfloat-equal suppress --- include/nlohmann/detail/macro_scope.hpp | 6 ------ single_include/nlohmann/json.hpp | 6 ------ 2 files changed, 12 deletions(-) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index e8f6cf16af..db29a7b51f 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -51,12 +51,6 @@ #include #endif -// disable float-equal warnings on GCC/clang -#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wfloat-equal" -#endif - #ifdef __has_include #if __has_include() #include diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 4d892bac4a..efbdd640a8 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2508,12 +2508,6 @@ JSON_HEDLEY_DIAGNOSTIC_POP #include #endif -// disable float-equal warnings on GCC/clang -#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__) - #pragma GCC diagnostic push - #pragma GCC diagnostic ignored "-Wfloat-equal" -#endif - // disable documentation warnings on clang #if defined(__clang__) #pragma clang diagnostic push From a83e52388eef65179c93424065b13b99a735e5cb Mon Sep 17 00:00:00 2001 From: Fredrik Sandhei Date: Thu, 14 Nov 2024 08:23:19 +0100 Subject: [PATCH 13/16] amalgamate --- single_include/nlohmann/json.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index efbdd640a8..f4a5edd6d9 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2503,6 +2503,7 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_NO_UNIQUE_ADDRESS [[no_unique_address]] #else #define JSON_NO_UNIQUE_ADDRESS +#endif #ifdef JSON_HAS_CPP_17 #include From 4341bef2d65fff8f016265fac5eac171d037baff Mon Sep 17 00:00:00 2001 From: Fredrik Sandhei Date: Thu, 14 Nov 2024 08:37:10 +0100 Subject: [PATCH 14/16] Move include of optional out of macro_scope; probably does not make sense to be there --- include/nlohmann/detail/conversions/from_json.hpp | 4 ++++ include/nlohmann/detail/conversions/to_json.hpp | 3 +++ include/nlohmann/detail/macro_scope.hpp | 4 ---- single_include/nlohmann/json.hpp | 11 +++++++---- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index eb4b2b790b..8f83341f48 100644 --- a/include/nlohmann/detail/conversions/from_json.hpp +++ b/include/nlohmann/detail/conversions/from_json.hpp @@ -13,6 +13,9 @@ #include // forward_list #include // inserter, front_inserter, end #include // map +#ifdef JSON_HAS_CPP_17 + #include // optional +#endif #include // string #include // tuple, make_tuple #include // is_arithmetic, is_same, is_enum, underlying_type, is_convertible @@ -20,6 +23,7 @@ #include // pair, declval #include // valarray + #include #include #include diff --git a/include/nlohmann/detail/conversions/to_json.hpp b/include/nlohmann/detail/conversions/to_json.hpp index 641d5d0258..18c4493e75 100644 --- a/include/nlohmann/detail/conversions/to_json.hpp +++ b/include/nlohmann/detail/conversions/to_json.hpp @@ -10,6 +10,9 @@ #include // copy #include // begin, end +#ifdef JSON_HAS_CPP_17 + #include // optional +#endif #include // string #include // tuple, get #include // is_same, is_constructible, is_floating_point, is_enum, underlying_type diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index db29a7b51f..be31fd88cb 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -47,10 +47,6 @@ #define JSON_HAS_CPP_11 #endif -#ifdef JSON_HAS_CPP_17 - #include -#endif - #ifdef __has_include #if __has_include() #include diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index f4a5edd6d9..123d106f18 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -162,6 +162,9 @@ #include // forward_list #include // inserter, front_inserter, end #include // map +#ifdef JSON_HAS_CPP_17 + #include // optional +#endif #include // string #include // tuple, make_tuple #include // is_arithmetic, is_same, is_enum, underlying_type, is_convertible @@ -169,6 +172,7 @@ #include // pair, declval #include // valarray + // #include // __ _____ _____ _____ // __| | __| | | | JSON for Modern C++ @@ -2505,10 +2509,6 @@ JSON_HEDLEY_DIAGNOSTIC_POP #define JSON_NO_UNIQUE_ADDRESS #endif -#ifdef JSON_HAS_CPP_17 - #include -#endif - // disable documentation warnings on clang #if defined(__clang__) #pragma clang diagnostic push @@ -5184,6 +5184,9 @@ NLOHMANN_JSON_NAMESPACE_END #include // copy #include // begin, end +#ifdef JSON_HAS_CPP_17 + #include // optional +#endif #include // string #include // tuple, get #include // is_same, is_constructible, is_floating_point, is_enum, underlying_type From 3a9ff3693502b3f495115edf7268368e43b67856 Mon Sep 17 00:00:00 2001 From: Fredrik Sandhei Date: Thu, 14 Nov 2024 15:37:41 +0100 Subject: [PATCH 15/16] Make clang-tidy happy --- tests/src/unit-readme.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/unit-readme.cpp b/tests/src/unit-readme.cpp index dc99073e20..99c5ab992b 100644 --- a/tests/src/unit-readme.cpp +++ b/tests/src/unit-readme.cpp @@ -171,7 +171,7 @@ TEST_CASE("README" * doctest::skip()) // find an entry CHECK(o.find("foo") != o.end()); - if (o.find("foo") != o.end()) + if (o.contains("foo")) { // there is an entry with key "foo" } From 296549c3ad3941ccf007db543c8e0d3f908a67d0 Mon Sep 17 00:00:00 2001 From: Fredrik Sandhei Date: Thu, 14 Nov 2024 15:51:27 +0100 Subject: [PATCH 16/16] Suppress lint instead of changing to 'contains' --- tests/src/unit-readme.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/unit-readme.cpp b/tests/src/unit-readme.cpp index 99c5ab992b..85d2319552 100644 --- a/tests/src/unit-readme.cpp +++ b/tests/src/unit-readme.cpp @@ -171,7 +171,7 @@ TEST_CASE("README" * doctest::skip()) // find an entry CHECK(o.find("foo") != o.end()); - if (o.contains("foo")) + if (o.find("foo") != o.end()) // NOLINT(readability-container-contains) { // there is an entry with key "foo" }