另客网go项目公用的代码库

reflect_marshaler.go 5.7KB

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