Search code examples
gogrpcprotoc

Proto3 JSON not converting field name to camelCase


Prot3 documentation says that by default JSON printer should convert the field name to lowerCamelCase and use that as the JSON name, but I did not observe this in my files:

Lng float32 `protobuf:"fixed32,1,opt,name=Lng,proto3" json:"Lng,omitempty"`
Lat float32 `protobuf:"fixed32,2,opt,name=Lat,proto3" json:"Lat,omitempty"`
message Coordinate {
    float Lng = 1;
    float Lat = 2;
}

Is there an explicit syntax that would do this? I am using protoc-gen-go v1.25.0 and protoc v3.13.0


Solution

  • I have the same problem with my Protos. so I found the solution is in the name of variables, you should change your proto message variables to this :

    message Coordinate {
        float lng = 1;
        float lat = 2;
    }
    

    the protobuf generates this :

    Lng float32 `protobuf:"fixed32,1,opt,name=lng,proto3" json:"lng,omitempty"`
    Lat float32 `protobuf:"fixed32,2,opt,name=lat,proto3" json:"lat,omitempty"`
    
    

    if you have a multi-word variable, you should change it to this:

        float location_lat = 1;
    

    so the generated result will be :

    LocationLat float32 `protobuf:"fixed32,1,opt,name=location_lat,proto3" json:"location_lat,omitempty"`