grpc: using jsonpb.Marshaler to do Marshal, map to jsonpb.Unmarsh
This commit is contained in:
parent
5b7454e5a8
commit
b655f7f55a
@ -22,6 +22,8 @@ type protoCodec struct{}
|
|||||||
type bytesCodec struct{}
|
type bytesCodec struct{}
|
||||||
type wrapCodec struct{ encoding.Codec }
|
type wrapCodec struct{ encoding.Codec }
|
||||||
|
|
||||||
|
var jsonpbMarshaler = &jsonpb.Marshaler{}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultGRPCCodecs = map[string]encoding.Codec{
|
defaultGRPCCodecs = map[string]encoding.Codec{
|
||||||
"application/json": jsonCodec{},
|
"application/json": jsonCodec{},
|
||||||
@ -113,6 +115,12 @@ func (bytesCodec) Name() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (jsonCodec) Marshal(v interface{}) ([]byte, error) {
|
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)
|
return json.Marshal(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,6 +23,8 @@ type bytesCodec struct{}
|
|||||||
type protoCodec struct{}
|
type protoCodec struct{}
|
||||||
type wrapCodec struct{ encoding.Codec }
|
type wrapCodec struct{ encoding.Codec }
|
||||||
|
|
||||||
|
var jsonpbMarshaler = &jsonpb.Marshaler{}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
defaultGRPCCodecs = map[string]encoding.Codec{
|
defaultGRPCCodecs = map[string]encoding.Codec{
|
||||||
"application/json": jsonCodec{},
|
"application/json": jsonCodec{},
|
||||||
@ -78,6 +80,12 @@ func (protoCodec) Name() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (jsonCodec) Marshal(v interface{}) ([]byte, error) {
|
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)
|
return json.Marshal(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user