Skip to content

Commit 1e81ffb

Browse files
committed
refactor tests, add WITH_DEFAULT for _T macros
1 parent b5d4cbb commit 1e81ffb

File tree

3 files changed

+247
-171
lines changed

3 files changed

+247
-171
lines changed

include/nlohmann/detail/macro_scope.hpp

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,57 @@
430430
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__)) } \
431431
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__)) }
432432

433+
#define NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, ...) \
434+
template<typename BasicJsonType> \
435+
ReturnType to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \
436+
{ \
437+
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
438+
}
439+
440+
#define NLOHMANN_DEFINE_TYPE_T_IMPL(ReturnType, Type, ...) \
441+
NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \
442+
template<typename BasicJsonType> \
443+
ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \
444+
{ \
445+
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
446+
}
447+
448+
#define NLOHMANN_DEFINE_TYPE_T_WITH_DEFAULT_IMPL(ReturnType, Type, ...) \
449+
NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \
450+
template<typename BasicJsonType> \
451+
ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \
452+
{ \
453+
Type nlohmann_json_default_obj; \
454+
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) \
455+
}
456+
457+
/*!
458+
@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object
459+
@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T
460+
@since version 3.10.6
461+
*/
462+
#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(Type, ...) NLOHMANN_DEFINE_TYPE_T_IMPL(friend void, Type, __VA_ARGS__)
463+
464+
/*!
465+
@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object
466+
@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T
467+
@since version 3.10.6
468+
*/
469+
#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(Type, ...) NLOHMANN_DEFINE_TYPE_T_IMPL(void, Type, __VA_ARGS__)
470+
471+
/*!
472+
@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object (works with missing fields in json)
473+
@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T_WITH_DEFAULT
474+
@since version 3.10.6
475+
*/
476+
#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T_WITH_DEFAULT(Type, ...) NLOHMANN_DEFINE_TYPE_T_WITH_DEFAULT_IMPL(friend void, Type, __VA_ARGS__)
477+
478+
/*!
479+
@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object (works with missing fields in json)
480+
@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T_WITH_DEFAULT
481+
@since version 3.10.6
482+
*/
483+
#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T_WITH_DEFAULT(Type, ...) NLOHMANN_DEFINE_TYPE_T_WITH_DEFAULT_IMPL(void, Type, __VA_ARGS__)
433484

434485
// inspired from https://stackoverflow.com/a/26745591
435486
// allows to call any std function as if (e.g. with begin):
@@ -469,40 +520,6 @@
469520
{ \
470521
}
471522

472-
/*!
473-
@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object
474-
@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T
475-
@since version 3.9.2
476-
*/
477-
#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(Type, ...) \
478-
template<typename BasicJsonType> \
479-
friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \
480-
{ \
481-
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
482-
} \
483-
template<typename BasicJsonType> \
484-
friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \
485-
{ \
486-
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
487-
}
488-
489-
/*!
490-
@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object
491-
@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T
492-
@since version 3.9.2
493-
*/
494-
#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(Type, ...) \
495-
template<typename BasicJsonType> \
496-
void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \
497-
{ \
498-
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
499-
} \
500-
template<typename BasicJsonType> \
501-
void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \
502-
{ \
503-
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
504-
}
505-
506523
#ifndef JSON_USE_IMPLICIT_CONVERSIONS
507524
#define JSON_USE_IMPLICIT_CONVERSIONS 1
508525
#endif

single_include/nlohmann/json.hpp

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,6 +2657,57 @@ using is_detected_convertible =
26572657
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__)) } \
26582658
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__)) }
26592659

2660+
#define NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, ...) \
2661+
template<typename BasicJsonType> \
2662+
ReturnType to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \
2663+
{ \
2664+
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
2665+
}
2666+
2667+
#define NLOHMANN_DEFINE_TYPE_T_IMPL(ReturnType, Type, ...) \
2668+
NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \
2669+
template<typename BasicJsonType> \
2670+
ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \
2671+
{ \
2672+
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
2673+
}
2674+
2675+
#define NLOHMANN_DEFINE_TYPE_T_WITH_DEFAULT_IMPL(ReturnType, Type, ...) \
2676+
NLOHMANN_DEFINE_TYPE_T_TO_IMPL(ReturnType, Type, __VA_ARGS__) \
2677+
template<typename BasicJsonType> \
2678+
ReturnType from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \
2679+
{ \
2680+
Type nlohmann_json_default_obj; \
2681+
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM_WITH_DEFAULT, __VA_ARGS__)) \
2682+
}
2683+
2684+
/*!
2685+
@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object
2686+
@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T
2687+
@since version 3.10.6
2688+
*/
2689+
#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(Type, ...) NLOHMANN_DEFINE_TYPE_T_IMPL(friend void, Type, __VA_ARGS__)
2690+
2691+
/*!
2692+
@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object
2693+
@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T
2694+
@since version 3.10.6
2695+
*/
2696+
#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(Type, ...) NLOHMANN_DEFINE_TYPE_T_IMPL(void, Type, __VA_ARGS__)
2697+
2698+
/*!
2699+
@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object (works with missing fields in json)
2700+
@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T_WITH_DEFAULT
2701+
@since version 3.10.6
2702+
*/
2703+
#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T_WITH_DEFAULT(Type, ...) NLOHMANN_DEFINE_TYPE_T_WITH_DEFAULT_IMPL(friend void, Type, __VA_ARGS__)
2704+
2705+
/*!
2706+
@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object (works with missing fields in json)
2707+
@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T_WITH_DEFAULT
2708+
@since version 3.10.6
2709+
*/
2710+
#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T_WITH_DEFAULT(Type, ...) NLOHMANN_DEFINE_TYPE_T_WITH_DEFAULT_IMPL(void, Type, __VA_ARGS__)
26602711

26612712
// inspired from https://stackoverflow.com/a/26745591
26622713
// allows to call any std function as if (e.g. with begin):
@@ -2696,40 +2747,6 @@ using is_detected_convertible =
26962747
{ \
26972748
}
26982749

2699-
/*!
2700-
@brief macro to briefly define intrusive serialization of a given type to/from any basic_json object
2701-
@def NLOHMANN_DEFINE_TYPE_INTRUSIVE_T
2702-
@since version 3.9.2
2703-
*/
2704-
#define NLOHMANN_DEFINE_TYPE_INTRUSIVE_T(Type, ...) \
2705-
template<typename BasicJsonType> \
2706-
friend void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \
2707-
{ \
2708-
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
2709-
} \
2710-
template<typename BasicJsonType> \
2711-
friend void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \
2712-
{ \
2713-
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
2714-
}
2715-
2716-
/*!
2717-
@brief macro to briefly define non-intrusive serialization of a given type to/from any basic_json object
2718-
@def NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T
2719-
@since version 3.9.2
2720-
*/
2721-
#define NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_T(Type, ...) \
2722-
template<typename BasicJsonType> \
2723-
void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \
2724-
{ \
2725-
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
2726-
} \
2727-
template<typename BasicJsonType> \
2728-
void from_json(const BasicJsonType& nlohmann_json_j, Type& nlohmann_json_t) \
2729-
{ \
2730-
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_FROM, __VA_ARGS__)) \
2731-
}
2732-
27332750
#ifndef JSON_USE_IMPLICIT_CONVERSIONS
27342751
#define JSON_USE_IMPLICIT_CONVERSIONS 1
27352752
#endif
@@ -18627,7 +18644,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
1862718644
detail::parser_callback_t<basic_json>cb = nullptr,
1862818645
const bool allow_exceptions = true,
1862918646
const bool ignore_comments = false
18630-
)
18647+
)
1863118648
{
1863218649
return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
1863318650
std::move(cb), allow_exceptions, ignore_comments);

0 commit comments

Comments
 (0)