mirror of
https://github.com/nlohmann/json.git
synced 2026-07-12 05:25:09 +00:00
Support zero-member types in NLOHMANN_DEFINE_TYPE_* macros (#4041)
NLOHMANN_DEFINE_TYPE_INTRUSIVE(Type) and its 11 sibling macros produced broken code for types with no members to serialize. Invoking a variadic macro so __VA_ARGS__ is empty is only standard-conforming since C++20, so a plain __VA_OPT__ fix (as tried in #5142) breaks every pre-C++20 build under -pedantic. Instead, make all 12 macros purely variadic and dispatch on argument count using a sentinel-padded extension of the existing NLOHMANN_JSON_GET_MACRO idiom, giving full C++11-C++26 support with no feature-test gate. Verified against real GCC 16 and Clang at -std=c++11/14/17/20 with -pedantic -Werror -Wvariadic-macros: zero regressions in the existing unit-udt_macro.cpp suite plus 12 new zero-member test cases. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
This commit is contained in:
@@ -778,6 +778,124 @@ class derived_person_only_serialize_private_3 : person_without_default_construct
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE_WITH_NAMES(derived_person_only_serialize_private_3, person_without_default_constructor_3, "json_hair_color", hair_color)
|
||||
};
|
||||
|
||||
// Zero-member types for issue #4041: NLOHMANN_DEFINE_TYPE_* and
|
||||
// NLOHMANN_DEFINE_DERIVED_TYPE_* must compile and produce a valid (empty)
|
||||
// JSON object when no member arguments are given.
|
||||
class empty_intrusive
|
||||
{
|
||||
public:
|
||||
bool operator==(const empty_intrusive& /*rhs*/) const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE(empty_intrusive)
|
||||
};
|
||||
|
||||
class empty_intrusive_with_default
|
||||
{
|
||||
public:
|
||||
bool operator==(const empty_intrusive_with_default& /*rhs*/) const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT(empty_intrusive_with_default)
|
||||
};
|
||||
|
||||
class empty_intrusive_only_serialize
|
||||
{
|
||||
public:
|
||||
NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE(empty_intrusive_only_serialize)
|
||||
};
|
||||
|
||||
class empty_non_intrusive
|
||||
{
|
||||
public:
|
||||
bool operator==(const empty_non_intrusive& /*rhs*/) const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
// NOLINTNEXTLINE(misc-use-internal-linkage)
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(empty_non_intrusive)
|
||||
|
||||
class empty_non_intrusive_with_default
|
||||
{
|
||||
public:
|
||||
bool operator==(const empty_non_intrusive_with_default& /*rhs*/) const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
};
|
||||
// NOLINTNEXTLINE(misc-use-internal-linkage)
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(empty_non_intrusive_with_default)
|
||||
|
||||
class empty_non_intrusive_only_serialize {};
|
||||
// NOLINTNEXTLINE(misc-use-internal-linkage)
|
||||
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(empty_non_intrusive_only_serialize)
|
||||
|
||||
class empty_derived_intrusive : public person_with_private_data
|
||||
{
|
||||
public:
|
||||
empty_derived_intrusive() = default;
|
||||
empty_derived_intrusive(std::string name_, int age_, json metadata_)
|
||||
: person_with_private_data(std::move(name_), age_, std::move(metadata_))
|
||||
{}
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE(empty_derived_intrusive, person_with_private_data)
|
||||
};
|
||||
|
||||
class empty_derived_intrusive_with_default : public person_with_private_data
|
||||
{
|
||||
public:
|
||||
empty_derived_intrusive_with_default() = default;
|
||||
empty_derived_intrusive_with_default(std::string name_, int age_, json metadata_)
|
||||
: person_with_private_data(std::move(name_), age_, std::move(metadata_))
|
||||
{}
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT(empty_derived_intrusive_with_default, person_with_private_data)
|
||||
};
|
||||
|
||||
class empty_derived_intrusive_only_serialize : public person_with_private_data
|
||||
{
|
||||
public:
|
||||
empty_derived_intrusive_only_serialize() = default;
|
||||
empty_derived_intrusive_only_serialize(std::string name_, int age_, json metadata_)
|
||||
: person_with_private_data(std::move(name_), age_, std::move(metadata_))
|
||||
{}
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE(empty_derived_intrusive_only_serialize, person_with_private_data)
|
||||
};
|
||||
|
||||
class empty_derived_non_intrusive : public person_with_private_data
|
||||
{
|
||||
public:
|
||||
empty_derived_non_intrusive() = default;
|
||||
empty_derived_non_intrusive(std::string name_, int age_, json metadata_)
|
||||
: person_with_private_data(std::move(name_), age_, std::move(metadata_))
|
||||
{}
|
||||
};
|
||||
// NOLINTNEXTLINE(misc-use-internal-linkage)
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE(empty_derived_non_intrusive, person_with_private_data)
|
||||
|
||||
class empty_derived_non_intrusive_with_default : public person_with_private_data
|
||||
{
|
||||
public:
|
||||
empty_derived_non_intrusive_with_default() = default;
|
||||
empty_derived_non_intrusive_with_default(std::string name_, int age_, json metadata_)
|
||||
: person_with_private_data(std::move(name_), age_, std::move(metadata_))
|
||||
{}
|
||||
};
|
||||
// NOLINTNEXTLINE(misc-use-internal-linkage)
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT(empty_derived_non_intrusive_with_default, person_with_private_data)
|
||||
|
||||
class empty_derived_non_intrusive_only_serialize : public person_with_private_data
|
||||
{
|
||||
public:
|
||||
empty_derived_non_intrusive_only_serialize() = default;
|
||||
empty_derived_non_intrusive_only_serialize(std::string name_, int age_, json metadata_)
|
||||
: person_with_private_data(std::move(name_), age_, std::move(metadata_))
|
||||
{}
|
||||
};
|
||||
// NOLINTNEXTLINE(misc-use-internal-linkage)
|
||||
NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE(empty_derived_non_intrusive_only_serialize, person_with_private_data)
|
||||
|
||||
} // namespace persons
|
||||
|
||||
TEST_CASE_TEMPLATE("Serialization/deserialization via NLOHMANN_DEFINE_TYPE_INTRUSIVE and NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE", Pair, // NOLINT(readability-math-missing-parentheses, bugprone-throwing-static-initialization)
|
||||
@@ -1191,3 +1309,108 @@ TEST_CASE_TEMPLATE("Serialization of non-default-constructible classes via NLOHM
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Regression tests for issue #4041: NLOHMANN_DEFINE_TYPE_* and
|
||||
// NLOHMANN_DEFINE_DERIVED_TYPE_* macros must compile and produce valid
|
||||
// (empty, or base-only for the derived case) JSON objects when no member
|
||||
// arguments are given, on every supported C++ standard.
|
||||
TEST_CASE_TEMPLATE("Serialization/deserialization of zero-member types via NLOHMANN_DEFINE_TYPE_* (issue #4041)", Json, // NOLINT(readability-math-missing-parentheses, bugprone-throwing-static-initialization)
|
||||
nlohmann::json, nlohmann::ordered_json)
|
||||
{
|
||||
constexpr bool is_ordered = std::is_same<Json, nlohmann::ordered_json>::value;
|
||||
const char* const derived_dump = is_ordered
|
||||
? R"({"age":1,"name":"Erik","metadata":null})"
|
||||
: R"({"age":1,"metadata":null,"name":"Erik"})";
|
||||
|
||||
SECTION("NLOHMANN_DEFINE_TYPE_INTRUSIVE with zero members")
|
||||
{
|
||||
persons::empty_intrusive obj{};
|
||||
Json j = obj;
|
||||
CHECK(j.dump() == "{}");
|
||||
CHECK(j.template get<persons::empty_intrusive>() == obj);
|
||||
}
|
||||
|
||||
SECTION("NLOHMANN_DEFINE_TYPE_INTRUSIVE_WITH_DEFAULT with zero members")
|
||||
{
|
||||
persons::empty_intrusive_with_default obj{};
|
||||
Json j = obj;
|
||||
CHECK(j.dump() == "{}");
|
||||
CHECK(j.template get<persons::empty_intrusive_with_default>() == obj);
|
||||
}
|
||||
|
||||
SECTION("NLOHMANN_DEFINE_TYPE_INTRUSIVE_ONLY_SERIALIZE with zero members")
|
||||
{
|
||||
persons::empty_intrusive_only_serialize obj{};
|
||||
Json j = obj;
|
||||
CHECK(j.dump() == "{}");
|
||||
}
|
||||
|
||||
SECTION("NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE with zero members")
|
||||
{
|
||||
persons::empty_non_intrusive obj{};
|
||||
Json j = obj;
|
||||
CHECK(j.dump() == "{}");
|
||||
CHECK(j.template get<persons::empty_non_intrusive>() == obj);
|
||||
}
|
||||
|
||||
SECTION("NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT with zero members")
|
||||
{
|
||||
persons::empty_non_intrusive_with_default obj{};
|
||||
Json j = obj;
|
||||
CHECK(j.dump() == "{}");
|
||||
CHECK(j.template get<persons::empty_non_intrusive_with_default>() == obj);
|
||||
}
|
||||
|
||||
SECTION("NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE with zero members")
|
||||
{
|
||||
persons::empty_non_intrusive_only_serialize obj{};
|
||||
Json j = obj;
|
||||
CHECK(j.dump() == "{}");
|
||||
}
|
||||
|
||||
SECTION("NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE with zero own members")
|
||||
{
|
||||
persons::empty_derived_intrusive obj{"Erik", 1, nullptr};
|
||||
Json j = obj;
|
||||
CHECK(j.dump() == derived_dump);
|
||||
CHECK(j.template get<persons::empty_derived_intrusive>() == obj);
|
||||
}
|
||||
|
||||
SECTION("NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_WITH_DEFAULT with zero own members")
|
||||
{
|
||||
persons::empty_derived_intrusive_with_default obj{"Erik", 1, nullptr};
|
||||
Json j = obj;
|
||||
CHECK(j.dump() == derived_dump);
|
||||
CHECK(j.template get<persons::empty_derived_intrusive_with_default>() == obj);
|
||||
}
|
||||
|
||||
SECTION("NLOHMANN_DEFINE_DERIVED_TYPE_INTRUSIVE_ONLY_SERIALIZE with zero own members")
|
||||
{
|
||||
persons::empty_derived_intrusive_only_serialize obj{"Erik", 1, nullptr};
|
||||
Json j = obj;
|
||||
CHECK(j.dump() == derived_dump);
|
||||
}
|
||||
|
||||
SECTION("NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE with zero own members")
|
||||
{
|
||||
persons::empty_derived_non_intrusive obj{"Erik", 1, nullptr};
|
||||
Json j = obj;
|
||||
CHECK(j.dump() == derived_dump);
|
||||
CHECK(j.template get<persons::empty_derived_non_intrusive>() == obj);
|
||||
}
|
||||
|
||||
SECTION("NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_WITH_DEFAULT with zero own members")
|
||||
{
|
||||
persons::empty_derived_non_intrusive_with_default obj{"Erik", 1, nullptr};
|
||||
Json j = obj;
|
||||
CHECK(j.dump() == derived_dump);
|
||||
CHECK(j.template get<persons::empty_derived_non_intrusive_with_default>() == obj);
|
||||
}
|
||||
|
||||
SECTION("NLOHMANN_DEFINE_DERIVED_TYPE_NON_INTRUSIVE_ONLY_SERIALIZE with zero own members")
|
||||
{
|
||||
persons::empty_derived_non_intrusive_only_serialize obj{"Erik", 1, nullptr};
|
||||
Json j = obj;
|
||||
CHECK(j.dump() == derived_dump);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user