From ad6eadeb70b6ba20cec985dea8da829fda2fbdd9 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 30 Jun 2020 13:59:43 +0200 Subject: [PATCH 1/4] :memo: refine documentation of error_handler parameter --- include/nlohmann/json.hpp | 3 ++- single_include/nlohmann/json.hpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index ace649b661..998aa72000 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -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 diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 7ab24c8444..426ee81574 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -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 From c7e079cc98a6c5534e8d4a209dd0984e569d0625 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 30 Jun 2020 14:26:52 +0200 Subject: [PATCH 2/4] :ambulance: add specialization of get_to #2175 --- include/nlohmann/json.hpp | 12 ++++++++++++ single_include/nlohmann/json.hpp | 12 ++++++++++++ test/src/unit-udt_macro.cpp | 28 +++++++++++++++++----------- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index ace649b661..00b7ce555c 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -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::value, + int> = 0> + ValueType & get_to(ValueType& v) const + { + v = *this; + return v; + } + template < typename T, std::size_t N, typename Array = T (&)[N], diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 7ab24c8444..9cba3069c6 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -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::value, + int> = 0> + ValueType & get_to(ValueType& v) const + { + v = *this; + return v; + } + template < typename T, std::size_t N, typename Array = T (&)[N], diff --git a/test/src/unit-udt_macro.cpp b/test/src/unit-udt_macro.cpp index ab2d324519..a45b2c5096 100644 --- a/test/src/unit-udt_macro.cpp +++ b/test/src/unit-udt_macro.cpp @@ -41,20 +41,22 @@ 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 std::tie(name, age, metadata) == std::tie(rhs.name, rhs.age, 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 @@ -62,19 +64,21 @@ 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 std::tie(name, age, metadata) == std::tie(rhs.name, rhs.age, 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 @@ -82,20 +86,22 @@ 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 std::tie(name, age, metadata) == std::tie(rhs.name, rhs.age, 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, @@ -106,8 +112,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); From f59f4a2b61161f8aab0d7b499fbb9b7a94b66188 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Tue, 30 Jun 2020 19:55:40 +0200 Subject: [PATCH 3/4] :green_heart: fix build --- test/src/unit-udt_macro.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/src/unit-udt_macro.cpp b/test/src/unit-udt_macro.cpp index a45b2c5096..4535ae5b53 100644 --- a/test/src/unit-udt_macro.cpp +++ b/test/src/unit-udt_macro.cpp @@ -32,8 +32,6 @@ SOFTWARE. #include using nlohmann::json; -#include - namespace persons { class person_with_private_data @@ -46,7 +44,7 @@ class person_with_private_data public: bool operator==(const person_with_private_data& rhs) const { - return std::tie(name, age, metadata) == std::tie(rhs.name, rhs.age, rhs.metadata); + return name == rhs.name && age == rhs.age && metadata == rhs.metadata; } person_with_private_data() = default; @@ -68,7 +66,7 @@ class person_without_private_data_1 bool operator==(const person_without_private_data_1& rhs) const { - return std::tie(name, age, metadata) == std::tie(rhs.name, rhs.age, rhs.metadata); + return name == rhs.name && age == rhs.age && metadata == rhs.metadata; } person_without_private_data_1() = default; @@ -90,7 +88,7 @@ class person_without_private_data_2 bool operator==(const person_without_private_data_2& rhs) const { - return std::tie(name, age, metadata) == std::tie(rhs.name, rhs.age, rhs.metadata); + return name == rhs.name && age == rhs.age && metadata == rhs.metadata; } person_without_private_data_2() = default; From efcc826ecb9b55893397f749e5514316ba8629bb Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Mon, 6 Jul 2020 12:37:39 +0200 Subject: [PATCH 4/4] :rotating_light: fix warning --- Makefile | 2 +- README.md | 4 ++-- test/src/unit-udt_macro.cpp | 46 ++++++++++++++++++------------------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index 8579fb5db1..ff62d6e7d8 100644 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/README.md b/README.md index 6d21e2d85a..3bb92d38ba 100644 --- a/README.md +++ b/README.md @@ -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) } ``` @@ -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) }; } ``` diff --git a/test/src/unit-udt_macro.cpp b/test/src/unit-udt_macro.cpp index 4535ae5b53..a6b66e9550 100644 --- a/test/src/unit-udt_macro.cpp +++ b/test/src/unit-udt_macro.cpp @@ -37,9 +37,9 @@ namespace persons class person_with_private_data { private: - std::string name; + std::string name = ""; int age = 0; - json metadata; + json metadata = nullptr; public: bool operator==(const person_with_private_data& rhs) const @@ -48,21 +48,21 @@ class person_with_private_data } person_with_private_data() = default; - person_with_private_data(std::string name, int age, json metadata) - : name(std::move(name)) - , age(age) - , metadata(std::move(metadata)) + 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, metadata); + 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; + std::string name = ""; + int age = 0; + json metadata = nullptr; bool operator==(const person_without_private_data_1& rhs) const { @@ -70,21 +70,21 @@ class person_without_private_data_1 } person_without_private_data_1() = default; - person_without_private_data_1(std::string name, int age, json metadata) - : name(std::move(name)) - , age(age) - , metadata(std::move(metadata)) + 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, metadata); + 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; + std::string name = ""; + int age = 0; + json metadata = nullptr; bool operator==(const person_without_private_data_2& rhs) const { @@ -92,14 +92,14 @@ class person_without_private_data_2 } person_without_private_data_2() = default; - person_without_private_data_2(std::string name, int age, json metadata) - : name(std::move(name)) - , age(age) - , metadata(std::move(metadata)) + 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, metadata); +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,