feature_reflect_marshaler.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. stringEncoder ValEncoder
  51. checkIsEmpty checkIsEmpty
  52. }
  53. func (encoder *textMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  54. obj := encoder.valType.UnsafeIndirect(ptr)
  55. if encoder.valType.IsNullable() && reflect2.IsNil(obj) {
  56. stream.WriteNil()
  57. return
  58. }
  59. marshaler := (obj).(encoding.TextMarshaler)
  60. bytes, err := marshaler.MarshalText()
  61. if err != nil {
  62. stream.Error = err
  63. } else {
  64. str := string(bytes)
  65. encoder.stringEncoder.Encode(unsafe.Pointer(&str), stream)
  66. }
  67. }
  68. func (encoder *textMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  69. return encoder.checkIsEmpty.IsEmpty(ptr)
  70. }
  71. type directTextMarshalerEncoder struct {
  72. stringEncoder ValEncoder
  73. checkIsEmpty checkIsEmpty
  74. }
  75. func (encoder *directTextMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  76. marshaler := *(*encoding.TextMarshaler)(ptr)
  77. if marshaler == nil {
  78. stream.WriteNil()
  79. return
  80. }
  81. bytes, err := marshaler.MarshalText()
  82. if err != nil {
  83. stream.Error = err
  84. } else {
  85. str := string(bytes)
  86. encoder.stringEncoder.Encode(unsafe.Pointer(&str), stream)
  87. }
  88. }
  89. func (encoder *directTextMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  90. return encoder.checkIsEmpty.IsEmpty(ptr)
  91. }
  92. type unmarshalerDecoder struct {
  93. templateInterface emptyInterface
  94. }
  95. func (decoder *unmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  96. templateInterface := decoder.templateInterface
  97. templateInterface.word = ptr
  98. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  99. unmarshaler := (*realInterface).(json.Unmarshaler)
  100. iter.nextToken()
  101. iter.unreadByte() // skip spaces
  102. bytes := iter.SkipAndReturnBytes()
  103. err := unmarshaler.UnmarshalJSON(bytes)
  104. if err != nil {
  105. iter.ReportError("unmarshalerDecoder", err.Error())
  106. }
  107. }
  108. type textUnmarshalerDecoder struct {
  109. templateInterface emptyInterface
  110. }
  111. func (decoder *textUnmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  112. templateInterface := decoder.templateInterface
  113. templateInterface.word = ptr
  114. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  115. unmarshaler := (*realInterface).(encoding.TextUnmarshaler)
  116. str := iter.ReadString()
  117. err := unmarshaler.UnmarshalText([]byte(str))
  118. if err != nil {
  119. iter.ReportError("textUnmarshalerDecoder", err.Error())
  120. }
  121. }