Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions include/nlohmann/detail/macro_scope.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@
NLOHMANN_JSON_PASTE4, \
NLOHMANN_JSON_PASTE3, \
NLOHMANN_JSON_PASTE2, \
NLOHMANN_JSON_PASTE1)(__VA_ARGS__))
NLOHMANN_JSON_PASTE1, \
NLOHMANN_JSON_PASTE0)(__VA_ARGS__))
#define NLOHMANN_JSON_PASTE0(func)
Comment on lines +342 to +344
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Route empty member lists to the zero-arg paste branch

Adding NLOHMANN_JSON_PASTE0 here does not make zero-member NLOHMANN_DEFINE_TYPE_* calls work, because the call sites pass NLOHMANN_JSON_PASTE(NLOHMANN_JSON_TO, __VA_ARGS__); when __VA_ARGS__ is empty this still leaves an empty argument after the comma, so dispatch selects NLOHMANN_JSON_PASTE2 and expands to invalid code like nlohmann_json_t.. In practice, NLOHMANN_DEFINE_TYPE_INTRUSIVE(MyType) still fails to compile, so the new tests introduced in this commit cannot build.

Useful? React with 👍 / 👎.

#define NLOHMANN_JSON_PASTE2(func, v1) func(v1)
#define NLOHMANN_JSON_PASTE3(func, v1, v2) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE2(func, v2)
#define NLOHMANN_JSON_PASTE4(func, v1, v2, v3) NLOHMANN_JSON_PASTE2(func, v1) NLOHMANN_JSON_PASTE3(func, v2, v3)
Expand Down Expand Up @@ -467,7 +469,9 @@
NLOHMANN_JSON_DOUBLE_PASTE3, \
NLOHMANN_JSON_DOUBLE_PASTE3, \
NLOHMANN_JSON_DOUBLE_PASTE1, \
NLOHMANN_JSON_DOUBLE_PASTE1)(__VA_ARGS__))
NLOHMANN_JSON_DOUBLE_PASTE1, \
NLOHMANN_JSON_DOUBLE_PASTE0)(__VA_ARGS__))
#define NLOHMANN_JSON_DOUBLE_PASTE0(func)
#define NLOHMANN_JSON_DOUBLE_PASTE3(func, v1, v2) func(v1, v2)
#define NLOHMANN_JSON_DOUBLE_PASTE5(func, v1, v2, v3, v4) NLOHMANN_JSON_DOUBLE_PASTE3(func, v1, v2) NLOHMANN_JSON_DOUBLE_PASTE3(func, v3, v4)
#define NLOHMANN_JSON_DOUBLE_PASTE7(func, v1, v2, v3, v4, v5, v6) NLOHMANN_JSON_DOUBLE_PASTE3(func, v1, v2) NLOHMANN_JSON_DOUBLE_PASTE5(func, v3, v4, v5, v6)
Expand Down
33 changes: 33 additions & 0 deletions tests/src/unit-udt_macro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,8 +778,41 @@ 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)
};

class empty_type
{
public:
NLOHMANN_DEFINE_TYPE_INTRUSIVE(empty_type)

bool operator==(const empty_type& /*unused*/) const
{
return true;
}
};

class empty_type_non_intrusive
{};

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(empty_type_non_intrusive)

} // namespace persons

TEST_CASE("NLOHMANN_DEFINE_TYPE_* with zero members")
{
SECTION("intrusive")
{
persons::empty_type v;
CHECK(json(v).dump() == "{}");
CHECK(json::parse("{}").get<persons::empty_type>() == v);
}

SECTION("non-intrusive")
{
persons::empty_type_non_intrusive v;
CHECK(json(v).dump() == "{}");
CHECK(json::parse("{}").get<persons::empty_type_non_intrusive>() == v);
}
}

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)
std::pair<nlohmann::json, persons::person_with_private_data>,
std::pair<nlohmann::json, persons::person_without_private_data_1>,
Expand Down
Loading