feature_reflect_marshaler.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package jsoniter
  2. import (
  3. "github.com/v2pro/plz/reflect2"
  4. "unsafe"
  5. "encoding"
  6. "encoding/json"
  7. "reflect"
  8. )
  9. func createEncoderOfMarshaler(cfg *frozenConfig, prefix string, typ reflect.Type) ValEncoder {
  10. if typ == marshalerType {
  11. checkIsEmpty := createCheckIsEmpty(cfg, typ)
  12. var encoder ValEncoder = &directMarshalerEncoder{
  13. checkIsEmpty: checkIsEmpty,
  14. }
  15. return encoder
  16. }
  17. if typ.Implements(marshalerType) {
  18. checkIsEmpty := createCheckIsEmpty(cfg, typ)
  19. var encoder ValEncoder = &marshalerEncoder{
  20. valType: reflect2.Type2(typ),
  21. checkIsEmpty: checkIsEmpty,
  22. }
  23. return encoder
  24. }
  25. ptrType := reflect.PtrTo(typ)
  26. if prefix != "" && ptrType.Implements(marshalerType) {
  27. checkIsEmpty := createCheckIsEmpty(cfg, ptrType)
  28. var encoder ValEncoder = &marshalerEncoder{
  29. valType: reflect2.Type2(ptrType),
  30. checkIsEmpty: checkIsEmpty,
  31. }
  32. return &referenceEncoder{encoder}
  33. }
  34. if typ == textMarshalerType {
  35. checkIsEmpty := createCheckIsEmpty(cfg, typ)
  36. var encoder ValEncoder = &directTextMarshalerEncoder{
  37. checkIsEmpty: checkIsEmpty,
  38. stringEncoder: cfg.EncoderOf(reflect.TypeOf("")),
  39. }
  40. return encoder
  41. }
  42. if typ.Implements(textMarshalerType) {
  43. checkIsEmpty := createCheckIsEmpty(cfg, typ)
  44. var encoder ValEncoder = &textMarshalerEncoder{
  45. valType: reflect2.Type2(typ),
  46. stringEncoder: cfg.EncoderOf(reflect.TypeOf("")),
  47. checkIsEmpty: checkIsEmpty,
  48. }
  49. return encoder
  50. }
  51. // if prefix is empty, the type is the root type
  52. if prefix != "" && ptrType.Implements(textMarshalerType) {
  53. checkIsEmpty := createCheckIsEmpty(cfg, ptrType)
  54. var encoder ValEncoder = &textMarshalerEncoder{
  55. valType: reflect2.Type2(ptrType),
  56. stringEncoder: cfg.EncoderOf(reflect.TypeOf("")),
  57. checkIsEmpty: checkIsEmpty,
  58. }
  59. return &referenceEncoder{encoder}
  60. }
  61. return nil
  62. }
  63. type marshalerEncoder struct {
  64. checkIsEmpty checkIsEmpty
  65. valType reflect2.Type
  66. }
  67. func (encoder *marshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  68. obj := encoder.valType.UnsafeIndirect(ptr)
  69. if encoder.valType.IsNullable() && reflect2.IsNil(obj) {
  70. stream.WriteNil()
  71. return
  72. }
  73. marshaler := obj.(json.Marshaler)
  74. bytes, err := marshaler.MarshalJSON()
  75. if err != nil {
  76. stream.Error = err
  77. } else {
  78. stream.Write(bytes)
  79. }
  80. }
  81. func (encoder *marshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  82. return encoder.checkIsEmpty.IsEmpty(ptr)
  83. }
  84. type directMarshalerEncoder struct {
  85. checkIsEmpty checkIsEmpty
  86. }
  87. func (encoder *directMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  88. marshaler := *(*json.Marshaler)(ptr)
  89. if marshaler == nil {
  90. stream.WriteNil()
  91. return
  92. }
  93. bytes, err := marshaler.MarshalJSON()
  94. if err != nil {
  95. stream.Error = err
  96. } else {
  97. stream.Write(bytes)
  98. }
  99. }
  100. func (encoder *directMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  101. return encoder.checkIsEmpty.IsEmpty(ptr)
  102. }
  103. type textMarshalerEncoder struct {
  104. valType reflect2.Type
  105. stringEncoder ValEncoder
  106. checkIsEmpty checkIsEmpty
  107. }
  108. func (encoder *textMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  109. obj := encoder.valType.UnsafeIndirect(ptr)
  110. if encoder.valType.IsNullable() && reflect2.IsNil(obj) {
  111. stream.WriteNil()
  112. return
  113. }
  114. marshaler := (obj).(encoding.TextMarshaler)
  115. bytes, err := marshaler.MarshalText()
  116. if err != nil {
  117. stream.Error = err
  118. } else {
  119. str := string(bytes)
  120. encoder.stringEncoder.Encode(unsafe.Pointer(&str), stream)
  121. }
  122. }
  123. func (encoder *textMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  124. return encoder.checkIsEmpty.IsEmpty(ptr)
  125. }
  126. type directTextMarshalerEncoder struct {
  127. stringEncoder ValEncoder
  128. checkIsEmpty checkIsEmpty
  129. }
  130. func (encoder *directTextMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  131. marshaler := *(*encoding.TextMarshaler)(ptr)
  132. if marshaler == nil {
  133. stream.WriteNil()
  134. return
  135. }
  136. bytes, err := marshaler.MarshalText()
  137. if err != nil {
  138. stream.Error = err
  139. } else {
  140. str := string(bytes)
  141. encoder.stringEncoder.Encode(unsafe.Pointer(&str), stream)
  142. }
  143. }
  144. func (encoder *directTextMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  145. return encoder.checkIsEmpty.IsEmpty(ptr)
  146. }
  147. type unmarshalerDecoder struct {
  148. templateInterface emptyInterface
  149. }
  150. func (decoder *unmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  151. templateInterface := decoder.templateInterface
  152. templateInterface.word = ptr
  153. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  154. unmarshaler := (*realInterface).(json.Unmarshaler)
  155. iter.nextToken()
  156. iter.unreadByte() // skip spaces
  157. bytes := iter.SkipAndReturnBytes()
  158. err := unmarshaler.UnmarshalJSON(bytes)
  159. if err != nil {
  160. iter.ReportError("unmarshalerDecoder", err.Error())
  161. }
  162. }
  163. type textUnmarshalerDecoder struct {
  164. templateInterface emptyInterface
  165. }
  166. func (decoder *textUnmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  167. templateInterface := decoder.templateInterface
  168. templateInterface.word = ptr
  169. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  170. unmarshaler := (*realInterface).(encoding.TextUnmarshaler)
  171. str := iter.ReadString()
  172. err := unmarshaler.UnmarshalText([]byte(str))
  173. if err != nil {
  174. iter.ReportError("textUnmarshalerDecoder", err.Error())
  175. }
  176. }