From e88041dc26eeaf32db22014255c7286ceae838d2 Mon Sep 17 00:00:00 2001 From: Joe Date: Thu, 4 Jul 2019 16:43:36 +0800 Subject: [PATCH] if unmarshal target is proto.Message, using jsonpb --- client/grpc/codec.go | 5 +++++ server/grpc/codec.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/client/grpc/codec.go b/client/grpc/codec.go index 62f30384..fe59c361 100644 --- a/client/grpc/codec.go +++ b/client/grpc/codec.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "github.com/golang/protobuf/jsonpb" "github.com/golang/protobuf/proto" "github.com/json-iterator/go" "github.com/micro/go-micro/codec" @@ -114,6 +115,10 @@ func (jsonCodec) Marshal(v interface{}) ([]byte, error) { } func (jsonCodec) Unmarshal(data []byte, v interface{}) error { + if pb, ok := v.(proto.Message); ok { + return jsonpb.UnmarshalString(string(data), pb) + } + return json.Unmarshal(data, v) } diff --git a/server/grpc/codec.go b/server/grpc/codec.go index cfa97f92..7562a044 100644 --- a/server/grpc/codec.go +++ b/server/grpc/codec.go @@ -5,6 +5,7 @@ import ( "fmt" "strings" + "github.com/golang/protobuf/jsonpb" "github.com/golang/protobuf/proto" "github.com/micro/go-micro/codec" "github.com/micro/go-micro/codec/bytes" @@ -79,6 +80,10 @@ func (jsonCodec) Marshal(v interface{}) ([]byte, error) { } func (jsonCodec) Unmarshal(data []byte, v interface{}) error { + if pb, ok := v.(proto.Message); ok { + return jsonpb.UnmarshalString(string(data), pb) + } + return json.Unmarshal(data, v) }