Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 2181 #18

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
46 changes: 44 additions & 2 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3782,6 +3782,27 @@ class basic_json

@since version 1.0.0
*/
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, const 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 default_value;
}

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>
Expand Down Expand Up @@ -3809,7 +3830,7 @@ class basic_json
*/
string_t value(const typename object_t::key_type& key, const char* default_value) const
{
return value(key, std::move(string_t(default_value)));
return value(key, string_t(default_value));
}

/*!
Expand Down Expand Up @@ -3855,6 +3876,27 @@ class basic_json

@since version 2.0.2
*/
template<class ValueType, typename std::enable_if<
std::is_convertible<basic_json_t, ValueType>::value, int>::type = 0>
ValueType value(const json_pointer& ptr, const 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 default_value;
}
}

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
Expand Down Expand Up @@ -3883,7 +3925,7 @@ class basic_json
JSON_HEDLEY_NON_NULL(3)
string_t value(const json_pointer& ptr, const char* default_value) const
{
return value(ptr, std::move(string_t(default_value)));
return value(ptr, string_t(default_value));
}

/*!
Expand Down
46 changes: 44 additions & 2 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19712,6 +19712,27 @@ class basic_json

@since version 1.0.0
*/
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, const 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 default_value;
}

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>
Expand Down Expand Up @@ -19739,7 +19760,7 @@ class basic_json
*/
string_t value(const typename object_t::key_type& key, const char* default_value) const
{
return value(key, std::move(string_t(default_value)));
return value(key, string_t(default_value));
}

/*!
Expand Down Expand Up @@ -19785,6 +19806,27 @@ class basic_json

@since version 2.0.2
*/
template<class ValueType, typename std::enable_if<
std::is_convertible<basic_json_t, ValueType>::value, int>::type = 0>
ValueType value(const json_pointer& ptr, const 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 default_value;
}
}

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
Expand Down Expand Up @@ -19813,7 +19855,7 @@ class basic_json
JSON_HEDLEY_NON_NULL(3)
string_t value(const json_pointer& ptr, const char* default_value) const
{
return value(ptr, std::move(string_t(default_value)));
return value(ptr, string_t(default_value));
}

/*!
Expand Down
12 changes: 12 additions & 0 deletions test/src/unit-element_access2.cpp
Original file line number Diff line number Diff line change
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
9 changes: 9 additions & 0 deletions test/src/unit-regression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1945,6 +1945,15 @@ TEST_CASE("regression tests")
)
);
}

SECTION("PR #2181 - regression bug with lvalue")
{
// see https://github.com/nlohmann/json/pull/2181#issuecomment-653326060
json j{{"x", "test"}};
std::string defval = "default value";
auto val = j.value("x", defval);
auto val2 = j.value("y", defval);
}
}

#if not defined(JSON_NOEXCEPTION)
Expand Down
12 changes: 6 additions & 6 deletions test/src/unit-udt_macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class person_with_private_data
class person_without_private_data_1
{
public:
std::string name = "";
int age = 0;
json metadata = nullptr;
std::string name = "";
int age = 0;
json metadata = nullptr;

bool operator==(const person_without_private_data_1& rhs) const
{
Expand All @@ -82,9 +82,9 @@ class person_without_private_data_1
class person_without_private_data_2
{
public:
std::string name = "";
int age = 0;
json metadata = nullptr;
std::string name = "";
int age = 0;
json metadata = nullptr;

bool operator==(const person_without_private_data_2& rhs) const
{
Expand Down