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) \ { \