2019-01-10 09:42:02 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-06-09 11:50:50 +08:00
|
|
|
|
|
|
|
"github.com/golang/protobuf/jsonpb"
|
|
|
|
"github.com/golang/protobuf/proto"
|
2019-01-10 09:42:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Marshaler struct{}
|
|
|
|
|
|
|
|
func (j Marshaler) Marshal(v interface{}) ([]byte, error) {
|
|
|
|
return json.Marshal(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j Marshaler) Unmarshal(d []byte, v interface{}) error {
|
2019-06-09 11:50:50 +08:00
|
|
|
if pb, ok := v.(proto.Message); ok {
|
|
|
|
return jsonpb.UnmarshalString(string(d), pb)
|
|
|
|
}
|
2019-01-10 09:42:02 +00:00
|
|
|
return json.Unmarshal(d, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j Marshaler) String() string {
|
|
|
|
return "json"
|
|
|
|
}
|