Skip to content

Fix zero-member serialization macros#5278

Open
dark-ous wants to merge 1 commit into
nlohmann:developfrom
dark-ous:fix-empty-macro
Open

Fix zero-member serialization macros#5278
dark-ous wants to merge 1 commit into
nlohmann:developfrom
dark-ous:fix-empty-macro

Conversation

@dark-ous

Copy link
Copy Markdown

Summary

Fixes #4041 by allowing the NLOHMANN_DEFINE_TYPE_* serialization macros to be used with zero-member types.

Previously, invoking these macros on an empty type resulted in invalid macro expansion and compilation failed. With this change, zero-member types can be serialized and deserialized correctly as empty JSON objects ({}).

Example:

struct Empty
{
    NLOHMANN_DEFINE_TYPE_INTRUSIVE(Empty)
};

Changes

  • Handle zero-member macro expansion in the serialization macros.
  • Serialize zero-member types as empty JSON objects ({}).
  • Preserve the existing behavior for types with member variables.
  • Add regression tests covering zero-member intrusive and non-intrusive types.

Implementation

The implementation handles the empty variadic argument case by using ##__VA_ARGS__ to remove the trailing comma when no member arguments are supplied and introduces a no-op NLOHMANN_JSON_PASTE1 expansion for the zero-member case.

Testing


  • The changes are described in detail, both the what and why.
  • An existing issue is referenced (NLOHMANN_DEFINE_TYPE_* fails with zero members #4041).
  • Code coverage remains at 100% with regression tests for the new behavior.
  • Documentation update is not required.
  • The source code has been amalgamated by running make amalgamate.

@dark-ous
dark-ous requested a review from nlohmann as a code owner July 16, 2026 23:24
@github-actions

Copy link
Copy Markdown

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated and/or formatted correctly.

📎 A ready-to-apply patch is attached to the failed workflow run as the amalgamation-patch artifact. Download it, then apply it locally from the repository root with:

git apply amalgamation.patch

This does not require installing astyle yourself.

@dark-ous Please read and follow the Contribution Guidelines.

Signed-off-by: dark-ous <hitmanbrother233p@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NLOHMANN_DEFINE_TYPE_* fails with zero members

1 participant