Skip to content
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

Add flag to control pb struct tag omitempty #1371

Closed
astraw99 opened this issue Oct 8, 2021 · 3 comments
Closed

Add flag to control pb struct tag omitempty #1371

astraw99 opened this issue Oct 8, 2021 · 3 comments

Comments

@astraw99
Copy link

astraw99 commented Oct 8, 2021

Is your feature request related to a problem? Please describe.
In the code, the omitempty tag is hard-coded:

tag := fmt.Sprintf("protobuf:%s json:%q", g.goTag(message, field, wiretype), jsonName+",omitempty")

thus it will shadow some of zero field values, like
errCode int32 = 1, if errCode = 0 then it will not be showed in the output.

Describe the solution you'd like
Hope add tag flag like below is preferred:

message test {
 bool someone = 1 [json_omitempty=false];
}

Describe alternatives you've considered
A hack method is:

sed -i "" -e "s/,omitempty//g" ./api/proto/*.go

Obviously it is not elegant, hope pb add flag officially, thanks.

@neild
Copy link
Contributor

neild commented Oct 8, 2021

The supported way to marshal a protobuf message as JSON is the protojson package, which follows the canonical protobuf JSON mapping.

You can use the protojson.MarshalOptions.EmitUnpopulated configuration setting to control whether zero-valued fields are emitted in the JSON encoding.

The json struct tags are deprecated. They date back to the early days of the Go protobuf implementation, before a cross-language canonical JSON mapping existed. We still generate them to avoid breaking users who depend on them, but we are no longer making any changes to these tags.

@neild
Copy link
Contributor

neild commented Nov 4, 2021

Closing: The json: struct tags exist for backwards compatibility, but no new code should rely on them and we do not intend to add new features related to them.

@neild neild closed this as completed Nov 4, 2021
@astraw99
Copy link
Author

astraw99 commented Nov 5, 2021

Got it, thanks.

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