Skip to content

Implement support for OmitZeroStructFields #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2024
Merged

Conversation

dsnet
Copy link
Collaborator

@dsnet dsnet commented Nov 20, 2024

By default, Marshal serializes Go structs with all fields even if each field is the zero Go value.
This can lead to verbose output emitting fields for which it is often indistinguishable from the zero value of the field itself.

The caller-specified OmitZeroStructFields is equivalent to specifying the omitzero tag option on every Go struct field. Static analysis of Go code used with JSON indicates that about 50% of all struct fields have omitempty specified. While omitempty is not the same as omitzero, it is probably safer to provide a caller-scoped OmitZeroStructFields option since doing so is more likely to produce output that is semantically equivalent to if the option was not specified at all.

The exceptions to the above is if the field value contains a -0.0 or if it implements IsZero that treats certain values other than the zero Go value as "zero".

By default, Marshal serializes Go structs with all fields
even if each field is the zero Go value.
This can lead to verbose output emitting fields for which it is
often indistinguishable from the zero value of the field itself.

The caller-specified OmitZeroStructFields is equivalent to
specifying the omitzero tag option on every Go struct field.
Static analysis of Go code used with JSON indicates that
about 50% of all struct fields have `omitempty` specified.
While `omitempty` is not the same as `omitzero`, it is probably
safer to provide a caller-scoped OmitZeroStructFields option since
doing so is more likely to produce output that is semantically
equivalent to if the option was not specified at all.

The exceptions to the above is if the field value contains a -0.0
or if it implements IsZero that treats certain values other than
the zero Go value as "zero".
Copy link
Collaborator

@mvdan mvdan left a comment

Choose a reason for hiding this comment

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

Do we want something similar for omitempty, or not because we expect most omitempty users to switch to omitzero with v2?

@dsnet
Copy link
Collaborator Author

dsnet commented Nov 20, 2024

It's not clear to me where you would want a global OmitEmpty over OmitZero. We can certainly add it in the future if someone brings a use-case.

@dsnet dsnet merged commit 2c9e0bd into master Nov 20, 2024
@dsnet dsnet deleted the omitzero-option branch November 20, 2024 17:46
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.

None yet

2 participants