// Result example: // type Post struct { // Number int64 `protobuf:"bytes,1,opt,name=number,json=no1,proto3" json:"no2"` // } message Post { int64 number = 1 [json_name="no1", (gogoproto.jsontag) = "no2"]; }
json_name + jsonpb
上个方案中已经出现了json_name,官方的说明如下,具体见参考链接
Generates JSON objects. Message field names are mapped to lowerCamelCase and become JSON object keys. If the json_name field option is specified, the specified value will be used as the key instead. Parsers accept both the lowerCamelCase name (or the one specified by the json_name option) and the original proto field name. null is an accepted value for all field types and treated as the default value of the corresponding field type.
json_name并不会影响 protoc-gen-go 生成的go结构体中的json tag,而是会在 protobuf tag 中生成指定的json name 它的用途是在protobuf->json时被应用,而要让它起作用,encoding/json包是不行的,它只认json tag。要使用 github.com/golang/protobuf/jsonpb 看如下例子理解 定义proto文件,强行在Blog中塞进了不同的字段命名格式