Merge pull request #565 from sunfuze/grpc-json-unmarshal

grpc: if unmarshal target is proto.Message, using jsonpb
This commit is contained in:
Asim Aslam 2019-07-04 11:16:11 +01:00 committed by GitHub
commit b35f227f7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/json-iterator/go" "github.com/json-iterator/go"
"github.com/micro/go-micro/codec" "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 { 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) return json.Unmarshal(data, v)
} }

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/micro/go-micro/codec" "github.com/micro/go-micro/codec"
"github.com/micro/go-micro/codec/bytes" "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 { 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) return json.Unmarshal(data, v)
} }