mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-18 08:26:38 +02:00
26 lines
465 B
Go
26 lines
465 B
Go
package json
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/golang/protobuf/jsonpb"
|
|
"github.com/golang/protobuf/proto"
|
|
)
|
|
|
|
type Marshaler struct{}
|
|
|
|
func (j Marshaler) Marshal(v interface{}) ([]byte, error) {
|
|
return json.Marshal(v)
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func (j Marshaler) String() string {
|
|
return "json"
|
|
}
|