mirror of
https://github.com/nlohmann/json.git
synced 2025-11-24 03:44:06 +08:00
Generate template functions with NLOHMANN_DEFINE_TYPE macros (#4597)
* Support any basic_json type in NLOHMANN_DEFINE_TYPE_* macros Signed-off-by: kimci86 <kimci86@hotmail.fr> * Test NLOHMANN_DEFINE_TYPE_* macros also support unordered_json Signed-off-by: kimci86 <kimci86@hotmail.fr> * Simplify test about NLOHMANN_DEFINE_TYPE_ with many arguments Signed-off-by: kimci86 <kimci86@hotmail.fr> * Remove extra scope in macros tests Signed-off-by: kimci86 <kimci86@hotmail.fr> * Remove unused test class in macros tests Signed-off-by: kimci86 <kimci86@hotmail.fr> * Update documentation about NLOHMANN_DEFINE_TYPE_* macros Signed-off-by: kimci86 <kimci86@hotmail.fr> * Fix NLOHMANN_JSON_SERIALIZE_ENUM documentation Signed-off-by: kimci86 <kimci86@hotmail.fr> * Mark some variables const in macros tests, fixes clang-tidy Signed-off-by: kimci86 <kimci86@hotmail.fr> * Workaround clang 3.5 issue with const object initialization Signed-off-by: kimci86 <kimci86@hotmail.fr> * Update highlighted lines in NLOHMANN_DEFINE_TYPE_* macros examples Signed-off-by: kimci86 <kimci86@hotmail.fr> * Fix swapped macros in documentation Signed-off-by: kimci86 <kimci86@hotmail.fr> * Remove extra backslashes at the end of macros Signed-off-by: kimci86 <kimci86@hotmail.fr> * Require basic_json type in NLOHMANN_DEFINE_TYPE_* generated functions Signed-off-by: kimci86 <kimci86@hotmail.fr> * Fix typos in macros documentation Signed-off-by: kimci86 <kimci86@hotmail.fr> --------- Signed-off-by: kimci86 <kimci86@hotmail.fr>
This commit is contained in:
@@ -326,19 +326,6 @@ class person_with_private_alphabet
|
||||
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 derived_person_with_private_alphabet : public person_with_private_alphabet
|
||||
{
|
||||
public:
|
||||
bool operator==(const derived_person_with_private_alphabet& other) const
|
||||
{
|
||||
return person_with_private_alphabet::operator==(other) && schwa == other.schwa;
|
||||
}
|
||||
|
||||
private:
|
||||
int schwa = 0;
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(derived_person_with_private_alphabet, person_with_private_alphabet, schwa)
|
||||
};
|
||||
|
||||
class person_with_public_alphabet
|
||||
{
|
||||
public:
|
||||
@@ -468,128 +455,166 @@ class derived_person_only_serialize_private : person_without_default_constructor
|
||||
|
||||
} // namespace persons
|
||||
|
||||
TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", T, // NOLINT(readability-math-missing-parentheses)
|
||||
persons::person_with_private_data,
|
||||
persons::person_without_private_data_1,
|
||||
persons::person_without_private_data_2)
|
||||
TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", Pair, // NOLINT(readability-math-missing-parentheses)
|
||||
std::pair<nlohmann::json, persons::person_with_private_data>,
|
||||
std::pair<nlohmann::json, persons::person_without_private_data_1>,
|
||||
std::pair<nlohmann::json, persons::person_without_private_data_2>,
|
||||
std::pair<nlohmann::ordered_json, persons::person_with_private_data>,
|
||||
std::pair<nlohmann::ordered_json, persons::person_without_private_data_1>,
|
||||
std::pair<nlohmann::ordered_json, persons::person_without_private_data_2>)
|
||||
{
|
||||
using Json = typename Pair::first_type;
|
||||
using T = typename Pair::second_type;
|
||||
constexpr bool is_ordered = std::is_same<Json, nlohmann::ordered_json>::value;
|
||||
|
||||
SECTION("person")
|
||||
{
|
||||
// serialization
|
||||
T p1("Erik", 1, {{"haircuts", 2}});
|
||||
CHECK(json(p1).dump() == "{\"age\":1,\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}");
|
||||
CHECK(Json(p1).dump() == (is_ordered ?
|
||||
R"({"age":1,"name":"Erik","metadata":{"haircuts":2}})" :
|
||||
R"({"age":1,"metadata":{"haircuts":2},"name":"Erik"})"));
|
||||
|
||||
// deserialization
|
||||
auto p2 = json(p1).get<T>();
|
||||
auto p2 = Json(p1).template get<T>();
|
||||
CHECK(p2 == p1);
|
||||
|
||||
// roundtrip
|
||||
CHECK(T(json(p1)) == p1);
|
||||
CHECK(json(T(json(p1))) == json(p1));
|
||||
CHECK(T(Json(p1)) == p1);
|
||||
CHECK(Json(T(Json(p1))) == Json(p1));
|
||||
|
||||
// check exception in case of missing field
|
||||
json j = json(p1);
|
||||
Json j = Json(p1);
|
||||
j.erase("age");
|
||||
CHECK_THROWS_WITH_AS(j.get<T>(), "[json.exception.out_of_range.403] key 'age' not found", json::out_of_range);
|
||||
CHECK_THROWS_WITH_AS(j.template get<T>(), "[json.exception.out_of_range.403] key 'age' not found", typename Json::out_of_range);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE and NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE", T, // NOLINT(readability-math-missing-parentheses)
|
||||
persons::derived_person_with_private_data,
|
||||
persons::derived_person_without_private_data_1,
|
||||
persons::derived_person_without_private_data_2)
|
||||
TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE and NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE", Pair, // NOLINT(readability-math-missing-parentheses)
|
||||
std::pair<nlohmann::json, persons::derived_person_with_private_data>,
|
||||
std::pair<nlohmann::json, persons::derived_person_without_private_data_1>,
|
||||
std::pair<nlohmann::json, persons::derived_person_without_private_data_2>,
|
||||
std::pair<nlohmann::ordered_json, persons::derived_person_with_private_data>,
|
||||
std::pair<nlohmann::ordered_json, persons::derived_person_without_private_data_1>,
|
||||
std::pair<nlohmann::ordered_json, persons::derived_person_without_private_data_2>)
|
||||
{
|
||||
using Json = typename Pair::first_type;
|
||||
using T = typename Pair::second_type;
|
||||
constexpr bool is_ordered = std::is_same<Json, nlohmann::ordered_json>::value;
|
||||
|
||||
SECTION("person")
|
||||
{
|
||||
// serialization
|
||||
T p1("Erik", 1, {{"haircuts", 2}}, "red");
|
||||
CHECK(json(p1).dump() == "{\"age\":1,\"hair_color\":\"red\",\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}");
|
||||
CHECK(Json(p1).dump() == (is_ordered ?
|
||||
R"({"age":1,"name":"Erik","metadata":{"haircuts":2},"hair_color":"red"})" :
|
||||
R"({"age":1,"hair_color":"red","metadata":{"haircuts":2},"name":"Erik"})"));
|
||||
|
||||
// deserialization
|
||||
auto p2 = json(p1).get<T>();
|
||||
auto p2 = Json(p1).template get<T>();
|
||||
CHECK(p2 == p1);
|
||||
|
||||
// roundtrip
|
||||
CHECK(T(json(p1)) == p1);
|
||||
CHECK(json(T(json(p1))) == json(p1));
|
||||
CHECK(T(Json(p1)) == p1);
|
||||
CHECK(Json(T(Json(p1))) == Json(p1));
|
||||
|
||||
// check exception in case of missing field
|
||||
json j = json(p1);
|
||||
Json j = Json(p1);
|
||||
j.erase("age");
|
||||
CHECK_THROWS_WITH_AS(j.get<T>(), "[json.exception.out_of_range.403] key 'age' not found", json::out_of_range);
|
||||
CHECK_THROWS_WITH_AS(j.template get<T>(), "[json.exception.out_of_range.403] key 'age' not found", typename Json::out_of_range);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT", T, // NOLINT(readability-math-missing-parentheses)
|
||||
persons::person_with_private_data_2,
|
||||
persons::person_without_private_data_3)
|
||||
TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT", Pair, // NOLINT(readability-math-missing-parentheses)
|
||||
std::pair<nlohmann::json, persons::person_with_private_data_2>,
|
||||
std::pair<nlohmann::json, persons::person_without_private_data_3>,
|
||||
std::pair<nlohmann::ordered_json, persons::person_with_private_data_2>,
|
||||
std::pair<nlohmann::ordered_json, persons::person_without_private_data_3>)
|
||||
{
|
||||
using Json = typename Pair::first_type;
|
||||
using T = typename Pair::second_type;
|
||||
constexpr bool is_ordered = std::is_same<Json, nlohmann::ordered_json>::value;
|
||||
|
||||
SECTION("person with default values")
|
||||
{
|
||||
// serialization of default constructed object
|
||||
T p0;
|
||||
CHECK(json(p0).dump() == "{\"age\":0,\"metadata\":null,\"name\":\"\"}");
|
||||
const T p0{};
|
||||
CHECK(Json(p0).dump() == (is_ordered ?
|
||||
R"({"age":0,"name":"","metadata":null})" :
|
||||
R"({"age":0,"metadata":null,"name":""})"));
|
||||
|
||||
// serialization
|
||||
T p1("Erik", 1, {{"haircuts", 2}});
|
||||
CHECK(json(p1).dump() == "{\"age\":1,\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}");
|
||||
CHECK(Json(p1).dump() == (is_ordered ?
|
||||
R"({"age":1,"name":"Erik","metadata":{"haircuts":2}})" :
|
||||
R"({"age":1,"metadata":{"haircuts":2},"name":"Erik"})"));
|
||||
|
||||
// deserialization
|
||||
auto p2 = json(p1).get<T>();
|
||||
auto p2 = Json(p1).template get<T>();
|
||||
CHECK(p2 == p1);
|
||||
|
||||
// roundtrip
|
||||
CHECK(T(json(p1)) == p1);
|
||||
CHECK(json(T(json(p1))) == json(p1));
|
||||
CHECK(T(Json(p1)) == p1);
|
||||
CHECK(Json(T(Json(p1))) == Json(p1));
|
||||
|
||||
// check default value in case of missing field
|
||||
json j = json(p1);
|
||||
Json j = Json(p1);
|
||||
j.erase("name");
|
||||
j.erase("age");
|
||||
j.erase("metadata");
|
||||
T p3 = j.get<T>();
|
||||
const T p3 = j.template get<T>();
|
||||
CHECK(p3.getName() == "");
|
||||
CHECK(p3.getAge() == 0);
|
||||
CHECK(p3.getMetadata() == nullptr);
|
||||
|
||||
// check default value in case of empty json
|
||||
const json j4;
|
||||
T p4 = j4.get<T>();
|
||||
const Json j4;
|
||||
const T p4 = j4.template get<T>();
|
||||
CHECK(p4.getName() == "");
|
||||
CHECK(p4.getAge() == 0);
|
||||
CHECK(p4.getMetadata() == nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT and NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT", T, // NOLINT(readability-math-missing-parentheses)
|
||||
persons::derived_person_with_private_data_2,
|
||||
persons::derived_person_without_private_data_3)
|
||||
TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT and NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT", Pair, // NOLINT(readability-math-missing-parentheses)
|
||||
std::pair<nlohmann::json, persons::derived_person_with_private_data_2>,
|
||||
std::pair<nlohmann::json, persons::derived_person_without_private_data_3>,
|
||||
std::pair<nlohmann::ordered_json, persons::derived_person_with_private_data_2>,
|
||||
std::pair<nlohmann::ordered_json, persons::derived_person_without_private_data_3>)
|
||||
{
|
||||
using Json = typename Pair::first_type;
|
||||
using T = typename Pair::second_type;
|
||||
constexpr bool is_ordered = std::is_same<Json, nlohmann::ordered_json>::value;
|
||||
|
||||
SECTION("derived person with default values")
|
||||
{
|
||||
// serialization of default constructed object
|
||||
T p0;
|
||||
CHECK(json(p0).dump() == "{\"age\":0,\"hair_color\":\"blue\",\"metadata\":null,\"name\":\"\"}");
|
||||
const T p0{};
|
||||
CHECK(Json(p0).dump() == (is_ordered ?
|
||||
R"({"age":0,"name":"","metadata":null,"hair_color":"blue"})" :
|
||||
R"({"age":0,"hair_color":"blue","metadata":null,"name":""})"));
|
||||
|
||||
// serialization
|
||||
T p1("Erik", 1, {{"haircuts", 2}}, "red");
|
||||
CHECK(json(p1).dump() == "{\"age\":1,\"hair_color\":\"red\",\"metadata\":{\"haircuts\":2},\"name\":\"Erik\"}");
|
||||
CHECK(Json(p1).dump() == (is_ordered ?
|
||||
R"({"age":1,"name":"Erik","metadata":{"haircuts":2},"hair_color":"red"})" :
|
||||
R"({"age":1,"hair_color":"red","metadata":{"haircuts":2},"name":"Erik"})"));
|
||||
|
||||
// deserialization
|
||||
auto p2 = json(p1).get<T>();
|
||||
auto p2 = Json(p1).template get<T>();
|
||||
CHECK(p2 == p1);
|
||||
|
||||
// roundtrip
|
||||
CHECK(T(json(p1)) == p1);
|
||||
CHECK(json(T(json(p1))) == json(p1));
|
||||
CHECK(T(Json(p1)) == p1);
|
||||
CHECK(Json(T(Json(p1))) == Json(p1));
|
||||
|
||||
// check default value in case of missing field
|
||||
json j = json(p1);
|
||||
Json j = Json(p1);
|
||||
j.erase("name");
|
||||
j.erase("age");
|
||||
j.erase("metadata");
|
||||
j.erase("hair_color");
|
||||
T p3 = j.get<T>();
|
||||
const T p3 = j.template get<T>();
|
||||
CHECK(p3.getName() == "");
|
||||
CHECK(p3.getAge() == 0);
|
||||
CHECK(p3.getMetadata() == nullptr);
|
||||
@@ -597,118 +622,81 @@ TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_DERIVED_TY
|
||||
}
|
||||
}
|
||||
|
||||
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, // NOLINT(readability-math-missing-parentheses)
|
||||
persons::person_with_private_alphabet,
|
||||
persons::person_with_public_alphabet)
|
||||
TEST_CASE_TEMPLATE("Serialization/deserialization of classes with 26 public/private member variables via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", Pair, // NOLINT(readability-math-missing-parentheses)
|
||||
std::pair<nlohmann::json, persons::person_with_private_alphabet>,
|
||||
std::pair<nlohmann::json, persons::person_with_public_alphabet>,
|
||||
std::pair<nlohmann::ordered_json, persons::person_with_private_alphabet>,
|
||||
std::pair<nlohmann::ordered_json, persons::person_with_public_alphabet>)
|
||||
{
|
||||
using Json = typename Pair::first_type;
|
||||
using T = typename Pair::second_type;
|
||||
|
||||
SECTION("alphabet")
|
||||
{
|
||||
{
|
||||
T obj1;
|
||||
nlohmann::json const j = obj1; //via json object
|
||||
T obj2;
|
||||
j.get_to(obj2);
|
||||
bool ok = (obj1 == obj2);
|
||||
CHECK(ok);
|
||||
}
|
||||
|
||||
{
|
||||
T obj1;
|
||||
nlohmann::json const j1 = obj1; //via json string
|
||||
std::string const s = j1.dump();
|
||||
nlohmann::json const j2 = nlohmann::json::parse(s);
|
||||
T obj2;
|
||||
j2.get_to(obj2);
|
||||
bool ok = (obj1 == obj2);
|
||||
CHECK(ok);
|
||||
}
|
||||
|
||||
{
|
||||
T obj1;
|
||||
nlohmann::json const j1 = obj1; //via msgpack
|
||||
std::vector<uint8_t> const buf = nlohmann::json::to_msgpack(j1);
|
||||
nlohmann::json const j2 = nlohmann::json::from_msgpack(buf);
|
||||
T obj2;
|
||||
j2.get_to(obj2);
|
||||
bool ok = (obj1 == obj2);
|
||||
CHECK(ok);
|
||||
}
|
||||
|
||||
{
|
||||
T obj1;
|
||||
nlohmann::json const j1 = obj1; //via bson
|
||||
std::vector<uint8_t> const buf = nlohmann::json::to_bson(j1);
|
||||
nlohmann::json const j2 = nlohmann::json::from_bson(buf);
|
||||
T obj2;
|
||||
j2.get_to(obj2);
|
||||
bool ok = (obj1 == obj2);
|
||||
CHECK(ok);
|
||||
}
|
||||
|
||||
{
|
||||
T obj1;
|
||||
nlohmann::json const j1 = obj1; //via cbor
|
||||
std::vector<uint8_t> const buf = nlohmann::json::to_cbor(j1);
|
||||
nlohmann::json const j2 = nlohmann::json::from_cbor(buf);
|
||||
T obj2;
|
||||
j2.get_to(obj2);
|
||||
bool ok = (obj1 == obj2);
|
||||
CHECK(ok);
|
||||
}
|
||||
|
||||
{
|
||||
T obj1;
|
||||
nlohmann::json const j1 = obj1; //via ubjson
|
||||
std::vector<uint8_t> const buf = nlohmann::json::to_ubjson(j1);
|
||||
nlohmann::json const j2 = nlohmann::json::from_ubjson(buf);
|
||||
T obj2;
|
||||
j2.get_to(obj2);
|
||||
bool ok = (obj1 == obj2);
|
||||
CHECK(ok);
|
||||
}
|
||||
T obj1;
|
||||
Json const j = obj1;
|
||||
T obj2;
|
||||
j.get_to(obj2);
|
||||
CHECK(obj1 == obj2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_TEMPLATE("Serialization of non-default-constructible classes via NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE", T, // NOLINT(readability-math-missing-parentheses)
|
||||
persons::person_without_default_constructor_1,
|
||||
persons::person_without_default_constructor_2)
|
||||
TEST_CASE_TEMPLATE("Serialization of non-default-constructible classes via NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE", Pair, // NOLINT(readability-math-missing-parentheses)
|
||||
std::pair<nlohmann::json, persons::person_without_default_constructor_1>,
|
||||
std::pair<nlohmann::json, persons::person_without_default_constructor_2>,
|
||||
std::pair<nlohmann::ordered_json, persons::person_without_default_constructor_1>,
|
||||
std::pair<nlohmann::ordered_json, persons::person_without_default_constructor_2>)
|
||||
{
|
||||
using Json = typename Pair::first_type;
|
||||
using T = typename Pair::second_type;
|
||||
constexpr bool is_ordered = std::is_same<Json, nlohmann::ordered_json>::value;
|
||||
|
||||
SECTION("person")
|
||||
{
|
||||
{
|
||||
// serialization of a single object
|
||||
T person{"Erik", 1};
|
||||
CHECK(json(person).dump() == "{\"age\":1,\"name\":\"Erik\"}");
|
||||
// serialization of a single object
|
||||
const T person{"Erik", 1};
|
||||
CHECK(Json(person).dump() == (is_ordered ?
|
||||
R"({"name":"Erik","age":1})" :
|
||||
R"({"age":1,"name":"Erik"})"));
|
||||
|
||||
// serialization of a container with objects
|
||||
std::vector<T> const two_persons
|
||||
{
|
||||
{"Erik", 1},
|
||||
{"Kyle", 2}
|
||||
};
|
||||
CHECK(json(two_persons).dump() == "[{\"age\":1,\"name\":\"Erik\"},{\"age\":2,\"name\":\"Kyle\"}]");
|
||||
}
|
||||
// serialization of a container with objects
|
||||
std::vector<T> const two_persons
|
||||
{
|
||||
{"Erik", 1},
|
||||
{"Kyle", 2}
|
||||
};
|
||||
CHECK(Json(two_persons).dump() == (is_ordered ?
|
||||
R"([{"name":"Erik","age":1},{"name":"Kyle","age":2}])" :
|
||||
R"([{"age":1,"name":"Erik"},{"age":2,"name":"Kyle"}])"));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_TEMPLATE("Serialization of non-default-constructible classes via NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE and NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE", T, // NOLINT(readability-math-missing-parentheses)
|
||||
persons::derived_person_only_serialize_public,
|
||||
persons::derived_person_only_serialize_private)
|
||||
TEST_CASE_TEMPLATE("Serialization of non-default-constructible classes via NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE and NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE", Pair, // NOLINT(readability-math-missing-parentheses)
|
||||
std::pair<nlohmann::json, persons::derived_person_only_serialize_public>,
|
||||
std::pair<nlohmann::json, persons::derived_person_only_serialize_private>,
|
||||
std::pair<nlohmann::ordered_json, persons::derived_person_only_serialize_public>,
|
||||
std::pair<nlohmann::ordered_json, persons::derived_person_only_serialize_private>)
|
||||
{
|
||||
using Json = typename Pair::first_type;
|
||||
using T = typename Pair::second_type;
|
||||
constexpr bool is_ordered = std::is_same<Json, nlohmann::ordered_json>::value;
|
||||
|
||||
SECTION("derived person only serialize")
|
||||
{
|
||||
{
|
||||
// serialization of a single object
|
||||
T person{"Erik", 1, "brown"};
|
||||
CHECK(json(person).dump() == "{\"age\":1,\"hair_color\":\"brown\",\"name\":\"Erik\"}");
|
||||
// serialization of a single object
|
||||
const T person{"Erik", 1, "brown"};
|
||||
CHECK(Json(person).dump() == (is_ordered ?
|
||||
R"({"name":"Erik","age":1,"hair_color":"brown"})" :
|
||||
R"({"age":1,"hair_color":"brown","name":"Erik"})"));
|
||||
|
||||
// serialization of a container with objects
|
||||
std::vector<T> const two_persons
|
||||
{
|
||||
{"Erik", 1, "brown"},
|
||||
{"Kyle", 2, "black"}
|
||||
};
|
||||
CHECK(json(two_persons).dump() == "[{\"age\":1,\"hair_color\":\"brown\",\"name\":\"Erik\"},{\"age\":2,\"hair_color\":\"black\",\"name\":\"Kyle\"}]");
|
||||
}
|
||||
// serialization of a container with objects
|
||||
std::vector<T> const two_persons
|
||||
{
|
||||
{"Erik", 1, "brown"},
|
||||
{"Kyle", 2, "black"}
|
||||
};
|
||||
CHECK(Json(two_persons).dump() == (is_ordered ?
|
||||
R"([{"name":"Erik","age":1,"hair_color":"brown"},{"name":"Kyle","age":2,"hair_color":"black"}])" :
|
||||
R"([{"age":1,"hair_color":"brown","name":"Erik"},{"age":2,"hair_color":"black","name":"Kyle"}])"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user