Skip to content

Commit

Permalink
Merge 30dd9bd into cf74131
Browse files Browse the repository at this point in the history
  • Loading branch information
dota17 committed Jul 13, 2020
2 parents cf74131 + 30dd9bd commit 49f5558
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
42 changes: 42 additions & 0 deletions include/nlohmann/json.hpp
Expand Up @@ -3802,6 +3802,27 @@ class basic_json
JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
}

template<class ValueType, typename std::enable_if<
std::is_convertible<basic_json_t, ValueType>::value
and not std::is_same<value_t, ValueType>::value, int>::type = 0>
ValueType value(const typename object_t::key_type& key, ValueType && default_value) const
{
// at only works for objects
if (JSON_HEDLEY_LIKELY(is_object()))
{
// if key is found, return value and given default value otherwise
const auto it = find(key);
if (it != end())
{
return *it;
}

return std::move(default_value);
}

JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
}

/*!
@brief overload for a default value of type const char*
@copydoc basic_json::value(const typename object_t::key_type&, const ValueType&) const
Expand Down Expand Up @@ -3875,6 +3896,27 @@ class basic_json
JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
}

template<class ValueType, typename std::enable_if<
std::is_convertible<basic_json_t, ValueType>::value, int>::type = 0>
ValueType value(const json_pointer& ptr, ValueType && default_value) const
{
// at only works for objects
if (JSON_HEDLEY_LIKELY(is_object()))
{
// if pointer resolves a value, return it or use default value
JSON_TRY
{
return ptr.get_checked(this);
}
JSON_INTERNAL_CATCH (out_of_range&)
{
return std::move(default_value);
}
}

JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
}

/*!
@brief overload for a default value of type const char*
@copydoc basic_json::value(const json_pointer&, ValueType) const
Expand Down
42 changes: 42 additions & 0 deletions single_include/nlohmann/json.hpp
Expand Up @@ -19790,6 +19790,27 @@ class basic_json
JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
}

template<class ValueType, typename std::enable_if<
std::is_convertible<basic_json_t, ValueType>::value
and not std::is_same<value_t, ValueType>::value, int>::type = 0>
ValueType value(const typename object_t::key_type& key, ValueType && default_value) const
{
// at only works for objects
if (JSON_HEDLEY_LIKELY(is_object()))
{
// if key is found, return value and given default value otherwise
const auto it = find(key);
if (it != end())
{
return *it;
}

return std::move(default_value);
}

JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
}

/*!
@brief overload for a default value of type const char*
@copydoc basic_json::value(const typename object_t::key_type&, const ValueType&) const
Expand Down Expand Up @@ -19863,6 +19884,27 @@ class basic_json
JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
}

template<class ValueType, typename std::enable_if<
std::is_convertible<basic_json_t, ValueType>::value, int>::type = 0>
ValueType value(const json_pointer& ptr, ValueType && default_value) const
{
// at only works for objects
if (JSON_HEDLEY_LIKELY(is_object()))
{
// if pointer resolves a value, return it or use default value
JSON_TRY
{
return ptr.get_checked(this);
}
JSON_INTERNAL_CATCH (out_of_range&)
{
return std::move(default_value);
}
}

JSON_THROW(type_error::create(306, "cannot use value() with " + std::string(type_name())));
}

/*!
@brief overload for a default value of type const char*
@copydoc basic_json::value(const json_pointer&, ValueType) const
Expand Down
12 changes: 12 additions & 0 deletions test/src/unit-element_access2.cpp
Expand Up @@ -282,6 +282,18 @@ TEST_CASE("element access 2")
CHECK_THROWS_WITH(j_nonobject_const.value("foo", 1),
"[json.exception.type_error.306] cannot use value() with number");
}
SECTION("default value is lvalue")
{
json j_nonobject(json::value_t::number_float);
const json j_nonobject_const(j_nonobject);
std::string defval = "default value";
CHECK_THROWS_AS(j_nonobject.value("foo", defval), json::type_error&);
CHECK_THROWS_AS(j_nonobject_const.value("foo", defval), json::type_error&);
CHECK_THROWS_WITH(j_nonobject.value("foo", defval),
"[json.exception.type_error.306] cannot use value() with number");
CHECK_THROWS_WITH(j_nonobject_const.value("foo", defval),
"[json.exception.type_error.306] cannot use value() with number");
}
}
}

Expand Down

0 comments on commit 49f5558

Please sign in to comment.