mirror of https://github.com/nlohmann/json.git
Only define conversion to std::optional when JSON_USE_IMPLICIT_CONVERSION is disabled.
parent
d27adfd021
commit
1a8d427a3d
|
@ -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,
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue