json.go 774 B

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