diff --git a/codec.go b/codec.go index 7362f49..53de36c 100644 --- a/codec.go +++ b/codec.go @@ -23,6 +23,8 @@ type bytesCodec struct{} type protoCodec struct{} type wrapCodec struct{ encoding.Codec } +var jsonpbMarshaler = &jsonpb.Marshaler{} + var ( defaultGRPCCodecs = map[string]encoding.Codec{ "application/json": jsonCodec{}, @@ -78,6 +80,12 @@ func (protoCodec) Name() string { } func (jsonCodec) Marshal(v interface{}) ([]byte, error) { + if pb, ok := v.(proto.Message); ok { + s, err := jsonpbMarshaler.MarshalToString(pb) + + return []byte(s), err + } + return json.Marshal(v) }