refactor tests, add WITH_DEFAULT for _T macros
This commit is contained in:
parent
b5d4cbb508
commit
1e81ffb76a
@ -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<typename BasicJsonType> \
|
||||
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<typename BasicJsonType> \
|
||||
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<typename BasicJsonType> \
|
||||
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<typename BasicJsonType> \
|
||||
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<typename BasicJsonType> \
|
||||
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<typename BasicJsonType> \
|
||||
void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \
|
||||
{ \
|
||||
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
|
||||
} \
|
||||
template<typename BasicJsonType> \
|
||||
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
|
||||
|
||||
@ -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<typename BasicJsonType> \
|
||||
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<typename BasicJsonType> \
|
||||
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<typename BasicJsonType> \
|
||||
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<typename BasicJsonType> \
|
||||
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<typename BasicJsonType> \
|
||||
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<typename BasicJsonType> \
|
||||
void to_json(BasicJsonType& nlohmann_json_j, const Type& nlohmann_json_t) \
|
||||
{ \
|
||||
NLOHMANN_JSON_EXPAND(NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__)) \
|
||||
} \
|
||||
template<typename BasicJsonType> \
|
||||
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_t<basic_json>cb = nullptr,
|
||||
const bool allow_exceptions = true,
|
||||
const bool ignore_comments = false
|
||||
)
|
||||
)
|
||||
{
|
||||
return ::nlohmann::detail::parser<basic_json, InputAdapterType>(std::move(adapter),
|
||||
std::move(cb), allow_exceptions, ignore_comments);
|
||||
|
||||
@ -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 <string>
|
||||
#include <vector>
|
||||
#include "doctest_compatibility.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
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 <typename TestedType_, typename BasicJsonType_ = nlohmann::json>
|
||||
template<typename TestedType_, typename BasicJsonType_ = nlohmann::json>
|
||||
struct TestTypePair
|
||||
{
|
||||
using TestedType = TestedType_;
|
||||
using TestedType = TestedType_;
|
||||
using BasicJsonType = BasicJsonType_;
|
||||
};
|
||||
|
||||
#define PERSON_PAIRS \
|
||||
TestTypePair<persons::person_with_private_data>, \
|
||||
TestTypePair<persons::person_without_private_data_1>, \
|
||||
TestTypePair<persons::person_without_private_data_2>, \
|
||||
TestTypePair<persons::person_t_with_private_data, nlohmann::json>, \
|
||||
TestTypePair<persons::person_t_without_private_data_1, nlohmann::json>, \
|
||||
TestTypePair<persons::person_t_without_private_data_2, nlohmann::json>, \
|
||||
TestTypePair<persons::person_t_with_private_data, nlohmann::ordered_json>, \
|
||||
TestTypePair<persons::person_t_without_private_data_1, nlohmann::ordered_json>, \
|
||||
TestTypePair<persons::person_t_without_private_data_2, nlohmann::ordered_json>
|
||||
#define PERSON_TYPES_TO_TEST \
|
||||
TestTypePair<persons::person_with_private_data>, \
|
||||
TestTypePair<persons::person_without_private_data_1>, \
|
||||
TestTypePair<persons::person_without_private_data_2>, \
|
||||
TestTypePair<persons::person_t_with_private_data>, \
|
||||
TestTypePair<persons::person_t_without_private_data_1>, \
|
||||
TestTypePair<persons::person_t_without_private_data_2>, \
|
||||
TestTypePair<persons::person_t_with_private_data, nlohmann::ordered_json>, \
|
||||
TestTypePair<persons::person_t_without_private_data_1, nlohmann::ordered_json>, \
|
||||
TestTypePair<persons::person_t_without_private_data_2, nlohmann::ordered_json>
|
||||
|
||||
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<json_t, nlohmann::ordered_json>::value )
|
||||
std::string json_string;
|
||||
if (std::is_same<json_t, nlohmann::ordered_json>::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<T>();
|
||||
@ -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<persons::person_with_private_data_2>, \
|
||||
TestTypePair<persons::person_without_private_data_3>, \
|
||||
TestTypePair<persons::person_t_with_private_data_2>, \
|
||||
TestTypePair<persons::person_t_without_private_data_3>, \
|
||||
TestTypePair<persons::person_t_with_private_data_2, nlohmann::ordered_json>, \
|
||||
TestTypePair<persons::person_t_without_private_data_3, nlohmann::ordered_json>
|
||||
|
||||
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<json_t, nlohmann::ordered_json>::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<json_t, nlohmann::ordered_json>::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<T>();
|
||||
auto p2 = json_t(p1).template get<T>();
|
||||
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>();
|
||||
T p3 = j.template get<T>();
|
||||
CHECK(p3.getName() == "");
|
||||
CHECK(p3.getAge() == 0);
|
||||
CHECK(p3.getMetadata() == nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
#define ALPHABET_PAIRS \
|
||||
TestTypePair<persons::person_with_private_alphabet>, \
|
||||
TestTypePair<persons::person_with_public_alphabet>, \
|
||||
TestTypePair<persons::person_t_with_private_alphabet, nlohmann::json>, \
|
||||
TestTypePair<persons::person_t_with_public_alphabet, nlohmann::json>, \
|
||||
TestTypePair<persons::person_t_with_private_alphabet, nlohmann::ordered_json>, \
|
||||
TestTypePair<persons::person_t_with_public_alphabet, nlohmann::ordered_json>
|
||||
#define ALPHABET_PAIRS \
|
||||
TestTypePair<persons::person_with_private_alphabet>, \
|
||||
TestTypePair<persons::person_with_public_alphabet>, \
|
||||
TestTypePair<persons::person_t_with_private_alphabet, nlohmann::json>, \
|
||||
TestTypePair<persons::person_t_with_public_alphabet, nlohmann::json>, \
|
||||
TestTypePair<persons::person_t_with_private_alphabet, nlohmann::ordered_json>, \
|
||||
TestTypePair<persons::person_t_with_public_alphabet, nlohmann::ordered_json>
|
||||
|
||||
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<uint8_t> 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<uint8_t> 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<uint8_t> 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<uint8_t> buf = json_t::to_ubjson(j1);
|
||||
json_t j2 = json_t::from_ubjson(buf);
|
||||
T obj2;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user