feature_reflect_native.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. package jsoniter
  2. import (
  3. "unsafe"
  4. )
  5. type stringCodec struct {
  6. }
  7. func (codec *stringCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  8. *((*string)(ptr)) = iter.ReadString()
  9. }
  10. func (codec *stringCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  11. str := *((*string)(ptr))
  12. stream.WriteString(str)
  13. }
  14. func (encoder *stringCodec) encodeInterface(val interface{}, stream *Stream) {
  15. writeToStream(val, stream, encoder)
  16. }
  17. func (codec *stringCodec) isEmpty(ptr unsafe.Pointer) bool {
  18. return *((*string)(ptr)) == ""
  19. }
  20. type intCodec struct {
  21. }
  22. func (codec *intCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  23. *((*int)(ptr)) = iter.ReadInt()
  24. }
  25. func (codec *intCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  26. stream.WriteInt(*((*int)(ptr)))
  27. }
  28. func (encoder *intCodec) encodeInterface(val interface{}, stream *Stream) {
  29. writeToStream(val, stream, encoder)
  30. }
  31. func (codec *intCodec) isEmpty(ptr unsafe.Pointer) bool {
  32. return *((*int)(ptr)) == 0
  33. }
  34. type int8Codec struct {
  35. }
  36. func (codec *int8Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  37. *((*int8)(ptr)) = iter.ReadInt8()
  38. }
  39. func (codec *int8Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  40. stream.WriteInt8(*((*int8)(ptr)))
  41. }
  42. func (encoder *int8Codec) encodeInterface(val interface{}, stream *Stream) {
  43. writeToStream(val, stream, encoder)
  44. }
  45. func (codec *int8Codec) isEmpty(ptr unsafe.Pointer) bool {
  46. return *((*int8)(ptr)) == 0
  47. }
  48. type int16Codec struct {
  49. }
  50. func (codec *int16Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  51. *((*int16)(ptr)) = iter.ReadInt16()
  52. }
  53. func (codec *int16Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  54. stream.WriteInt16(*((*int16)(ptr)))
  55. }
  56. func (encoder *int16Codec) encodeInterface(val interface{}, stream *Stream) {
  57. writeToStream(val, stream, encoder)
  58. }
  59. func (codec *int16Codec) isEmpty(ptr unsafe.Pointer) bool {
  60. return *((*int16)(ptr)) == 0
  61. }
  62. type int32Codec struct {
  63. }
  64. func (codec *int32Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  65. *((*int32)(ptr)) = iter.ReadInt32()
  66. }
  67. func (codec *int32Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  68. stream.WriteInt32(*((*int32)(ptr)))
  69. }
  70. func (encoder *int32Codec) encodeInterface(val interface{}, stream *Stream) {
  71. writeToStream(val, stream, encoder)
  72. }
  73. func (codec *int32Codec) isEmpty(ptr unsafe.Pointer) bool {
  74. return *((*int32)(ptr)) == 0
  75. }
  76. type int64Codec struct {
  77. }
  78. func (codec *int64Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  79. *((*int64)(ptr)) = iter.ReadInt64()
  80. }
  81. func (codec *int64Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  82. stream.WriteInt64(*((*int64)(ptr)))
  83. }
  84. func (encoder *int64Codec) encodeInterface(val interface{}, stream *Stream) {
  85. writeToStream(val, stream, encoder)
  86. }
  87. func (codec *int64Codec) isEmpty(ptr unsafe.Pointer) bool {
  88. return *((*int64)(ptr)) == 0
  89. }
  90. type uintCodec struct {
  91. }
  92. func (codec *uintCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  93. *((*uint)(ptr)) = iter.ReadUint()
  94. }
  95. func (codec *uintCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  96. stream.WriteUint(*((*uint)(ptr)))
  97. }
  98. func (encoder *uintCodec) encodeInterface(val interface{}, stream *Stream) {
  99. writeToStream(val, stream, encoder)
  100. }
  101. func (codec *uintCodec) isEmpty(ptr unsafe.Pointer) bool {
  102. return *((*uint)(ptr)) == 0
  103. }
  104. type uint8Codec struct {
  105. }
  106. func (codec *uint8Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  107. *((*uint8)(ptr)) = iter.ReadUint8()
  108. }
  109. func (codec *uint8Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  110. stream.WriteUint8(*((*uint8)(ptr)))
  111. }
  112. func (encoder *uint8Codec) encodeInterface(val interface{}, stream *Stream) {
  113. writeToStream(val, stream, encoder)
  114. }
  115. func (codec *uint8Codec) isEmpty(ptr unsafe.Pointer) bool {
  116. return *((*uint8)(ptr)) == 0
  117. }
  118. type uint16Codec struct {
  119. }
  120. func (decoder *uint16Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  121. *((*uint16)(ptr)) = iter.ReadUint16()
  122. }
  123. func (codec *uint16Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  124. stream.WriteUint16(*((*uint16)(ptr)))
  125. }
  126. func (encoder *uint16Codec) encodeInterface(val interface{}, stream *Stream) {
  127. writeToStream(val, stream, encoder)
  128. }
  129. func (codec *uint16Codec) isEmpty(ptr unsafe.Pointer) bool {
  130. return *((*uint16)(ptr)) == 0
  131. }
  132. type uint32Codec struct {
  133. }
  134. func (codec *uint32Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  135. *((*uint32)(ptr)) = iter.ReadUint32()
  136. }
  137. func (codec *uint32Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  138. stream.WriteUint32(*((*uint32)(ptr)))
  139. }
  140. func (encoder *uint32Codec) encodeInterface(val interface{}, stream *Stream) {
  141. writeToStream(val, stream, encoder)
  142. }
  143. func (codec *uint32Codec) isEmpty(ptr unsafe.Pointer) bool {
  144. return *((*uint32)(ptr)) == 0
  145. }
  146. type uint64Codec struct {
  147. }
  148. func (codec *uint64Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  149. *((*uint64)(ptr)) = iter.ReadUint64()
  150. }
  151. func (codec *uint64Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  152. stream.WriteUint64(*((*uint64)(ptr)))
  153. }
  154. func (encoder *uint64Codec) encodeInterface(val interface{}, stream *Stream) {
  155. writeToStream(val, stream, encoder)
  156. }
  157. func (codec *uint64Codec) isEmpty(ptr unsafe.Pointer) bool {
  158. return *((*uint64)(ptr)) == 0
  159. }
  160. type float32Codec struct {
  161. }
  162. func (codec *float32Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  163. *((*float32)(ptr)) = iter.ReadFloat32()
  164. }
  165. func (codec *float32Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  166. stream.WriteFloat32(*((*float32)(ptr)))
  167. }
  168. func (encoder *float32Codec) encodeInterface(val interface{}, stream *Stream) {
  169. writeToStream(val, stream, encoder)
  170. }
  171. func (codec *float32Codec) isEmpty(ptr unsafe.Pointer) bool {
  172. return *((*float32)(ptr)) == 0
  173. }
  174. type float64Codec struct {
  175. }
  176. func (codec *float64Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  177. *((*float64)(ptr)) = iter.ReadFloat64()
  178. }
  179. func (codec *float64Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  180. stream.WriteFloat64(*((*float64)(ptr)))
  181. }
  182. func (encoder *float64Codec) encodeInterface(val interface{}, stream *Stream) {
  183. writeToStream(val, stream, encoder)
  184. }
  185. func (codec *float64Codec) isEmpty(ptr unsafe.Pointer) bool {
  186. return *((*float64)(ptr)) == 0
  187. }
  188. type boolCodec struct {
  189. }
  190. func (codec *boolCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  191. *((*bool)(ptr)) = iter.ReadBool()
  192. }
  193. func (codec *boolCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  194. stream.WriteBool(*((*bool)(ptr)))
  195. }
  196. func (encoder *boolCodec) encodeInterface(val interface{}, stream *Stream) {
  197. writeToStream(val, stream, encoder)
  198. }
  199. func (codec *boolCodec) isEmpty(ptr unsafe.Pointer) bool {
  200. return !(*((*bool)(ptr)))
  201. }
  202. type interfaceCodec struct {
  203. }
  204. func (codec *interfaceCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  205. *((*interface{})(ptr)) = iter.Read()
  206. }
  207. func (codec *interfaceCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  208. stream.WriteVal(*((*interface{})(ptr)))
  209. }
  210. func (encoder *interfaceCodec) encodeInterface(val interface{}, stream *Stream) {
  211. stream.WriteVal(val)
  212. }
  213. func (codec *interfaceCodec) isEmpty(ptr unsafe.Pointer) bool {
  214. return ptr == nil
  215. }
  216. type anyCodec struct {
  217. }
  218. func (codec *anyCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  219. *((*Any)(ptr)) = iter.ReadAny()
  220. }
  221. func (codec *anyCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  222. (*((*Any)(ptr))).WriteTo(stream)
  223. }
  224. func (encoder *anyCodec) encodeInterface(val interface{}, stream *Stream) {
  225. (val.(Any)).WriteTo(stream)
  226. }
  227. func (encoder *anyCodec) isEmpty(ptr unsafe.Pointer) bool {
  228. return (*((*Any)(ptr))).Size() == 0
  229. }
  230. type stringNumberDecoder struct {
  231. elemDecoder Decoder
  232. }
  233. func (decoder *stringNumberDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
  234. c := iter.nextToken()
  235. if c != '"' {
  236. iter.reportError("stringNumberDecoder", `expect "`)
  237. return
  238. }
  239. decoder.elemDecoder.decode(ptr, iter)
  240. if iter.Error != nil {
  241. return
  242. }
  243. c = iter.readByte()
  244. if c != '"' {
  245. iter.reportError("stringNumberDecoder", `expect "`)
  246. return
  247. }
  248. }