Skip to content

Instantly share code, notes, and snippets.

View NickYadance's full-sized avatar
:octocat:
Looking for any cool stuff

nICK NickYadance

:octocat:
Looking for any cool stuff
  • Shanghai
View GitHub Profile
@NickYadance
NickYadance / gist:0396614a973016a457583937d02e995d
Last active October 13, 2025 10:41
Workaround for protojson to marshal int64 as int instead of string

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.

  1. get gogoproto

go get github.com/gogo/protobuf

  1. copy jsonpb.go into your code: https://github.com/gogo/protobuf/blob/master/jsonpb/jsonpb.go#L739