Skip to content

Conversation

@nsd20463
Copy link
Contributor

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.

omitzero was added to the go stdlib encoder/json in go 1.24
and does accept omitempty in the go stdlib encoder/json.
@nsd20463 nsd20463 changed the title Support for jsson:",omitzero" tag Support for jason:",omitzero" tag Sep 26, 2025
@nsd20463 nsd20463 changed the title Support for jason:",omitzero" tag Support for json:",omitzero" tag Sep 26, 2025
tests/data.go Outdated

// NOTE: omitzero DOES have effect on non-pointer struct fields.
SubZ, SubNZ SubStruct `json:",omitzero"`
SubPZ, SubPNZ *SubStruct `json:",omitzero"`
Copy link
Contributor

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

Copy link
Contributor Author

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)
@nsd20463
Copy link
Contributor Author

While adding more test cases I realized that combining omitempty and omitzero together generated code which didn't pass go vet, so the most recent commit fixes that by treating it as a special case in the encoder generator.

Copy link
Contributor

@neal neal left a comment

Choose a reason for hiding this comment

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

LGTM

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