diff --git a/config/config.go b/config/config.go index 42df7fb0a..bd77a2e32 100644 --- a/config/config.go +++ b/config/config.go @@ -7,9 +7,6 @@ import ( "sync" "time" - "github.com/go-kratos/kratos/v2/encoding" - // init json encoder - _ "github.com/go-kratos/kratos/v2/encoding/json" "github.com/go-kratos/kratos/v2/log" ) @@ -20,8 +17,6 @@ var ( ErrTypeAssert = errors.New("type assert error") _ Config = (*config)(nil) - - codec = encoding.GetCodec("json") ) // Observer is config observer. @@ -125,7 +120,7 @@ func (c *config) Scan(v interface{}) error { if err != nil { return err } - return codec.Unmarshal(data, v) + return unmarshalJSON(data, v) } func (c *config) Watch(key string, o Observer) error { diff --git a/config/reader.go b/config/reader.go index 601557eeb..637a5f524 100644 --- a/config/reader.go +++ b/config/reader.go @@ -1,10 +1,13 @@ package config import ( + "encoding/json" "fmt" "strings" "github.com/imdario/mergo" + "google.golang.org/protobuf/encoding/protojson" + "google.golang.org/protobuf/proto" ) // Reader is config reader. @@ -71,16 +74,16 @@ func (r *reader) Value(path string) (Value, bool) { } func (r *reader) Source() ([]byte, error) { - return codec.Marshal(r.values) + return marshalJSON(r.values) } func cloneMap(src map[string]interface{}) (map[string]interface{}, error) { - data, err := codec.Marshal(src) + data, err := marshalJSON(src) if err != nil { return nil, err } dst := make(map[string]interface{}) - if err = codec.Unmarshal(data, &dst); err != nil { + if err = unmarshalJSON(data, &dst); err != nil { return nil, err } return dst, nil @@ -104,3 +107,17 @@ func convertMap(src interface{}) interface{} { return src } } + +func marshalJSON(v interface{}) ([]byte, error) { + if m, ok := v.(proto.Message); ok { + return protojson.Marshal(m) + } + return json.Marshal(v) +} + +func unmarshalJSON(data []byte, v interface{}) error { + if m, ok := v.(proto.Message); ok { + return protojson.Unmarshal(data, m) + } + return json.Unmarshal(data, v) +} diff --git a/encoding/json/json.go b/encoding/json/json.go index 7f78063a2..1f57c2599 100644 --- a/encoding/json/json.go +++ b/encoding/json/json.go @@ -5,25 +5,11 @@ import ( "reflect" "github.com/go-kratos/kratos/v2/encoding" - - "google.golang.org/protobuf/encoding/protojson" - "google.golang.org/protobuf/proto" ) // Name is the name registered for the json codec. const Name = "json" -var ( - // MarshalOptions is a configurable JSON format marshaler. - MarshalOptions = protojson.MarshalOptions{ - EmitUnpopulated: true, - } - // UnmarshalOptions is a configurable JSON format parser. - UnmarshalOptions = protojson.UnmarshalOptions{ - DiscardUnknown: true, - } -) - func init() { encoding.RegisterCodec(codec{}) } @@ -32,24 +18,18 @@ func init() { type codec struct{} func (codec) Marshal(v interface{}) ([]byte, error) { - if m, ok := v.(proto.Message); ok { - return MarshalOptions.Marshal(m) - } return json.Marshal(v) } func (codec) Unmarshal(data []byte, v interface{}) error { rv := reflect.ValueOf(v) for rv.Kind() == reflect.Ptr { - if rv.IsNil() { + if rv.IsNil() && rv.CanSet() { rv.Set(reflect.New(rv.Type().Elem())) v = rv.Interface() } rv = rv.Elem() } - if m, ok := v.(proto.Message); ok { - return UnmarshalOptions.Unmarshal(data, m) - } return json.Unmarshal(data, v) }