From d4d579bd5ad5125cab0d017c993a3c384c347f44 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 cfa97f9..7562a04 100644 --- a/codec.go +++ b/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) }