-
Notifications
You must be signed in to change notification settings - Fork 451
Support for json:",omitzero" tag #429
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
base: master
Are you sure you want to change the base?
Conversation
omitzero was added to the go stdlib encoder/json in go 1.24
and does accept omitempty in the go stdlib encoder/json.
tests/data.go
Outdated
|
|
||
| // NOTE: omitzero DOES have effect on non-pointer struct fields. | ||
| SubZ, SubNZ SubStruct `json:",omitzero"` | ||
| SubPZ, SubPNZ *SubStruct `json:",omitzero"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
may be worth also testing pointer values set with zero-value struct/string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure, why not. done.
(both should marshal to something in order to match stdlib json, and they do)
Without a special case the generated code can look like
if v != nil && v != nil {
or
if v != "" && v != "" {
and that gets flagged by go vet (go 1.25.5)
|
While adding more test cases I realized that combining omitempty and omitzero together generated code which didn't pass |
neal
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
This also fixes the
X uintptr json:",omitempty"
omitempty applied to a zero-value uintptr is treated as empty by the stdlib json.Marshal, but wasn't by easyjson.