From 028a397f2ff7cba9e1073c25285010d3a49c1a5d Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Sat, 26 Jun 2021 21:54:37 +0900 Subject: [PATCH 01/12] Add macros for custom types in DEFINE_TYPE_{NON_,}INTRUSIVE --- include/nlohmann/detail/macro_scope.hpp | 46 ++++++++++++++++++++----- single_include/nlohmann/json.hpp | 34 +++++++++++++----- 2 files changed, 64 insertions(+), 16 deletions(-) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 706f1b971..4a948af44 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -375,26 +375,56 @@ #define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1); /*! -@brief macro +@brief macro to briefly define intrusive serialization of a given type to/from JSON +@note you can define your own specialized macroses like NLOHMANN_DEFINE_TYPE_INTRUSIVE +@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL +@since version 3.9.2 +*/ +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL(BasicJsonType, Type, ...) \ + friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ + } \ + friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } + +/*! +@brief macro to briefly define non-intrusive serialization of a given type to/from JSON +@note you can define your own specialized macroses like NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE +@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL +@since version 3.9.2 +*/ +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL(BasicJsonType, Type, ...) \ + inline void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ + } \ + inline void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } + +/*! +@brief macro to briefly define intrusive serialization of given type to/from nlohmann::json @def NLOHMANN_DEFINE_TYPE_INTRUSIVE @since version 3.9.0 +@sa NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL */ -#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \ - friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ - friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL(nlohmann::json, Type, __VA_ARGS__) #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \ friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) } /*! -@brief macro +@brief macro to briefly define non-intrusive serialization of given type to/from nlohmann::json @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE @since version 3.9.0 +@sa NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL */ -#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \ - inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ - inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL(nlohmann::json, Type, __VA_ARGS__) #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \ inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 50e5c805c..aeb63f946 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2602,26 +2602,44 @@ using is_detected_convertible = #define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1); /*! -@brief macro +@brief macro to briefly define intrusive serialization of a given type to/from JSON +@note you can define your own specialized macroses like NLOHMANN_DEFINE_TYPE_INTRUSIVE +@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL +@since version 3.9.2 +*/ +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL(BasicJsonType, Type, ...) \ + friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ + friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } + +/*! +@brief macro to briefly define non-intrusive serialization of a given type to/from JSON +@note you can define your own specialized macroses like NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE +@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL +@since version 3.9.2 +*/ +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL(BasicJsonType, Type, ...) \ + inline void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ + inline void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } + +/*! +@brief macro to briefly define intrusive serialization of given type to/from nlohmann::json @def NLOHMANN_DEFINE_TYPE_INTRUSIVE @since version 3.9.0 +@sa NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL */ -#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) \ - friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ - friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL(nlohmann::json, Type, __VA_ARGS__) #define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \ friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) } /*! -@brief macro +@brief macro to briefly define non-intrusive serialization of given type to/from nlohmann::json @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE @since version 3.9.0 +@sa NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL */ -#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) \ - inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ - inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL(nlohmann::json, Type, __VA_ARGS__) #define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \ inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ From efe20d5391fea93ef84315868471c741ced75dc5 Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Sat, 26 Jun 2021 21:56:17 +0900 Subject: [PATCH 02/12] Add macros for template version of DEFINE_TYPE_{NON_,}INTRUSIVE --- include/nlohmann/detail/macro_scope.hpp | 34 +++++++++++++++++++++++++ single_include/nlohmann/json.hpp | 34 +++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 4a948af44..04d7561e0 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -469,6 +469,40 @@ { \ } +/*! +@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object +@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T +@since version 3.9.2 +*/ +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(Type, ...) \ + template \ + friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ + } \ + template \ + friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } + +/*! +@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object +@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T +@since version 3.9.2 +*/ +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(Type, ...) \ + template \ + void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ + } \ + template \ + void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } + #ifndef JSON_USE_IMPLICIT_CONVERSIONS #define JSON_USE_IMPLICIT_CONVERSIONS 1 #endif diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index aeb63f946..0c3e09c41 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2684,6 +2684,40 @@ using is_detected_convertible = { \ } +/*! +@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object +@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T +@since version 3.9.2 +*/ +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(Type, ...) \ + template \ + friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ + } \ + template \ + friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } \ + +/*! +@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object +@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T +@since version 3.9.2 +*/ +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(Type, ...) \ + template \ + void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ + } \ + template \ + void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } + #ifndef JSON_USE_IMPLICIT_CONVERSIONS #define JSON_USE_IMPLICIT_CONVERSIONS 1 #endif From 7002b1daad823ec7e3588bc723078741b1674f8d Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Sat, 26 Jun 2021 21:56:48 +0900 Subject: [PATCH 03/12] Refactor and add tests for templated version of DEFINE_TYPE macros --- tests/src/unit-udt_macro.cpp | 442 +++++++++++++++-------------------- 1 file changed, 187 insertions(+), 255 deletions(-) diff --git a/tests/src/unit-udt_macro.cpp b/tests/src/unit-udt_macro.cpp index 1762d08da..023e8bffb 100644 --- a/tests/src/unit-udt_macro.cpp +++ b/tests/src/unit-udt_macro.cpp @@ -36,295 +36,218 @@ using nlohmann::json; namespace persons { -class person_with_private_data -{ - private: - std::string name{}; - int age = 0; - json metadata = nullptr; - - public: - bool operator==(const person_with_private_data& rhs) const - { - return name == rhs.name && age == rhs.age && metadata == rhs.metadata; +#define PERSON_CLASS_BODY(ClassName, Visibility) \ +Visibility: \ + std::string name; \ + int age = 0; \ + json metadata = nullptr; \ + \ + public: \ + bool operator==(const ClassName& rhs) const \ + { \ + return name == rhs.name && age == rhs.age && metadata == rhs.metadata; \ + } \ + ClassName() = default; \ + ClassName(std::string name_, int age_, json metadata_) \ + : name(std::move(name_)) \ + , age(age_) \ + , metadata(std::move(metadata_)) \ + {} \ + std::string getName() const \ + { \ + return name; \ + } \ + int getAge() const \ + { \ + return age; \ + } \ + json getMetadata() const \ + { \ + return metadata; \ } - 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_)) - {} +#define ALPHABET_CLASS_BODY(ClassName, Visibility) \ + public: \ + bool operator==(const ClassName& other) const \ + { \ + return a == other.a && \ + b == other.b && \ + c == other.c && \ + d == other.d && \ + e == other.e && \ + f == other.f && \ + g == other.g && \ + h == other.h && \ + i == other.i && \ + j == other.j && \ + k == other.k && \ + l == other.l && \ + m == other.m && \ + n == other.n && \ + o == other.o && \ + p == other.p && \ + q == other.q && \ + r == other.r && \ + s == other.s && \ + t == other.t && \ + u == other.u && \ + v == other.v && \ + w == other.w && \ + x == other.x && \ + y == other.y && \ + z == other.z; \ + } \ + Visibility : int a = 0; \ + int b = 0; \ + int c = 0; \ + int d = 0; \ + int e = 0; \ + int f = 0; \ + int g = 0; \ + int h = 0; \ + int i = 0; \ + int j = 0; \ + int k = 0; \ + int l = 0; \ + int m = 0; \ + int n = 0; \ + int o = 0; \ + int p = 0; \ + int q = 0; \ + int r = 0; \ + int s = 0; \ + int t = 0; \ + int u = 0; \ + int v = 0; \ + int w = 0; \ + int x = 0; \ + int y = 0; \ + int z = 0; +class person_with_private_data +{ + PERSON_CLASS_BODY(person_with_private_data, private) NLOHMANN_DEFINE_TYPE_INTRUSIVE(person_with_private_data, age, name, metadata) }; class person_with_private_data_2 { - private: - std::string name{}; - int age = 0; - json metadata = nullptr; - - public: - bool operator==(const person_with_private_data_2& rhs) const - { - return name == rhs.name && age == rhs.age && metadata == rhs.metadata; - } - - person_with_private_data_2() = default; - person_with_private_data_2(std::string name_, int age_, json metadata_) - : name(std::move(name_)) - , age(age_) - , metadata(std::move(metadata_)) - {} - - std::string getName() const - { - return name; - } - int getAge() const - { - return age; - } - json getMetadata() const - { - return metadata; - } - + PERSON_CLASS_BODY(person_with_private_data_2, private) NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(person_with_private_data_2, age, name, metadata) }; class person_without_private_data_1 { - public: - std::string name{}; - int age = 0; - json metadata = nullptr; - - bool operator==(const person_without_private_data_1& rhs) const - { - 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_, json metadata_) - : name(std::move(name_)) - , age(age_) - , metadata(std::move(metadata_)) - {} - + PERSON_CLASS_BODY(person_without_private_data_1, public) 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 = nullptr; - - bool operator==(const person_without_private_data_2& rhs) const - { - 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_, json metadata_) - : name(std::move(name_)) - , age(age_) - , metadata(std::move(metadata_)) - {} + PERSON_CLASS_BODY(person_without_private_data_2, public) }; - NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person_without_private_data_2, age, name, metadata) class person_without_private_data_3 { - public: - std::string name{}; - int age = 0; - json metadata = nullptr; + PERSON_CLASS_BODY(person_without_private_data_3, public) +}; +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(person_without_private_data_3, age, name, metadata) - bool operator==(const person_without_private_data_3& rhs) const - { - return name == rhs.name && age == rhs.age && metadata == rhs.metadata; - } - - person_without_private_data_3() = default; - person_without_private_data_3(std::string name_, int age_, json metadata_) - : name(std::move(name_)) - , age(age_) - , metadata(std::move(metadata_)) - {} - - std::string getName() const - { - return name; - } - int getAge() const - { - return age; - } - json getMetadata() const - { - return metadata; - } +class person_t_with_private_data +{ + PERSON_CLASS_BODY(person_t_with_private_data, private) + NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(person_t_with_private_data, age, name, metadata) }; -NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(person_without_private_data_3, age, name, metadata) +class person_t_without_private_data_1 +{ + PERSON_CLASS_BODY(person_t_without_private_data_1, public) + NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(person_t_without_private_data_1, age, name, metadata) +}; + +class person_t_without_private_data_2 +{ + PERSON_CLASS_BODY(person_t_without_private_data_2, public) +}; +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(person_t_without_private_data_2, age, name, metadata) class person_with_private_alphabet { - public: - bool operator==(const person_with_private_alphabet& other) const - { - return a == other.a && - b == other.b && - c == other.c && - d == other.d && - e == other.e && - f == other.f && - g == other.g && - h == other.h && - i == other.i && - j == other.j && - k == other.k && - l == other.l && - m == other.m && - n == other.n && - o == other.o && - p == other.p && - q == other.q && - r == other.r && - s == other.s && - t == other.t && - u == other.u && - v == other.v && - w == other.w && - x == other.x && - y == other.y && - z == other.z; - } - - private: - int a = 0; - int b = 0; - int c = 0; - int d = 0; - int e = 0; - int f = 0; - int g = 0; - int h = 0; - int i = 0; - int j = 0; - int k = 0; - int l = 0; - int m = 0; - int n = 0; - int o = 0; - int p = 0; - int q = 0; - int r = 0; - int s = 0; - int t = 0; - int u = 0; - int v = 0; - int w = 0; - int x = 0; - int y = 0; - int z = 0; + ALPHABET_CLASS_BODY(person_with_private_alphabet, private) NLOHMANN_DEFINE_TYPE_INTRUSIVE(person_with_private_alphabet, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) }; class person_with_public_alphabet { - public: - bool operator==(const person_with_public_alphabet& other) const - { - return a == other.a && - b == other.b && - c == other.c && - d == other.d && - e == other.e && - f == other.f && - g == other.g && - h == other.h && - i == other.i && - j == other.j && - k == other.k && - l == other.l && - m == other.m && - n == other.n && - o == other.o && - p == other.p && - q == other.q && - r == other.r && - s == other.s && - t == other.t && - u == other.u && - v == other.v && - w == other.w && - x == other.x && - y == other.y && - z == other.z; - } - - int a = 0; - int b = 0; - int c = 0; - int d = 0; - int e = 0; - int f = 0; - int g = 0; - int h = 0; - int i = 0; - int j = 0; - int k = 0; - int l = 0; - int m = 0; - int n = 0; - int o = 0; - int p = 0; - int q = 0; - int r = 0; - int s = 0; - int t = 0; - int u = 0; - int v = 0; - int w = 0; - int x = 0; - int y = 0; - int z = 0; + ALPHABET_CLASS_BODY(person_with_public_alphabet, public) }; - NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person_with_public_alphabet, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) + +class person_t_with_private_alphabet +{ + ALPHABET_CLASS_BODY(person_t_with_private_alphabet, private) + NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(person_t_with_private_alphabet, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) +}; + +class person_t_with_public_alphabet +{ + ALPHABET_CLASS_BODY(person_t_with_public_alphabet, public) +}; +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(person_t_with_public_alphabet, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) } // namespace persons -TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", T, - persons::person_with_private_data, - persons::person_without_private_data_1, - persons::person_without_private_data_2) +// Trick described in https://github.com/onqtam/doctest/blob/master/doc/markdown/parameterized-tests.md +// in note "if you need parameterization on more than 1 type" +template +struct TestTypePair { + using TestedType = TestedType_; + using BasicJsonType = BasicJsonType_; +}; + +#define PERSON_PAIRS \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair + +TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", PairT, PERSON_PAIRS) +{ + using T = typename PairT::TestedType; + using json_t = typename PairT::BasicJsonType; + SECTION("person") { // serialization T p1("Erik", 1, {{"haircuts", 2}}); - CHECK(json(p1).dump() == "{\"age\":1,\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}"); + if ( std::is_same::value ) + { + CHECK(json_t(p1).dump() == "{\"age\":1,\"name\":\"Erik\",\"metadata\":{\"haircuts\":2}}"); + } + else + { + CHECK(json_t(p1).dump() == "{\"age\":1,\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}"); + } // deserialization - auto p2 = json(p1).get(); + auto p2 = json_t(p1).template get(); CHECK(p2 == p1); // roundtrip - CHECK(T(json(p1)) == p1); - CHECK(json(T(json(p1))) == json(p1)); + CHECK(T(json_t(p1)) == p1); + CHECK(json_t(T(json_t(p1))) == json_t(p1)); // check exception in case of missing field - json j = json(p1); + json_t j = json_t(p1); j.erase("age"); - CHECK_THROWS_WITH_AS(j.get(), "[json.exception.out_of_range.403] key 'age' not found", json::out_of_range); + CHECK_THROWS_WITH_AS(j.template get(), "[json.exception.out_of_range.403] key 'age' not found", typename json_t::out_of_range); } } @@ -362,15 +285,24 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRU } } -TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/private member variables via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", T, - persons::person_with_private_alphabet, - persons::person_with_public_alphabet) +#define ALPHABET_PAIRS \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair + +TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/private member variables via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", PairT, ALPHABET_PAIRS) { + using T = typename PairT::TestedType; + using json_t = typename PairT::BasicJsonType; + SECTION("alphabet") { { T obj1; - nlohmann::json j = obj1; //via json object + json_t j = obj1; //via json object T obj2; j.get_to(obj2); bool ok = (obj1 == obj2); @@ -379,9 +311,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv { T obj1; - nlohmann::json j1 = obj1; //via json string + json_t j1 = obj1; //via json string std::string s = j1.dump(); - nlohmann::json j2 = nlohmann::json::parse(s); + json_t j2 = json_t::parse(s); T obj2; j2.get_to(obj2); bool ok = (obj1 == obj2); @@ -390,9 +322,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv { T obj1; - nlohmann::json j1 = obj1; //via msgpack - std::vector buf = nlohmann::json::to_msgpack(j1); - nlohmann::json j2 = nlohmann::json::from_msgpack(buf); + json_t j1 = obj1; //via msgpack + std::vector buf = json_t::to_msgpack(j1); + json_t j2 = json_t::from_msgpack(buf); T obj2; j2.get_to(obj2); bool ok = (obj1 == obj2); @@ -401,9 +333,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv { T obj1; - nlohmann::json j1 = obj1; //via bson - std::vector buf = nlohmann::json::to_bson(j1); - nlohmann::json j2 = nlohmann::json::from_bson(buf); + json_t j1 = obj1; //via bson + std::vector buf = json_t::to_bson(j1); + json_t j2 = json_t::from_bson(buf); T obj2; j2.get_to(obj2); bool ok = (obj1 == obj2); @@ -412,9 +344,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv { T obj1; - nlohmann::json j1 = obj1; //via cbor - std::vector buf = nlohmann::json::to_cbor(j1); - nlohmann::json j2 = nlohmann::json::from_cbor(buf); + json_t j1 = obj1; //via cbor + std::vector buf = json_t::to_cbor(j1); + json_t j2 = json_t::from_cbor(buf); T obj2; j2.get_to(obj2); bool ok = (obj1 == obj2); @@ -423,9 +355,9 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv { T obj1; - nlohmann::json j1 = obj1; //via ubjson - std::vector buf = nlohmann::json::to_ubjson(j1); - nlohmann::json j2 = nlohmann::json::from_ubjson(buf); + json_t j1 = obj1; //via ubjson + std::vector buf = json_t::to_ubjson(j1); + json_t j2 = json_t::from_ubjson(buf); T obj2; j2.get_to(obj2); bool ok = (obj1 == obj2); From b5d4cbb508cd1be6d7ae1e02fd8d114552c5a5b7 Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Tue, 20 Jul 2021 11:52:40 +0900 Subject: [PATCH 04/12] Running the CI test locally --- single_include/nlohmann/json.hpp | 26 +++++++++++++++++++------- tests/src/unit-udt_macro.cpp | 2 +- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 0c3e09c41..97a707cbe 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2607,9 +2607,15 @@ using is_detected_convertible = @def NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL @since version 3.9.2 */ -#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL(BasicJsonType, Type, ...) \ - friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ - friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL(BasicJsonType, Type, ...) \ + friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ + } \ + friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } /*! @brief macro to briefly define non-intrusive serialization of a given type to/from JSON @@ -2617,9 +2623,15 @@ using is_detected_convertible = @def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL @since version 3.9.2 */ -#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL(BasicJsonType, Type, ...) \ - inline void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ - inline void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) } +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL(BasicJsonType, Type, ...) \ + inline void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ + } \ + inline void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } /*! @brief macro to briefly define intrusive serialization of given type to/from nlohmann::json @@ -2699,7 +2711,7 @@ using is_detected_convertible = friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ { \ NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ - } \ + } /*! @brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object diff --git a/tests/src/unit-udt_macro.cpp b/tests/src/unit-udt_macro.cpp index 023e8bffb..6c91d1486 100644 --- a/tests/src/unit-udt_macro.cpp +++ b/tests/src/unit-udt_macro.cpp @@ -97,7 +97,7 @@ Visibility: \ y == other.y && \ z == other.z; \ } \ - Visibility : int a = 0; \ + Visibility : int a = 0; \ int b = 0; \ int c = 0; \ int d = 0; \ From 1e81ffb76a5f55883c37b7d5b7d0f99174654adf Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Sat, 25 Jun 2022 23:20:53 +0900 Subject: [PATCH 05/12] refactor tests, add WITH_DEFAULT for _T macros --- include/nlohmann/detail/macro_scope.hpp | 85 +++++---- single_include/nlohmann/json.hpp | 87 +++++---- tests/src/unit-udt_macro.cpp | 244 ++++++++++++++---------- 3 files changed, 246 insertions(+), 170 deletions(-) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index 04d7561e0..a369826d4 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -430,6 +430,57 @@ inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) } +#define NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, ...) \ + template \ + ReturnType to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ + } + +#define NLOHMANN_DEFINE_TYPE_T_IMPL(ReturnType, Type, ...) \ + NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \ + template \ + ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } + +#define NLOHMANN_DEFINE_TYPE_T_WITH_DEFAULT_IMPL(ReturnType, Type, ...) \ + NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \ + template \ + ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + Type nlohmann_json_default_obj; \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) \ + } + +/*! +@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object +@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T +@since version 3.10.6 +*/ +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(Type, ...) NLOHMANN_DEFINE_TYPE_T_IMPL(friend void, Type, __VA_ARGS__) + +/*! +@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object +@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T +@since version 3.10.6 +*/ +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(Type, ...) NLOHMANN_DEFINE_TYPE_T_IMPL(void, Type, __VA_ARGS__) + +/*! +@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object (works with missing fields in json) +@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T_WITH_DEFAULT +@since version 3.10.6 +*/ +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T_WITH_DEFAULT(Type, ...) NLOHMANN_DEFINE_TYPE_T_WITH_DEFAULT_IMPL(friend void, Type, __VA_ARGS__) + +/*! +@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object (works with missing fields in json) +@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T_WITH_DEFAULT +@since version 3.10.6 +*/ +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T_WITH_DEFAULT(Type, ...) NLOHMANN_DEFINE_TYPE_T_WITH_DEFAULT_IMPL(void, Type, __VA_ARGS__) // inspired from https://stackoverflow.com/a/26745591 // allows to call any std function as if (e.g. with begin): @@ -469,40 +520,6 @@ { \ } -/*! -@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object -@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T -@since version 3.9.2 -*/ -#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(Type, ...) \ - template \ - friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ - } \ - template \ - friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ - } - -/*! -@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object -@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T -@since version 3.9.2 -*/ -#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(Type, ...) \ - template \ - void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ - } \ - template \ - void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ - } - #ifndef JSON_USE_IMPLICIT_CONVERSIONS #define JSON_USE_IMPLICIT_CONVERSIONS 1 #endif diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index 97a707cbe..ab85ab028 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2657,6 +2657,57 @@ using is_detected_convertible = inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) } +#define NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, ...) \ + template \ + ReturnType to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ + } + +#define NLOHMANN_DEFINE_TYPE_T_IMPL(ReturnType, Type, ...) \ + NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \ + template \ + ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } + +#define NLOHMANN_DEFINE_TYPE_T_WITH_DEFAULT_IMPL(ReturnType, Type, ...) \ + NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \ + template \ + ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + Type nlohmann_json_default_obj; \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) \ + } + +/*! +@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object +@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T +@since version 3.10.6 +*/ +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(Type, ...) NLOHMANN_DEFINE_TYPE_T_IMPL(friend void, Type, __VA_ARGS__) + +/*! +@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object +@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T +@since version 3.10.6 +*/ +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(Type, ...) NLOHMANN_DEFINE_TYPE_T_IMPL(void, Type, __VA_ARGS__) + +/*! +@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object (works with missing fields in json) +@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T_WITH_DEFAULT +@since version 3.10.6 +*/ +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T_WITH_DEFAULT(Type, ...) NLOHMANN_DEFINE_TYPE_T_WITH_DEFAULT_IMPL(friend void, Type, __VA_ARGS__) + +/*! +@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object (works with missing fields in json) +@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T_WITH_DEFAULT +@since version 3.10.6 +*/ +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T_WITH_DEFAULT(Type, ...) NLOHMANN_DEFINE_TYPE_T_WITH_DEFAULT_IMPL(void, Type, __VA_ARGS__) // inspired from https://stackoverflow.com/a/26745591 // allows to call any std function as if (e.g. with begin): @@ -2696,40 +2747,6 @@ using is_detected_convertible = { \ } -/*! -@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object -@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T -@since version 3.9.2 -*/ -#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(Type, ...) \ - template \ - friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ - } \ - template \ - friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ - } - -/*! -@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object -@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T -@since version 3.9.2 -*/ -#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(Type, ...) \ - template \ - void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ - } \ - template \ - void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ - } - #ifndef JSON_USE_IMPLICIT_CONVERSIONS #define JSON_USE_IMPLICIT_CONVERSIONS 1 #endif @@ -18627,7 +18644,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec detail::parser_callback_tcb = nullptr, const bool allow_exceptions = true, const bool ignore_comments = false - ) + ) { return ::nlohmann::detail::parser(std::move(adapter), std::move(cb), allow_exceptions, ignore_comments); diff --git a/tests/src/unit-udt_macro.cpp b/tests/src/unit-udt_macro.cpp index 6c91d1486..c08797460 100644 --- a/tests/src/unit-udt_macro.cpp +++ b/tests/src/unit-udt_macro.cpp @@ -27,15 +27,14 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include "doctest_compatibility.h" #include #include -#include "doctest_compatibility.h" #include using nlohmann::json; -namespace persons -{ +namespace persons { #define PERSON_CLASS_BODY(ClassName, Visibility) \ Visibility: \ std::string name; \ @@ -66,63 +65,64 @@ Visibility: \ return metadata; \ } -#define ALPHABET_CLASS_BODY(ClassName, Visibility) \ - public: \ - bool operator==(const ClassName& other) const \ - { \ - return a == other.a && \ - b == other.b && \ - c == other.c && \ - d == other.d && \ - e == other.e && \ - f == other.f && \ - g == other.g && \ - h == other.h && \ - i == other.i && \ - j == other.j && \ - k == other.k && \ - l == other.l && \ - m == other.m && \ - n == other.n && \ - o == other.o && \ - p == other.p && \ - q == other.q && \ - r == other.r && \ - s == other.s && \ - t == other.t && \ - u == other.u && \ - v == other.v && \ - w == other.w && \ - x == other.x && \ - y == other.y && \ - z == other.z; \ - } \ - Visibility : int a = 0; \ - int b = 0; \ - int c = 0; \ - int d = 0; \ - int e = 0; \ - int f = 0; \ - int g = 0; \ - int h = 0; \ - int i = 0; \ - int j = 0; \ - int k = 0; \ - int l = 0; \ - int m = 0; \ - int n = 0; \ - int o = 0; \ - int p = 0; \ - int q = 0; \ - int r = 0; \ - int s = 0; \ - int t = 0; \ - int u = 0; \ - int v = 0; \ - int w = 0; \ - int x = 0; \ - int y = 0; \ - int z = 0; +#define ALPHABET_CLASS_BODY(ClassName, Visibility) \ + public: \ + bool operator==(const ClassName& other) const \ + { \ + return a == other.a && \ + b == other.b && \ + c == other.c && \ + d == other.d && \ + e == other.e && \ + f == other.f && \ + g == other.g && \ + h == other.h && \ + i == other.i && \ + j == other.j && \ + k == other.k && \ + l == other.l && \ + m == other.m && \ + n == other.n && \ + o == other.o && \ + p == other.p && \ + q == other.q && \ + r == other.r && \ + s == other.s && \ + t == other.t && \ + u == other.u && \ + v == other.v && \ + w == other.w && \ + x == other.x && \ + y == other.y && \ + z == other.z; \ + } \ +Visibility: \ + int a = 0; \ + int b = 0; \ + int c = 0; \ + int d = 0; \ + int e = 0; \ + int f = 0; \ + int g = 0; \ + int h = 0; \ + int i = 0; \ + int j = 0; \ + int k = 0; \ + int l = 0; \ + int m = 0; \ + int n = 0; \ + int o = 0; \ + int p = 0; \ + int q = 0; \ + int r = 0; \ + int s = 0; \ + int t = 0; \ + int u = 0; \ + int v = 0; \ + int w = 0; \ + int x = 0; \ + int y = 0; \ + int z = 0; class person_with_private_data { @@ -160,6 +160,12 @@ class person_t_with_private_data NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(person_t_with_private_data, age, name, metadata) }; +class person_t_with_private_data_2 +{ + PERSON_CLASS_BODY(person_t_with_private_data_2, private) + NLOHMANN_DEFINE_TYPE_INTRUSIVE_T_WITH_DEFAULT(person_t_with_private_data_2, age, name, metadata) +}; + class person_t_without_private_data_1 { PERSON_CLASS_BODY(person_t_without_private_data_1, public) @@ -172,6 +178,12 @@ class person_t_without_private_data_2 }; NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(person_t_without_private_data_2, age, name, metadata) +class person_t_without_private_data_3 +{ + PERSON_CLASS_BODY(person_t_without_private_data_3, public) +}; +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T_WITH_DEFAULT(person_t_without_private_data_3, age, name, metadata) + class person_with_private_alphabet { ALPHABET_CLASS_BODY(person_with_private_alphabet, private) @@ -184,7 +196,6 @@ class person_with_public_alphabet }; NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(person_with_public_alphabet, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) - class person_t_with_private_alphabet { ALPHABET_CLASS_BODY(person_t_with_private_alphabet, private) @@ -196,29 +207,30 @@ class person_t_with_public_alphabet ALPHABET_CLASS_BODY(person_t_with_public_alphabet, public) }; NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(person_t_with_public_alphabet, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z) -} // namespace persons +} // namespace persons // Trick described in https://github.com/onqtam/doctest/blob/master/doc/markdown/parameterized-tests.md // in note "if you need parameterization on more than 1 type" -template +template struct TestTypePair { - using TestedType = TestedType_; + using TestedType = TestedType_; using BasicJsonType = BasicJsonType_; }; -#define PERSON_PAIRS \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair +#define PERSON_TYPES_TO_TEST \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair -TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", PairT, PERSON_PAIRS) +TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", PairT, PERSON_TYPES_TO_TEST) +#undef PERSON_TYPES_TO_TEST { using T = typename PairT::TestedType; using json_t = typename PairT::BasicJsonType; @@ -227,14 +239,16 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRU { // serialization T p1("Erik", 1, {{"haircuts", 2}}); - if ( std::is_same::value ) + std::string json_string; + if (std::is_same::value) { - CHECK(json_t(p1).dump() == "{\"age\":1,\"name\":\"Erik\",\"metadata\":{\"haircuts\":2}}"); + json_string = "{\"age\":1,\"name\":\"Erik\",\"metadata\":{\"haircuts\":2}}"; } else { - CHECK(json_t(p1).dump() == "{\"age\":1,\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}"); + json_string = "{\"age\":1,\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}"; } + CHECK(json_t(p1).dump() == json_string); // deserialization auto p2 = json_t(p1).template get(); @@ -251,49 +265,77 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRU } } -TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT", T, - persons::person_with_private_data_2, - persons::person_without_private_data_3) +#define PERSON_TYPES_TO_TEST \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair + +TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT", PairT, PERSON_TYPES_TO_TEST) +#undef PERSON_TYPES_TO_TEST { + using T = typename PairT::TestedType; + using json_t = typename PairT::BasicJsonType; + SECTION("person with default values") { // serialization of default constructed object T p0; - CHECK(json(p0).dump() == "{\"age\":0,\"metadata\":null,\"name\":\"\"}"); + std::string json_string; + if (std::is_same::value) + { + json_string = "{\"age\":0,\"name\":\"\",\"metadata\":null}"; + } + else + { + json_string = "{\"age\":0,\"metadata\":null,\"name\":\"\"}"; + } + CHECK(json_t(p0).dump() == json_string); // serialization T p1("Erik", 1, {{"haircuts", 2}}); - CHECK(json(p1).dump() == "{\"age\":1,\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}"); + if (std::is_same::value) + { + json_string = "{\"age\":1,\"name\":\"Erik\",\"metadata\":{\"haircuts\":2}}"; + } + else + { + json_string = "{\"age\":1,\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}"; + } + CHECK(json_t(p1).dump() == json_string); // deserialization - auto p2 = json(p1).get(); + auto p2 = json_t(p1).template get(); CHECK(p2 == p1); // roundtrip - CHECK(T(json(p1)) == p1); - CHECK(json(T(json(p1))) == json(p1)); + CHECK(T(json_t(p1)) == p1); + CHECK(json_t(T(json_t(p1))) == json_t(p1)); // check default value in case of missing field - json j = json(p1); + json_t j = json_t(p1); j.erase("name"); j.erase("age"); j.erase("metadata"); - T p3 = j.get(); + T p3 = j.template get(); CHECK(p3.getName() == ""); CHECK(p3.getAge() == 0); CHECK(p3.getMetadata() == nullptr); } } -#define ALPHABET_PAIRS \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair +#define ALPHABET_PAIRS \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/private member variables via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", PairT, ALPHABET_PAIRS) +#undef ALPHABET_PAIRS { using T = typename PairT::TestedType; using json_t = typename PairT::BasicJsonType; @@ -302,7 +344,7 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv { { T obj1; - json_t j = obj1; //via json object + json_t j = obj1; //via json object T obj2; j.get_to(obj2); bool ok = (obj1 == obj2); @@ -311,7 +353,7 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv { T obj1; - json_t j1 = obj1; //via json string + json_t j1 = obj1; //via json string std::string s = j1.dump(); json_t j2 = json_t::parse(s); T obj2; @@ -322,7 +364,7 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv { T obj1; - json_t j1 = obj1; //via msgpack + json_t j1 = obj1; //via msgpack std::vector buf = json_t::to_msgpack(j1); json_t j2 = json_t::from_msgpack(buf); T obj2; @@ -333,7 +375,7 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv { T obj1; - json_t j1 = obj1; //via bson + json_t j1 = obj1; //via bson std::vector buf = json_t::to_bson(j1); json_t j2 = json_t::from_bson(buf); T obj2; @@ -344,7 +386,7 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv { T obj1; - json_t j1 = obj1; //via cbor + json_t j1 = obj1; //via cbor std::vector buf = json_t::to_cbor(j1); json_t j2 = json_t::from_cbor(buf); T obj2; @@ -355,7 +397,7 @@ TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/priv { T obj1; - json_t j1 = obj1; //via ubjson + json_t j1 = obj1; //via ubjson std::vector buf = json_t::to_ubjson(j1); json_t j2 = json_t::from_ubjson(buf); T obj2; From 3170fbad8b2dbe4c68ad4c1a3ab3700a9de5dd57 Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Sun, 26 Jun 2022 00:06:11 +0900 Subject: [PATCH 06/12] correct some impl details --- include/nlohmann/detail/macro_scope.hpp | 61 +++++++++---------------- single_include/nlohmann/json.hpp | 61 +++++++++---------------- 2 files changed, 44 insertions(+), 78 deletions(-) diff --git a/include/nlohmann/detail/macro_scope.hpp b/include/nlohmann/detail/macro_scope.hpp index a369826d4..756350bbc 100644 --- a/include/nlohmann/detail/macro_scope.hpp +++ b/include/nlohmann/detail/macro_scope.hpp @@ -374,36 +374,23 @@ #define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1); #define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1); -/*! -@brief macro to briefly define intrusive serialization of a given type to/from JSON -@note you can define your own specialized macroses like NLOHMANN_DEFINE_TYPE_INTRUSIVE -@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL -@since version 3.9.2 -*/ -#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL(BasicJsonType, Type, ...) \ - friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ - } \ - friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ +#define NLOHMANN_DEFINE_TYPE_TO_IMPL(ReturnType, BasicJsonType, Type, ...) \ + ReturnType to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ } - -/*! -@brief macro to briefly define non-intrusive serialization of a given type to/from JSON -@note you can define your own specialized macroses like NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE -@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL -@since version 3.9.2 -*/ -#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL(BasicJsonType, Type, ...) \ - inline void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ - } \ - inline void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ +#define NLOHMANN_DEFINE_TYPE_IMPL(ReturnType, BasicJsonType, Type, ...) \ + NLOHMANN_DEFINE_TYPE_TO_IMPL(ReturnType, BasicJsonType, Type, __VA_ARGS__) \ + ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } +#define NLOHMANN_DEFINE_TYPE_WITH_DEFAULT_IMPL(ReturnType, BasicJsonType, Type, ...) \ + NLOHMANN_DEFINE_TYPE_TO_IMPL(ReturnType, BasicJsonType, Type, __VA_ARGS__) \ + ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + Type nlohmann_json_default_obj; \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) \ } /*! @@ -412,11 +399,9 @@ @since version 3.9.0 @sa NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL */ -#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL(nlohmann::json, Type, __VA_ARGS__) +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) NLOHMANN_DEFINE_TYPE_IMPL(friend void, nlohmann::json, Type, __VA_ARGS__) -#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \ - friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ - friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) } +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) NLOHMANN_DEFINE_TYPE_WITH_DEFAULT_IMPL(friend void, nlohmann::json, Type, __VA_ARGS__) /*! @brief macro to briefly define non-intrusive serialization of given type to/from nlohmann::json @@ -424,11 +409,9 @@ @since version 3.9.0 @sa NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL */ -#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL(nlohmann::json, Type, __VA_ARGS__) +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) NLOHMANN_DEFINE_TYPE_IMPL(inline void, nlohmann::json, Type, __VA_ARGS__) -#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \ - inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ - inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) } +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) NLOHMANN_DEFINE_TYPE_WITH_DEFAULT_IMPL(inline void, nlohmann::json, Type, __VA_ARGS__) #define NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, ...) \ template \ @@ -438,7 +421,7 @@ } #define NLOHMANN_DEFINE_TYPE_T_IMPL(ReturnType, Type, ...) \ - NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \ + NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \ template \ ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ { \ @@ -446,7 +429,7 @@ } #define NLOHMANN_DEFINE_TYPE_T_WITH_DEFAULT_IMPL(ReturnType, Type, ...) \ - NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \ + NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \ template \ ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ { \ diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index ab85ab028..bc1c1a3a0 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -2601,36 +2601,23 @@ using is_detected_convertible = #define NLOHMANN_JSON_FROM(v1) nlohmann_json_j.at(#v1).get_to(nlohmann_json_t.v1); #define NLOHMANN_JSON_FROM_WITH_DEFAULT(v1) nlohmann_json_t.v1 = nlohmann_json_j.value(#v1, nlohmann_json_default_obj.v1); -/*! -@brief macro to briefly define intrusive serialization of a given type to/from JSON -@note you can define your own specialized macroses like NLOHMANN_DEFINE_TYPE_INTRUSIVE -@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL -@since version 3.9.2 -*/ -#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL(BasicJsonType, Type, ...) \ - friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ - } \ - friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ +#define NLOHMANN_DEFINE_TYPE_TO_IMPL(ReturnType, BasicJsonType, Type, ...) \ + ReturnType to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ } - -/*! -@brief macro to briefly define non-intrusive serialization of a given type to/from JSON -@note you can define your own specialized macroses like NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE -@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL -@since version 3.9.2 -*/ -#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL(BasicJsonType, Type, ...) \ - inline void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \ - } \ - inline void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ - { \ - NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ +#define NLOHMANN_DEFINE_TYPE_IMPL(ReturnType, BasicJsonType, Type, ...) \ + NLOHMANN_DEFINE_TYPE_TO_IMPL(ReturnType, BasicJsonType, Type, __VA_ARGS__) \ + ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \ + } +#define NLOHMANN_DEFINE_TYPE_WITH_DEFAULT_IMPL(ReturnType, BasicJsonType, Type, ...) \ + NLOHMANN_DEFINE_TYPE_TO_IMPL(ReturnType, BasicJsonType, Type, __VA_ARGS__) \ + ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ + { \ + Type nlohmann_json_default_obj; \ + NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) \ } /*! @@ -2639,11 +2626,9 @@ using is_detected_convertible = @since version 3.9.0 @sa NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL */ -#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) NLOHMANN_DEFINE_TYPE_INTRUSIVE_IMPL(nlohmann::json, Type, __VA_ARGS__) +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type, ...) NLOHMANN_DEFINE_TYPE_IMPL(friend void, nlohmann::json, Type, __VA_ARGS__) -#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) \ - friend void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ - friend void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) } +#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(Type, ...) NLOHMANN_DEFINE_TYPE_WITH_DEFAULT_IMPL(friend void, nlohmann::json, Type, __VA_ARGS__) /*! @brief macro to briefly define non-intrusive serialization of given type to/from nlohmann::json @@ -2651,11 +2636,9 @@ using is_detected_convertible = @since version 3.9.0 @sa NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL */ -#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_IMPL(nlohmann::json, Type, __VA_ARGS__) +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Type, ...) NLOHMANN_DEFINE_TYPE_IMPL(inline void, nlohmann::json, Type, __VA_ARGS__) -#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) \ - inline void to_json(nlohmann::json& nlohmann_json_j, const Type& nlohmann_json_t) { NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) } \ - inline void from_json(const nlohmann::json& nlohmann_json_j, Type& nlohmann_json_t) { Type nlohmann_json_default_obj; NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) } +#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(Type, ...) NLOHMANN_DEFINE_TYPE_WITH_DEFAULT_IMPL(inline void, nlohmann::json, Type, __VA_ARGS__) #define NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, ...) \ template \ @@ -2665,7 +2648,7 @@ using is_detected_convertible = } #define NLOHMANN_DEFINE_TYPE_T_IMPL(ReturnType, Type, ...) \ - NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \ + NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \ template \ ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ { \ @@ -2673,7 +2656,7 @@ using is_detected_convertible = } #define NLOHMANN_DEFINE_TYPE_T_WITH_DEFAULT_IMPL(ReturnType, Type, ...) \ - NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \ + NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \ template \ ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \ { \ From 7a09aec44a201309be6d9652bc32e23d1d37e54f Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Sun, 26 Jun 2022 01:55:25 +0900 Subject: [PATCH 07/12] add initializer for the compiler, despite not needing one --- tests/src/unit-udt_macro.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/unit-udt_macro.cpp b/tests/src/unit-udt_macro.cpp index c08797460..397b91338 100644 --- a/tests/src/unit-udt_macro.cpp +++ b/tests/src/unit-udt_macro.cpp @@ -37,7 +37,7 @@ using nlohmann::json; namespace persons { #define PERSON_CLASS_BODY(ClassName, Visibility) \ Visibility: \ - std::string name; \ + std::string name = ""; \ int age = 0; \ json metadata = nullptr; \ \ From f4adc22b382e5a5215d7fd5f6e0e396a3c55a972 Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Sun, 26 Jun 2022 02:01:27 +0900 Subject: [PATCH 08/12] Fix accidental change --- single_include/nlohmann/json.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/single_include/nlohmann/json.hpp b/single_include/nlohmann/json.hpp index bc1c1a3a0..bbc6c8a7f 100644 --- a/single_include/nlohmann/json.hpp +++ b/single_include/nlohmann/json.hpp @@ -18627,7 +18627,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec detail::parser_callback_tcb = nullptr, const bool allow_exceptions = true, const bool ignore_comments = false - ) + ) { return ::nlohmann::detail::parser(std::move(adapter), std::move(cb), allow_exceptions, ignore_comments); From 420f47fd1e872c028fc7dba6432a5779cbac6579 Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Sun, 26 Jun 2022 03:30:44 +0900 Subject: [PATCH 09/12] astyle fix, clashes with .clang-format --- tests/src/unit-udt_macro.cpp | 61 ++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/tests/src/unit-udt_macro.cpp b/tests/src/unit-udt_macro.cpp index 397b91338..ce4ebe260 100644 --- a/tests/src/unit-udt_macro.cpp +++ b/tests/src/unit-udt_macro.cpp @@ -34,23 +34,24 @@ SOFTWARE. #include using nlohmann::json; -namespace persons { +namespace persons +{ #define PERSON_CLASS_BODY(ClassName, Visibility) \ -Visibility: \ + Visibility: \ std::string name = ""; \ int age = 0; \ json metadata = nullptr; \ \ - public: \ + public: \ bool operator==(const ClassName& rhs) const \ { \ return name == rhs.name && age == rhs.age && metadata == rhs.metadata; \ } \ ClassName() = default; \ ClassName(std::string name_, int age_, json metadata_) \ - : name(std::move(name_)) \ - , age(age_) \ - , metadata(std::move(metadata_)) \ + : name(std::move(name_)) \ + , age(age_) \ + , metadata(std::move(metadata_)) \ {} \ std::string getName() const \ { \ @@ -66,7 +67,7 @@ Visibility: \ } #define ALPHABET_CLASS_BODY(ClassName, Visibility) \ - public: \ + public: \ bool operator==(const ClassName& other) const \ { \ return a == other.a && \ @@ -96,7 +97,7 @@ Visibility: \ y == other.y && \ z == other.z; \ } \ -Visibility: \ + Visibility: \ int a = 0; \ int b = 0; \ int c = 0; \ @@ -220,14 +221,14 @@ struct TestTypePair #define PERSON_TYPES_TO_TEST \ TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", PairT, PERSON_TYPES_TO_TEST) #undef PERSON_TYPES_TO_TEST @@ -265,13 +266,13 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRU } } -#define PERSON_TYPES_TO_TEST \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair +#define PERSON_TYPES_TO_TEST \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT", PairT, PERSON_TYPES_TO_TEST) #undef PERSON_TYPES_TO_TEST @@ -326,13 +327,13 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRU } } -#define ALPHABET_PAIRS \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair, \ - TestTypePair +#define ALPHABET_PAIRS \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair, \ + TestTypePair TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/private member variables via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", PairT, ALPHABET_PAIRS) #undef ALPHABET_PAIRS From 47c3eb785ec49b69b21d16d6c53aff2e0ff8b6c1 Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Sun, 26 Jun 2022 04:01:43 +0900 Subject: [PATCH 10/12] astyle is really choosy --- tests/src/unit-udt_macro.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/src/unit-udt_macro.cpp b/tests/src/unit-udt_macro.cpp index ce4ebe260..3bf4b1d34 100644 --- a/tests/src/unit-udt_macro.cpp +++ b/tests/src/unit-udt_macro.cpp @@ -41,7 +41,6 @@ namespace persons std::string name = ""; \ int age = 0; \ json metadata = nullptr; \ - \ public: \ bool operator==(const ClassName& rhs) const \ { \ From 5f822d863f2473ea92982f24374e581519245d67 Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Sun, 26 Jun 2022 11:37:23 +0900 Subject: [PATCH 11/12] fix clang tidy warnings and add nolint for certain occaisons --- tests/src/unit-udt_macro.cpp | 187 ++++++++++++++++++----------------- 1 file changed, 95 insertions(+), 92 deletions(-) diff --git a/tests/src/unit-udt_macro.cpp b/tests/src/unit-udt_macro.cpp index 3bf4b1d34..519dae905 100644 --- a/tests/src/unit-udt_macro.cpp +++ b/tests/src/unit-udt_macro.cpp @@ -36,92 +36,95 @@ using nlohmann::json; namespace persons { -#define PERSON_CLASS_BODY(ClassName, Visibility) \ - Visibility: \ - std::string name = ""; \ - int age = 0; \ - json metadata = nullptr; \ - public: \ - bool operator==(const ClassName& rhs) const \ - { \ - return name == rhs.name && age == rhs.age && metadata == rhs.metadata; \ - } \ - ClassName() = default; \ - ClassName(std::string name_, int age_, json metadata_) \ - : name(std::move(name_)) \ - , age(age_) \ - , metadata(std::move(metadata_)) \ - {} \ - std::string getName() const \ - { \ - return name; \ - } \ - int getAge() const \ - { \ - return age; \ - } \ - json getMetadata() const \ - { \ - return metadata; \ +#define PERSON_CLASS_BODY(ClassName, Visibility) \ + /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \ + Visibility: \ + /* NOLINTNEXTLINE(readability-redundant-string-init): collides with -Weffc++ */ \ + std::string name = ""; \ + int age = 0; \ + json metadata = nullptr; \ + public: \ + bool operator==(const ClassName& rhs) const \ + { \ + return name == rhs.name && age == rhs.age && metadata == rhs.metadata; \ + } \ + ClassName() = default; \ + ClassName(std::string name_, int age_, json metadata_) \ + : name(std::move(name_)) \ + , age(age_) \ + , metadata(std::move(metadata_)) \ + {} \ + std::string getName() const \ + { \ + return name; \ + } \ + int getAge() const \ + { \ + return age; \ + } \ + json getMetadata() const \ + { \ + return metadata; \ } -#define ALPHABET_CLASS_BODY(ClassName, Visibility) \ - public: \ - bool operator==(const ClassName& other) const \ - { \ - return a == other.a && \ - b == other.b && \ - c == other.c && \ - d == other.d && \ - e == other.e && \ - f == other.f && \ - g == other.g && \ - h == other.h && \ - i == other.i && \ - j == other.j && \ - k == other.k && \ - l == other.l && \ - m == other.m && \ - n == other.n && \ - o == other.o && \ - p == other.p && \ - q == other.q && \ - r == other.r && \ - s == other.s && \ - t == other.t && \ - u == other.u && \ - v == other.v && \ - w == other.w && \ - x == other.x && \ - y == other.y && \ - z == other.z; \ - } \ - Visibility: \ - int a = 0; \ - int b = 0; \ - int c = 0; \ - int d = 0; \ - int e = 0; \ - int f = 0; \ - int g = 0; \ - int h = 0; \ - int i = 0; \ - int j = 0; \ - int k = 0; \ - int l = 0; \ - int m = 0; \ - int n = 0; \ - int o = 0; \ - int p = 0; \ - int q = 0; \ - int r = 0; \ - int s = 0; \ - int t = 0; \ - int u = 0; \ - int v = 0; \ - int w = 0; \ - int x = 0; \ - int y = 0; \ +#define ALPHABET_CLASS_BODY(ClassName, Visibility) \ + public: \ + bool operator==(const ClassName& other) const \ + { \ + return a == other.a && \ + b == other.b && \ + c == other.c && \ + d == other.d && \ + e == other.e && \ + f == other.f && \ + g == other.g && \ + h == other.h && \ + i == other.i && \ + j == other.j && \ + k == other.k && \ + l == other.l && \ + m == other.m && \ + n == other.n && \ + o == other.o && \ + p == other.p && \ + q == other.q && \ + r == other.r && \ + s == other.s && \ + t == other.t && \ + u == other.u && \ + v == other.v && \ + w == other.w && \ + x == other.x && \ + y == other.y && \ + z == other.z; \ + } \ + /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \ + Visibility: \ + int a = 0; \ + int b = 0; \ + int c = 0; \ + int d = 0; \ + int e = 0; \ + int f = 0; \ + int g = 0; \ + int h = 0; \ + int i = 0; \ + int j = 0; \ + int k = 0; \ + int l = 0; \ + int m = 0; \ + int n = 0; \ + int o = 0; \ + int p = 0; \ + int q = 0; \ + int r = 0; \ + int s = 0; \ + int t = 0; \ + int u = 0; \ + int v = 0; \ + int w = 0; \ + int x = 0; \ + int y = 0; \ int z = 0; class person_with_private_data @@ -218,8 +221,8 @@ struct TestTypePair using BasicJsonType = BasicJsonType_; }; -#define PERSON_TYPES_TO_TEST \ - TestTypePair, \ +#define PERSON_TYPES_TO_TEST \ + TestTypePair, \ TestTypePair, \ TestTypePair, \ TestTypePair, \ @@ -242,11 +245,11 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRU std::string json_string; if (std::is_same::value) { - json_string = "{\"age\":1,\"name\":\"Erik\",\"metadata\":{\"haircuts\":2}}"; + json_string = R"str({"age":1,"name":"Erik","metadata":{"haircuts":2}})str"; } else { - json_string = "{\"age\":1,\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}"; + json_string = R"str({"age":1,"metadata":{"haircuts":2},"name":"Erik"})str"; } CHECK(json_t(p1).dump() == json_string); @@ -286,11 +289,11 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRU std::string json_string; if (std::is_same::value) { - json_string = "{\"age\":0,\"name\":\"\",\"metadata\":null}"; + json_string = R"str({"age":0,"name":"","metadata":null})str"; } else { - json_string = "{\"age\":0,\"metadata\":null,\"name\":\"\"}"; + json_string = R"str({"age":0,"metadata":null,"name":""})str"; } CHECK(json_t(p0).dump() == json_string); @@ -298,11 +301,11 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRU T p1("Erik", 1, {{"haircuts", 2}}); if (std::is_same::value) { - json_string = "{\"age\":1,\"name\":\"Erik\",\"metadata\":{\"haircuts\":2}}"; + json_string = R"str({"age":1,"name":"Erik","metadata":{"haircuts":2}})str"; } else { - json_string = "{\"age\":1,\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}"; + json_string = R"str({"age":1,"metadata":{"haircuts":2},"name":"Erik"})str"; } CHECK(json_t(p1).dump() == json_string); From 4cb97ca915a7648cceed2d508442e901ce89f162 Mon Sep 17 00:00:00 2001 From: Kunal Tyagi Date: Sun, 26 Jun 2022 21:44:04 +0900 Subject: [PATCH 12/12] disable warning for truncated decorated name --- tests/CMakeLists.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8f9240f1a..2741cb429 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -42,10 +42,11 @@ target_compile_features(test_main PRIVATE cxx_std_11) target_compile_options(test_main PUBLIC $<$:/EHsc;$<$:/Od>> # MSVC: Force to always compile with W4 + # Disable warning C4503: ...: decorated name length (4096) exceeded, name was truncated # Disable warning C4566: character represented by universal-character-name '\uFF01' # cannot be represented in the current code page (1252) # Disable warning C4996: 'nlohmann::basic_json<...>::operator <<': was declared deprecated - $<$:/W4 /wd4566 /wd4996> + $<$:/W4 /wd4503 /wd4566 /wd4996> # https://github.com/nlohmann/json/issues/1114 $<$:/bigobj> $<$:-Wa,-mbig-obj>