reflect_marshaler.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package jsoniter
  2. import (
  3. "encoding"
  4. "encoding/json"
  5. "unsafe"
  6. "github.com/modern-go/reflect2"
  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. marshaler := obj.(json.Marshaler)
  91. bytes, err := marshaler.MarshalJSON()
  92. if err != nil {
  93. stream.Error = err
  94. } else {
  95. // html escape was already done by jsoniter
  96. // but the extra '\n' should be trimed
  97. l := len(bytes)
  98. if l > 0 && bytes[l-1] == '\n' {
  99. bytes = bytes[:l-1]
  100. }
  101. stream.Write(bytes)
  102. }
  103. }
  104. func (encoder *marshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  105. return encoder.checkIsEmpty.IsEmpty(ptr)
  106. }
  107. type directMarshalerEncoder struct {
  108. checkIsEmpty checkIsEmpty
  109. }
  110. func (encoder *directMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  111. marshaler := *(*json.Marshaler)(ptr)
  112. if marshaler == nil {
  113. stream.WriteNil()
  114. return
  115. }
  116. bytes, err := marshaler.MarshalJSON()
  117. if err != nil {
  118. stream.Error = err
  119. } else {
  120. stream.Write(bytes)
  121. }
  122. }
  123. func (encoder *directMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  124. return encoder.checkIsEmpty.IsEmpty(ptr)
  125. }
  126. type textMarshalerEncoder struct {
  127. valType reflect2.Type
  128. stringEncoder ValEncoder
  129. checkIsEmpty checkIsEmpty
  130. }
  131. func (encoder *textMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  132. obj := encoder.valType.UnsafeIndirect(ptr)
  133. if encoder.valType.IsNullable() && reflect2.IsNil(obj) {
  134. stream.WriteNil()
  135. return
  136. }
  137. marshaler := (obj).(encoding.TextMarshaler)
  138. bytes, err := marshaler.MarshalText()
  139. if err != nil {
  140. stream.Error = err
  141. } else {
  142. str := string(bytes)
  143. encoder.stringEncoder.Encode(unsafe.Pointer(&str), stream)
  144. }
  145. }
  146. func (encoder *textMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  147. return encoder.checkIsEmpty.IsEmpty(ptr)
  148. }
  149. type directTextMarshalerEncoder struct {
  150. stringEncoder ValEncoder
  151. checkIsEmpty checkIsEmpty
  152. }
  153. func (encoder *directTextMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  154. marshaler := *(*encoding.TextMarshaler)(ptr)
  155. if marshaler == nil {
  156. stream.WriteNil()
  157. return
  158. }
  159. bytes, err := marshaler.MarshalText()
  160. if err != nil {
  161. stream.Error = err
  162. } else {
  163. str := string(bytes)
  164. encoder.stringEncoder.Encode(unsafe.Pointer(&str), stream)
  165. }
  166. }
  167. func (encoder *directTextMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  168. return encoder.checkIsEmpty.IsEmpty(ptr)
  169. }
  170. type unmarshalerDecoder struct {
  171. valType reflect2.Type
  172. }
  173. func (decoder *unmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  174. valType := decoder.valType
  175. obj := valType.UnsafeIndirect(ptr)
  176. unmarshaler := obj.(json.Unmarshaler)
  177. iter.nextToken()
  178. iter.unreadByte() // skip spaces
  179. bytes := iter.SkipAndReturnBytes()
  180. err := unmarshaler.UnmarshalJSON(bytes)
  181. if err != nil {
  182. iter.ReportError("unmarshalerDecoder", err.Error())
  183. }
  184. }
  185. type textUnmarshalerDecoder struct {
  186. valType reflect2.Type
  187. }
  188. func (decoder *textUnmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  189. valType := decoder.valType
  190. obj := valType.UnsafeIndirect(ptr)
  191. if reflect2.IsNil(obj) {
  192. ptrType := valType.(*reflect2.UnsafePtrType)
  193. elemType := ptrType.Elem()
  194. elem := elemType.UnsafeNew()
  195. ptrType.UnsafeSet(ptr, unsafe.Pointer(&elem))
  196. obj = valType.UnsafeIndirect(ptr)
  197. }
  198. unmarshaler := (obj).(encoding.TextUnmarshaler)
  199. str := iter.ReadString()
  200. err := unmarshaler.UnmarshalText([]byte(str))
  201. if err != nil {
  202. iter.ReportError("textUnmarshalerDecoder", err.Error())
  203. }
  204. }