feature_reflect_marshaler.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package jsoniter
  2. import (
  3. "github.com/v2pro/plz/reflect2"
  4. "unsafe"
  5. "encoding"
  6. "encoding/json"
  7. )
  8. type marshalerEncoder struct {
  9. checkIsEmpty checkIsEmpty
  10. valType reflect2.Type
  11. }
  12. func (encoder *marshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  13. obj := encoder.valType.UnsafeIndirect(ptr)
  14. if encoder.valType.IsNullable() && reflect2.IsNil(obj) {
  15. stream.WriteNil()
  16. return
  17. }
  18. marshaler := obj.(json.Marshaler)
  19. bytes, err := marshaler.MarshalJSON()
  20. if err != nil {
  21. stream.Error = err
  22. } else {
  23. stream.Write(bytes)
  24. }
  25. }
  26. func (encoder *marshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  27. return encoder.checkIsEmpty.IsEmpty(ptr)
  28. }
  29. type directMarshalerEncoder struct {
  30. checkIsEmpty checkIsEmpty
  31. }
  32. func (encoder *directMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  33. marshaler := *(*json.Marshaler)(ptr)
  34. if marshaler == nil {
  35. stream.WriteNil()
  36. return
  37. }
  38. bytes, err := marshaler.MarshalJSON()
  39. if err != nil {
  40. stream.Error = err
  41. } else {
  42. stream.Write(bytes)
  43. }
  44. }
  45. func (encoder *directMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  46. return encoder.checkIsEmpty.IsEmpty(ptr)
  47. }
  48. type textMarshalerEncoder struct {
  49. valType reflect2.Type
  50. checkIsEmpty checkIsEmpty
  51. }
  52. func (encoder *textMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  53. obj := encoder.valType.UnsafeIndirect(ptr)
  54. if encoder.valType.IsNullable() && reflect2.IsNil(obj) {
  55. stream.WriteNil()
  56. return
  57. }
  58. marshaler := (obj).(encoding.TextMarshaler)
  59. bytes, err := marshaler.MarshalText()
  60. if err != nil {
  61. stream.Error = err
  62. } else {
  63. stream.WriteString(string(bytes))
  64. }
  65. }
  66. func (encoder *textMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  67. return encoder.checkIsEmpty.IsEmpty(ptr)
  68. }
  69. type directTextMarshalerEncoder struct {
  70. checkIsEmpty checkIsEmpty
  71. }
  72. func (encoder *directTextMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  73. marshaler := *(*encoding.TextMarshaler)(ptr)
  74. if marshaler == nil {
  75. stream.WriteNil()
  76. return
  77. }
  78. bytes, err := marshaler.MarshalText()
  79. if err != nil {
  80. stream.Error = err
  81. } else {
  82. stream.WriteString(string(bytes))
  83. }
  84. }
  85. func (encoder *directTextMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  86. return encoder.checkIsEmpty.IsEmpty(ptr)
  87. }
  88. type unmarshalerDecoder struct {
  89. templateInterface emptyInterface
  90. }
  91. func (decoder *unmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  92. templateInterface := decoder.templateInterface
  93. templateInterface.word = ptr
  94. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  95. unmarshaler := (*realInterface).(json.Unmarshaler)
  96. iter.nextToken()
  97. iter.unreadByte() // skip spaces
  98. bytes := iter.SkipAndReturnBytes()
  99. err := unmarshaler.UnmarshalJSON(bytes)
  100. if err != nil {
  101. iter.ReportError("unmarshalerDecoder", err.Error())
  102. }
  103. }
  104. type textUnmarshalerDecoder struct {
  105. templateInterface emptyInterface
  106. }
  107. func (decoder *textUnmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  108. templateInterface := decoder.templateInterface
  109. templateInterface.word = ptr
  110. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  111. unmarshaler := (*realInterface).(encoding.TextUnmarshaler)
  112. str := iter.ReadString()
  113. err := unmarshaler.UnmarshalText([]byte(str))
  114. if err != nil {
  115. iter.ReportError("textUnmarshalerDecoder", err.Error())
  116. }
  117. }