Protojson will marshal int64 as string instead of int, which is a known issue (protocolbuffers/protobuf#2679) that won't be solved for now due to json standard. This is especially painful when we want to emit default value of 0 for int64 field, but they all become "0".
This gist offered a workaround based on gogoproto
. The trick is that gogoproto
implemented its own protojson and is compatible with protobuf.The implementation code is in a single file that you can directly copy to your code and update it, without leaving your code repository or contributing to github. Also it's quite easy to adjust the marshaler into what we wanted in that single file. Check below for detailed steps.
- get
gogoproto
go get github.com/gogo/protobuf
- copy jsonpb.go into your code: https://github.com/gogo/protobuf/blob/master/jsonpb/jsonpb.go#L739