encoding/json: remove proto json (#753)

* remove protojson encoding
pull/755/head
Tony Chen 4 years ago committed by GitHub
parent f526d6b975
commit 28009889bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      config/config.go
  2. 23
      config/reader.go
  3. 22
      encoding/json/json.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 {

@ -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)
}

@ -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)
}

Loading…
Cancel
Save