mirror of
https://github.com/nlohmann/json.git
synced 2026-07-03 17:24:18 +00:00
Extend value to arrays when using JSON pointers (#5223)
* ✨ extend value to arrays when using JSON pointers Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 💚 avoid exceptions Signed-off-by: Niels Lohmann <mail@nlohmann.me> * 💚 avoid exceptions Signed-off-by: Niels Lohmann <mail@nlohmann.me> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
+14
-16
@@ -2393,19 +2393,18 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
&& !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
|
||||
ValueType value(const json_pointer& ptr, const ValueType& default_value) const
|
||||
{
|
||||
// value only works for objects
|
||||
if (JSON_HEDLEY_LIKELY(is_object()))
|
||||
// value only works for arrays and objects
|
||||
if (JSON_HEDLEY_LIKELY(is_structured()))
|
||||
{
|
||||
// If the pointer resolves to a value, return it. Otherwise, return
|
||||
// 'default_value'.
|
||||
JSON_TRY
|
||||
const auto* res = ptr.get_checked_or_null(this);
|
||||
if (JSON_HEDLEY_LIKELY(res != nullptr))
|
||||
{
|
||||
return ptr.get_checked(this).template get<ValueType>();
|
||||
}
|
||||
JSON_INTERNAL_CATCH (out_of_range&)
|
||||
{
|
||||
return default_value;
|
||||
return res->template get<ValueType>();
|
||||
}
|
||||
|
||||
return default_value;
|
||||
}
|
||||
|
||||
JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
|
||||
@@ -2419,19 +2418,18 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
&& !std::is_same<value_t, detail::uncvref_t<ValueType>>::value, int > = 0 >
|
||||
ReturnType value(const json_pointer& ptr, ValueType && default_value) const
|
||||
{
|
||||
// value only works for objects
|
||||
if (JSON_HEDLEY_LIKELY(is_object()))
|
||||
// value only works for arrays and objects
|
||||
if (JSON_HEDLEY_LIKELY(is_structured()))
|
||||
{
|
||||
// If the pointer resolves to a value, return it. Otherwise, return
|
||||
// 'default_value'.
|
||||
JSON_TRY
|
||||
const auto* res = ptr.get_checked_or_null(this);
|
||||
if (JSON_HEDLEY_LIKELY(res != nullptr))
|
||||
{
|
||||
return ptr.get_checked(this).template get<ReturnType>();
|
||||
}
|
||||
JSON_INTERNAL_CATCH (out_of_range&)
|
||||
{
|
||||
return std::forward<ValueType>(default_value);
|
||||
return res->template get<ReturnType>();
|
||||
}
|
||||
|
||||
return std::forward<ValueType>(default_value);
|
||||
}
|
||||
|
||||
JSON_THROW(type_error::create(306, detail::concat("cannot use value() with ", type_name()), this));
|
||||
|
||||
Reference in New Issue
Block a user