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
Question: How can I get JSONPB to omit fields #373
Comments
json tag is still for encoding with the Also if EmitDefaults with jsonpb does not work, you should create an issue on golang/protobuf. |
Hey @awalterschulze Thanks for the response! Here's my version of $ protoc --version
libprotoc 3.5.1 I'll remove the JSONTAG stuff tonight and try again. That said, the "json" tag does seem to get picked up, because I'm also using it to fix case (ie: json: someField, swapping to camelCase, which is more Javascript-y). From what I can tell, the library just seems to ignore the "-" option. I'll try it again tonight. |
Looking forward to your feedback. I just want to eliminate some variables, before trying to load everything in my head. |
Actually to clarify, I'm trying to get the Capacity field to not appear when marshaled to JSON at all. We use GOGO to generate models that we use internally between services, and externally when serialized to JSON by our gateway. Externally, some fields (like capacity) don't make any sense, so we don't want them to actually appear in the resulting JSON. Perhaps what I need to do is read more on the Also to clarify, I'm using this package: |
Ah ok. I don't think the jsonpb package is made to not marshal fields, except maybe omitempty. |
Hrm, ok. Yep, that's going to be a little gnarly. Before I do this (and I'll happily share the code) is there a good way to do this besides rewriting marshalObject? https://github.com/gogo/protobuf/blob/master/jsonpb/jsonpb.go#L151 |
So sorry, I did not initially understand that you were not looking for the semantics of So I still think implementing the JSONPBMarshaler marshaler is your best bet, but I agree its not ideal, given that the methods you want to call back into like marshalObject are all private. I guess another way is to give your field a name with a prefix If you also don't want to protoMarshal the field then there is another option using Another way is to use All 4 of these solutions are not ideal and pretty advanced. I personally don't think its a good fit, but maybe one of these will work for you. |
Thanks! Yeah I still haven't figured out a plan. The custom marshaller seems like the only option. We like the jsonpb package for its ability to deal with enums and we use these fields internally with gRPC. The XXX hack is interesting, but it's a bummer to name a field this way. |
Using the
gogo/jsonpb
package, is there a way to omit a field when marshaling to JSON?My proto looks like this:
My generated file looks like:
And I use this to marshal:
I'm probably missing something obvious, but I can't seem to find the right invocation :)
The text was updated successfully, but these errors were encountered: