JSON marshal is a combination of golang jsonbp and JSONBuiltin marshaler.
This marshaler doesn't ignore json tags such as json:"myName"
or json:"-"
gogoproto
in proto file
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
RemoveEmpty
overrides EmitDefaults
option.
It can be used to remove json:"something,omitempty"
tags generated by some generators
in main file
mux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONCustom{RemoveEmpty:true}))
proto file
message User{
name string = 1 [(gogoproto.jsontag) = "NAME,removeEmpty"];
}
This feature can be used to nest fields in the resulting JSON while maintaining a flat structure in code.
proto file
message Msg {
Firstname string = 1 [(gogoproto.jsontag) = "name.firstname"];
PseudoFirstname string = 2 [(gogoproto.jsontag) = "lastname"];
EmbedMsg = 3 [(gogoproto.nullable) = false, (gogoproto.embed) = true];
Lastname string = 4 [(gogoproto.jsontag) = "name.lastname"];
Inside string = 5 [(gogoproto.jsontag) = "name.inside.a.b.c"];
}
message EmbedMsg{
Opt1 string = 1 [(gogoproto.jsontag) = "opt1"];
}
msgInput := Msg{
Firstname: "One",
Lastname: "Two",
PseudoFirstname: "Three",
EmbedMsg: EmbedMsg{
Opt1: "var",
},
Inside: "goo",
}
{
"lastname": "Three",
"name": {
"firstname": "One",
"inside": {
"a": {
"b": {
"c": "goo"
}
}
},
"lastname": "Two"
},
"opt1": "var"
}
IncomingToOutgoingHeaderKey transforms incoming gRPC context into outgoing. It can be used when proxying gRPC requests.
someClient.someMethod(IncomingToOutgoingContext(ctx), &someMsg{})