Skip to content

Commit

Permalink
Merge pull request nlohmann#15 from nlohmann/develop
Browse files Browse the repository at this point in the history
Sync Fork from Upstream Repo
  • Loading branch information
sthagen committed Jul 7, 2020
2 parents 812845e + efcc826 commit 1f720b4
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ pedantic_gcc:
-Wmismatched-tags \
-Wmissing-attributes \
-Wmissing-braces \
-Wmissing-declarations \
-Wno-missing-declarations \
-Wmissing-field-initializers \
-Wmissing-include-dirs \
-Wmissing-profile \
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -886,7 +886,7 @@ The `to_json`/`from_json` functions for the `person` struct above can be created

```cpp
namespace ns {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person, name, address, age);
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person, name, address, age)
}
```
Expand All @@ -901,7 +901,7 @@ namespace ns {
int postcode;
public:
NLOHMANN_DEFINE_TYPE_INTRUSIVE(address, street, housenumber, postcode);
NLOHMANN_DEFINE_TYPE_INTRUSIVE(address, street, housenumber, postcode)
};
}
```
Expand Down
15 changes: 14 additions & 1 deletion include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2238,7 +2238,8 @@ class basic_json
@param[in] error_handler how to react on decoding errors; there are three
possible values: `strict` (throws and exception in case a decoding error
occurs; default), `replace` (replace invalid UTF-8 sequences with U+FFFD),
and `ignore` (ignore invalid UTF-8 sequences during serialization).
and `ignore` (ignore invalid UTF-8 sequences during serialization; all
bytes are copied to the output unchanged).
@return string containing the serialization of the JSON value
Expand Down Expand Up @@ -3019,6 +3020,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
15 changes: 14 additions & 1 deletion single_include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18168,7 +18168,8 @@ class basic_json
@param[in] error_handler how to react on decoding errors; there are three
possible values: `strict` (throws and exception in case a decoding error
occurs; default), `replace` (replace invalid UTF-8 sequences with U+FFFD),
and `ignore` (ignore invalid UTF-8 sequences during serialization).
and `ignore` (ignore invalid UTF-8 sequences during serialization; all
bytes are copied to the output unchanged).

@return string containing the serialization of the JSON value

Expand Down Expand Up @@ -18949,6 +18950,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
52 changes: 28 additions & 24 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;
std::string name = "";
int age = 0;
json metadata = nullptr;

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)
: name(std::move(name))
, age(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;
std::string name = "";
int age = 0;
json metadata = nullptr;

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)
: name(std::move(name))
, age(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;
std::string name = "";
int age = 0;
json metadata = nullptr;

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)
: name(std::move(name))
, age(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

0 comments on commit 1f720b4

Please sign in to comment.