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

Add specialization of get_to #2233

Merged
merged 2 commits into from
Jul 6, 2020
Merged
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
12 changes: 12 additions & 0 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3019,6 +3019,18 @@ class basic_json
return v;
}

// specialization to allow to call get_to with a basic_json value
// see https://github.com/nlohmann/json/issues/2175
template<typename ValueType,
detail::enable_if_t <
detail::is_basic_json<ValueType>::value,
int> = 0>
ValueType & get_to(ValueType& v) const
{
v = *this;
return v;
}

template <
typename T, std::size_t N,
typename Array = T (&)[N],
Expand Down
12 changes: 12 additions & 0 deletions single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18949,6 +18949,18 @@ class basic_json
return v;
}

// specialization to allow to call get_to with a basic_json value
// see https://github.com/nlohmann/json/issues/2175
template<typename ValueType,
detail::enable_if_t <
detail::is_basic_json<ValueType>::value,
int> = 0>
ValueType & get_to(ValueType& v) const
{
v = *this;
return v;
}

template <
typename T, std::size_t N,
typename Array = T (&)[N],
Expand Down
30 changes: 17 additions & 13 deletions test/src/unit-udt_macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,70 +32,74 @@ SOFTWARE.
#include <nlohmann/json.hpp>
using nlohmann::json;

#include <utility>

namespace persons
{
class person_with_private_data
{
private:
std::string name;
int age = 0;
json metadata;

public:
bool operator==(const person_with_private_data& rhs) const
{
return std::tie(name, age) == std::tie(rhs.name, rhs.age);
return name == rhs.name && age == rhs.age && metadata == rhs.metadata;
}

person_with_private_data() = default;
person_with_private_data(std::string name, int age)
person_with_private_data(std::string name, int age, json metadata)
: name(std::move(name))
, age(age)
, metadata(std::move(metadata))
{}

NLOHMANN_DEFINE_TYPE_INTRUSIVE(person_with_private_data, age, name);
NLOHMANN_DEFINE_TYPE_INTRUSIVE(person_with_private_data, age, name, metadata);
};

class person_without_private_data_1
{
public:
std::string name;
int age = 0;
json metadata;

bool operator==(const person_without_private_data_1& rhs) const
{
return std::tie(name, age) == std::tie(rhs.name, rhs.age);
return name == rhs.name && age == rhs.age && metadata == rhs.metadata;
}

person_without_private_data_1() = default;
person_without_private_data_1(std::string name, int age)
person_without_private_data_1(std::string name, int age, json metadata)
: name(std::move(name))
, age(age)
, metadata(std::move(metadata))
{}

NLOHMANN_DEFINE_TYPE_INTRUSIVE(person_without_private_data_1, age, name);
NLOHMANN_DEFINE_TYPE_INTRUSIVE(person_without_private_data_1, age, name, metadata);
};

class person_without_private_data_2
{
public:
std::string name;
int age = 0;
json metadata;

bool operator==(const person_without_private_data_2& rhs) const
{
return std::tie(name, age) == std::tie(rhs.name, rhs.age);
return name == rhs.name && age == rhs.age && metadata == rhs.metadata;
}

person_without_private_data_2() = default;
person_without_private_data_2(std::string name, int age)
person_without_private_data_2(std::string name, int age, json metadata)
: name(std::move(name))
, age(age)
, metadata(std::move(metadata))
{}
};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person_without_private_data_2, age, name);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person_without_private_data_2, age, name, metadata);
} // namespace persons

TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE", T,
Expand All @@ -106,8 +110,8 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRU
SECTION("person")
{
// serialization
T p1("Erik", 1);
CHECK(json(p1).dump() == "{\"age\":1,\"name\":\"Erik\"}");
T p1("Erik", 1, {{"haircuts", 2}});
CHECK(json(p1).dump() == "{\"age\":1,\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}");

// deserialization
T p2 = json(p1);
Expand Down