From fb97097ef6e3f2f8a2a5e3c853c4e8516e295b5c 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 --- codec.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/codec.go b/codec.go index 62f3038..fe59c36 100644 --- a/codec.go +++ b/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) }