diff --git a/include/nlohmann/detail/conversions/from_json.hpp b/include/nlohmann/detail/conversions/from_json.hpp index a51cb53db..d0e024687 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 6940d479b..86b067057 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -4616,6 +4616,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) { @@ -4628,7 +4629,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 939909ec9..2ba69d83c 100644 --- a/tests/src/unit-conversions.cpp +++ b/tests/src/unit-conversions.cpp @@ -1570,6 +1570,7 @@ TEST_CASE("JSON to enum mapping") } #ifdef JSON_HAS_CPP_17 +#ifndef JSON_USE_IMPLICIT_CONVERSIONS TEST_CASE("std::optional") { SECTION("null") @@ -1605,7 +1606,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") @@ -1614,7 +1615,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") @@ -1627,6 +1628,7 @@ TEST_CASE("std::optional") } } #endif +#endif #ifdef JSON_HAS_CPP_17 #undef JSON_HAS_CPP_17