🚑 add specialization of get_to #2175

This commit is contained in:
Niels Lohmann 2020-06-30 14:26:52 +02:00
parent eb7376bb13
commit c7e079cc98
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
3 changed files with 41 additions and 11 deletions

View file

@ -3019,6 +3019,18 @@ class basic_json
return v; 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 < template <
typename T, std::size_t N, typename T, std::size_t N,
typename Array = T (&)[N], typename Array = T (&)[N],

View file

@ -18949,6 +18949,18 @@ class basic_json
return v; 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 < template <
typename T, std::size_t N, typename T, std::size_t N,
typename Array = T (&)[N], typename Array = T (&)[N],

View file

@ -41,20 +41,22 @@ class person_with_private_data
private: private:
std::string name; std::string name;
int age = 0; int age = 0;
json metadata;
public: public:
bool operator==(const person_with_private_data& rhs) const bool operator==(const person_with_private_data& rhs) const
{ {
return std::tie(name, age) == std::tie(rhs.name, rhs.age); return std::tie(name, age, metadata) == std::tie(rhs.name, rhs.age, rhs.metadata);
} }
person_with_private_data() = default; 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)) : name(std::move(name))
, age(age) , 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 class person_without_private_data_1
@ -62,19 +64,21 @@ class person_without_private_data_1
public: public:
std::string name; std::string name;
int age = 0; int age = 0;
json metadata;
bool operator==(const person_without_private_data_1& rhs) const bool operator==(const person_without_private_data_1& rhs) const
{ {
return std::tie(name, age) == std::tie(rhs.name, rhs.age); return std::tie(name, age, metadata) == std::tie(rhs.name, rhs.age, rhs.metadata);
} }
person_without_private_data_1() = default; 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)) : name(std::move(name))
, age(age) , 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 class person_without_private_data_2
@ -82,20 +86,22 @@ class person_without_private_data_2
public: public:
std::string name; std::string name;
int age = 0; int age = 0;
json metadata;
bool operator==(const person_without_private_data_2& rhs) const bool operator==(const person_without_private_data_2& rhs) const
{ {
return std::tie(name, age) == std::tie(rhs.name, rhs.age); return std::tie(name, age, metadata) == std::tie(rhs.name, rhs.age, rhs.metadata);
} }
person_without_private_data_2() = default; 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)) : name(std::move(name))
, age(age) , 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 } // namespace persons
TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE", T, TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE", T,
@ -106,8 +112,8 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRU
SECTION("person") SECTION("person")
{ {
// serialization // serialization
T p1("Erik", 1); T p1("Erik", 1, {{"haircuts", 2}});
CHECK(json(p1).dump() == "{\"age\":1,\"name\":\"Erik\"}"); CHECK(json(p1).dump() == "{\"age\":1,\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}");
// deserialization // deserialization
T p2 = json(p1); T p2 = json(p1);