Protobuf: Dealing with json omitempty tags in proto3

Created on 30 Nov 2015  路  3Comments  路  Source: golang/protobuf

We use a combination of protobuf and json in our platform. The current protobuf implementation will add "omitempty" tags to all json fields. This can be problematic as go base values will not show up in the json.

For example:
message Response {
bool success = 1;
int64 count = 2;
}

if success == false and count == 0, they will not show up in the json even though those are perfectly valid values.

I'm not sure what he solution is here but it would be nice to have an option to not omitempty.

Most helpful comment

@skadilover use jsonpb package with EmitDefault set to true, found here: https://godoc.org/github.com/golang/protobuf/jsonpb#Marshaler

All 3 comments

The officially support mechanism for rendering protos as JSON is to use the included jsonpb package.

@dsymonds Could u describle it more clearly? How to solve the problem?

@skadilover use jsonpb package with EmitDefault set to true, found here: https://godoc.org/github.com/golang/protobuf/jsonpb#Marshaler

Was this page helpful?
0 / 5 - 0 ratings