json.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package jsonpb
  2. //
  3. //import (
  4. // "bytes"
  5. // "encoding/json"
  6. // "reflect"
  7. //
  8. // "github.com/gogo/protobuf/proto"
  9. //
  10. // "github.com/golang/protobuf/jsonpb"
  11. //
  12. // "github.com/go-kratos/kratos/v2/encoding"
  13. //)
  14. //
  15. //// Name is the name registered for the json codec.
  16. //const Name = "json"
  17. //
  18. //var jsonpbMarshaler *jsonpb.Marshaler
  19. //
  20. //func init() {
  21. // jsonpbMarshaler = &jsonpb.Marshaler{
  22. // EnumsAsInts: true,
  23. // EmitDefaults: true,
  24. // OrigName: true,
  25. // }
  26. // encoding.RegisterCodec(codec{})
  27. //}
  28. //
  29. //// codec is a Codec implementation with json.
  30. //type codec struct{}
  31. //
  32. //func (codec) Marshal(v interface{}) ([]byte, error) {
  33. // if _, ok := v.(proto.Message); ok {
  34. // var buf bytes.Buffer
  35. // err := jsonpbMarshaler.Marshal(&buf, v.(proto.Message))
  36. // if err != nil {
  37. // return nil, err
  38. // }
  39. // return buf.Bytes(), nil
  40. // }
  41. // return json.Marshal(v)
  42. //}
  43. //
  44. //func (codec) Unmarshal(data []byte, v interface{}) error {
  45. // rv := reflect.ValueOf(v)
  46. // for rv.Kind() == reflect.Ptr {
  47. // if rv.IsNil() && rv.CanInterface() {
  48. // rv.Set(reflect.New(rv.Type().Elem()))
  49. // v = rv.Interface()
  50. // }
  51. // rv = rv.Elem()
  52. // }
  53. // return json.Unmarshal(data, v)
  54. //}
  55. //
  56. //func (codec) Name() string {
  57. // return Name
  58. //}