| 12345678910111213141516171819202122232425262728293031323334353637383940 |
- package json
- import (
- "github.com/bytedance/sonic"
- "github.com/go-kratos/kratos/v2/encoding"
- )
- // Name is the name registered for the json codec.
- const Name = "json"
- var json = sonic.Config{
- EscapeHTML: true,
- }.Froze()
- func init() {
- encoding.RegisterCodec(codec{})
- }
- // codec is a Codec implementation with json.
- type codec struct{}
- func (codec) Marshal(v interface{}) ([]byte, error) {
- return json.Marshal(v)
- }
- func (codec) Unmarshal(data []byte, v interface{}) error {
- //rv := reflect.ValueOf(v)
- //for rv.Kind() == reflect.Ptr {
- // if rv.IsNil() && rv.CanInterface() {
- // rv.Set(reflect.New(rv.Type().Elem()))
- // v = rv.Interface()
- // }
- // rv = rv.Elem()
- //}
- return json.Unmarshal(data, v)
- }
- func (codec) Name() string {
- return Name
- }
|