Skip to content

Option for Customizing Field Omission #24

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

Closed
inoc603 opened this issue Mar 19, 2024 · 3 comments
Closed

Option for Customizing Field Omission #24

inoc603 opened this issue Mar 19, 2024 · 3 comments

Comments

@inoc603
Copy link

inoc603 commented Mar 19, 2024

The new omitzero behavior is very nice, but it can only be defined using tags. Can we introduce a new option to modify the behavior, e.g. treat omitempty as omitzero, so structs imported from another package can be marshaled with the new omitzero behavior?

@dsnet
Copy link
Collaborator

dsnet commented Mar 21, 2024

I think something like this is reasonable. Probably an option like "OmitZeroStructFields".

@dsnet
Copy link
Collaborator

dsnet commented Apr 17, 2024

I analyzed all public Go code known by the proxy as of 2024-04-01 for existing json struct tags. This is the number of each combination of tags:

Percent Count Tag combination
49.9% 26260569 name,omitempty
44.2% 23290292 name
5.4% 2821440 -
0.3% 136470 ,inline
0.2% 114903 ,omitempty
0.0% 14071
0.0% 6261 ,omitempty,inline
0.0% 266 name,inline
0.0% 115 name,omitzero
0.0% 97 name,omitzero,omitempty
0.0% 68 name,omitempty,inline
0.0% 1 ,format

It seems that omitempty or omitzero is present in about half of all fields with an explicit json tag.

EDIT: I removed occurrences within test files.

@dsnet
Copy link
Collaborator

dsnet commented Nov 20, 2024

#61 adds a caller-specified option to omit all zero struct fields.

@dsnet dsnet closed this as completed Nov 20, 2024
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

No branches or pull requests

2 participants