reflect_marshaler.go 5.6 KB

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