Skip to content

Add custom type handling for nullable fields in JSON serialization#2388

Closed
Zetonem wants to merge 1 commit intostephenberry:mainfrom
Zetonem:main
Closed

Add custom type handling for nullable fields in JSON serialization#2388
Zetonem wants to merge 1 commit intostephenberry:mainfrom
Zetonem:main

Conversation

@Zetonem
Copy link
Copy Markdown

@Zetonem Zetonem commented Mar 20, 2026

Summary

When a glz::object field uses glz::custom<setter, getter> and the getter returns std::optional that is std::nullopt, Glaze currently serializes the field as "field": null instead of omitting the field (under skip_null_members=true). This PR adds a regression test and fixes the JSON writer so the field is skipped when the custom getter produces a null/empty optional.

Changes

  1. Fix skip logic for JSON serialization
  • Updated the Opts.skip_null_members handling for fields whose value type is glz::custom_t.
  • For glz::custom_t, the serializer now invokes the custom getter (supports member-function/member-object pointer and invocable forms), detects whether the getter result is “null” for nullable return types (notably std::optional / optional-like via existing nullable_* concepts) and skips writing the field when the getter result is empty (std::nullopt -> omitted field)
  1. Ensure glz::custom_t is treated as a candidate for skipping

Expanded the maybe_skipped condition so that when skip_null_members=true, glz::custom_t fields are considered “potentially skippable”, allowing the runtime logic in json/write.hpp to run.

Issue

#2386

@stephenberry
Copy link
Copy Markdown
Owner

@Zetonem, thanks for submitting this. I see a few completeness issues. I'll either contribute to this PR, or open a similar one and close this. But, this is helpful to see an initial solution, thanks!

@stephenberry
Copy link
Copy Markdown
Owner

I just merged in a similar fix with #2390, so I'm closing this. But, thanks for contributing and feel free to keep posting issues and PRs!

@Zetonem
Copy link
Copy Markdown
Author

Zetonem commented Mar 22, 2026

I just merged in a similar fix with #2390, so I'm closing this. But, thanks for contributing and feel free to keep posting issues and PRs!

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants