unmarshal json with jsonpb if accepter is pb

This commit is contained in:
magodo
2019-06-09 11:50:50 +08:00
parent 95b8147fa1
commit 3573ac818f
2 changed files with 19 additions and 6 deletions

View File

@@ -2,6 +2,9 @@ package json
import (
"encoding/json"
"github.com/golang/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
)
type Marshaler struct{}
@@ -11,6 +14,9 @@ func (j Marshaler) Marshal(v interface{}) ([]byte, error) {
}
func (j Marshaler) Unmarshal(d []byte, v interface{}) error {
if pb, ok := v.(proto.Message); ok {
return jsonpb.UnmarshalString(string(d), pb)
}
return json.Unmarshal(d, v)
}