Only define conversion to std::optional when JSON_USE_IMPLICIT_CONVERSION is disabled.

pull/4036/head
Fredrik Sandhei 2024-03-21 22:00:28 +01:00
parent d27adfd021
commit 1a8d427a3d
3 changed files with 12 additions and 4 deletions

View File

@ -44,6 +44,7 @@ inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
} }
#ifdef JSON_HAS_CPP_17 #ifdef JSON_HAS_CPP_17
#ifndef JSON_USE_IMPLICIT_CONVERSIONS
template<typename BasicJsonType, typename T> template<typename BasicJsonType, typename T>
void from_json(const BasicJsonType& j, std::optional<T>& opt) void from_json(const BasicJsonType& j, std::optional<T>& opt)
{ {
@ -56,7 +57,9 @@ void from_json(const BasicJsonType& j, std::optional<T>& opt)
opt.emplace(j.template get<T>()); opt.emplace(j.template get<T>());
} }
} }
#endif
#endif // JSON_USE_IMPLICIT_CONVERSIONS
#endif // JSON_HAS_CPP_17
// overloads for basic_json template parameters // overloads for basic_json template parameters
template < typename BasicJsonType, typename ArithmeticType, template < typename BasicJsonType, typename ArithmeticType,

View File

@ -4616,6 +4616,7 @@ inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
} }
#ifdef JSON_HAS_CPP_17 #ifdef JSON_HAS_CPP_17
#ifndef JSON_USE_IMPLICIT_CONVERSIONS
template<typename BasicJsonType, typename T> template<typename BasicJsonType, typename T>
void from_json(const BasicJsonType& j, std::optional<T>& opt) void from_json(const BasicJsonType& j, std::optional<T>& opt)
{ {
@ -4628,7 +4629,9 @@ void from_json(const BasicJsonType& j, std::optional<T>& opt)
opt.emplace(j.template get<T>()); opt.emplace(j.template get<T>());
} }
} }
#endif
#endif // JSON_USE_IMPLICIT_CONVERSIONS
#endif // JSON_HAS_CPP_17
// overloads for basic_json template parameters // overloads for basic_json template parameters
template < typename BasicJsonType, typename ArithmeticType, template < typename BasicJsonType, typename ArithmeticType,

View File

@ -1570,6 +1570,7 @@ TEST_CASE("JSON to enum mapping")
} }
#ifdef JSON_HAS_CPP_17 #ifdef JSON_HAS_CPP_17
#ifndef JSON_USE_IMPLICIT_CONVERSIONS
TEST_CASE("std::optional") TEST_CASE("std::optional")
{ {
SECTION("null") SECTION("null")
@ -1605,7 +1606,7 @@ TEST_CASE("std::optional")
std::optional<int> opt_int = 1; std::optional<int> opt_int = 1;
CHECK(json(opt_int) == j_number); CHECK(json(opt_int) == j_number);
CHECK(std::optional<int>(j_number) == opt_int); CHECK(j_number.get<std::optional<int>>() == opt_int);
} }
SECTION("array") SECTION("array")
@ -1614,7 +1615,7 @@ TEST_CASE("std::optional")
std::vector<std::optional<int>> opt_array = {{1, 2, std::nullopt}}; std::vector<std::optional<int>> opt_array = {{1, 2, std::nullopt}};
CHECK(json(opt_array) == j_array); CHECK(json(opt_array) == j_array);
CHECK(std::vector<std::optional<int>>(j_array) == opt_array); CHECK(j_array.get<std::vector<std::optional<int>>>() == opt_array);
} }
SECTION("object") SECTION("object")
@ -1627,6 +1628,7 @@ TEST_CASE("std::optional")
} }
} }
#endif #endif
#endif
#ifdef JSON_HAS_CPP_17 #ifdef JSON_HAS_CPP_17
#undef JSON_HAS_CPP_17 #undef JSON_HAS_CPP_17