json.go 762 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package json
  2. import (
  3. "github.com/bytedance/sonic"
  4. "github.com/go-kratos/kratos/v2/encoding"
  5. )
  6. // Name is the name registered for the json codec.
  7. const Name = "json"
  8. var json = sonic.Config{
  9. EscapeHTML: true,
  10. }.Froze()
  11. func init() {
  12. encoding.RegisterCodec(codec{})
  13. }
  14. // codec is a Codec implementation with json.
  15. type codec struct{}
  16. func (codec) Marshal(v interface{}) ([]byte, error) {
  17. return json.Marshal(v)
  18. }
  19. func (codec) Unmarshal(data []byte, v interface{}) error {
  20. //rv := reflect.ValueOf(v)
  21. //for rv.Kind() == reflect.Ptr {
  22. // if rv.IsNil() && rv.CanInterface() {
  23. // rv.Set(reflect.New(rv.Type().Elem()))
  24. // v = rv.Interface()
  25. // }
  26. // rv = rv.Elem()
  27. //}
  28. return json.Unmarshal(data, v)
  29. }
  30. func (codec) Name() string {
  31. return Name
  32. }