Merge pull request #570 from sunfuze/grpc-json-marshal

grpc: using jsonpb.Marshaler to do Marshal, map to jsonpb.Unmarsh
This commit is contained in:
Asim Aslam 2019-07-08 08:44:51 +01:00 committed by GitHub
commit 5cd1e81ba9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 0 deletions

View File

@ -22,6 +22,8 @@ type protoCodec struct{}
type bytesCodec struct{}
type wrapCodec struct{ encoding.Codec }
var jsonpbMarshaler = &jsonpb.Marshaler{}
var (
defaultGRPCCodecs = map[string]encoding.Codec{
"application/json": jsonCodec{},
@ -113,6 +115,12 @@ func (bytesCodec) 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)
}

View File

@ -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)
}