Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/nlohmann/detail/conversions/from_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
}

#ifdef JSON_HAS_CPP_17
template<typename BasicJsonType, typename T>
template < typename BasicJsonType, typename T,
typename std::enable_if < !nlohmann::detail::is_basic_json<T>::value, int >::type = 0 >
void from_json(const BasicJsonType& j, std::optional<T>& opt)
{
if (j.is_null())
Expand Down
3 changes: 2 additions & 1 deletion single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4889,7 +4889,8 @@ inline void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
}

#ifdef JSON_HAS_CPP_17
template<typename BasicJsonType, typename T>
template < typename BasicJsonType, typename T,
typename std::enable_if < !nlohmann::detail::is_basic_json<T>::value, int >::type = 0 >
void from_json(const BasicJsonType& j, std::optional<T>& opt)
{
if (j.is_null())
Expand Down
17 changes: 17 additions & 0 deletions tests/src/unit-regression2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,23 @@ TEST_CASE("regression tests 2")
const auto decoded = json_4804::from_cbor(data);
CHECK((decoded == json_4804::array()));
}

SECTION("issue #5046 - implicit conversion of return json to std::optional no longer implicit")
{
constexpr const char* CFG_PROP_DEFAULT = "default";

const json jval{};
auto GetValue = [&](const json & valRoot) -> std::optional<json>
{
if (valRoot.contains(CFG_PROP_DEFAULT))
{
return valRoot.at(CFG_PROP_DEFAULT);
}
return std::nullopt;
};
auto result = GetValue(jval);
CHECK(!result.has_value());
}
#endif
}

Expand Down
Loading