feature_reflect_native.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. package jsoniter
  2. import (
  3. "unsafe"
  4. "encoding/json"
  5. )
  6. type stringCodec struct {
  7. }
  8. func (codec *stringCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  9. *((*string)(ptr)) = iter.ReadString()
  10. }
  11. func (codec *stringCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  12. str := *((*string)(ptr))
  13. stream.WriteString(str)
  14. }
  15. func (encoder *stringCodec) encodeInterface(val interface{}, stream *Stream) {
  16. writeToStream(val, stream, encoder)
  17. }
  18. func (codec *stringCodec) isEmpty(ptr unsafe.Pointer) bool {
  19. return *((*string)(ptr)) == ""
  20. }
  21. type intCodec struct {
  22. }
  23. func (codec *intCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  24. *((*int)(ptr)) = iter.ReadInt()
  25. }
  26. func (codec *intCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  27. stream.WriteInt(*((*int)(ptr)))
  28. }
  29. func (encoder *intCodec) encodeInterface(val interface{}, stream *Stream) {
  30. writeToStream(val, stream, encoder)
  31. }
  32. func (codec *intCodec) isEmpty(ptr unsafe.Pointer) bool {
  33. return *((*int)(ptr)) == 0
  34. }
  35. type int8Codec struct {
  36. }
  37. func (codec *int8Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  38. *((*int8)(ptr)) = iter.ReadInt8()
  39. }
  40. func (codec *int8Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  41. stream.WriteInt8(*((*int8)(ptr)))
  42. }
  43. func (encoder *int8Codec) encodeInterface(val interface{}, stream *Stream) {
  44. writeToStream(val, stream, encoder)
  45. }
  46. func (codec *int8Codec) isEmpty(ptr unsafe.Pointer) bool {
  47. return *((*int8)(ptr)) == 0
  48. }
  49. type int16Codec struct {
  50. }
  51. func (codec *int16Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  52. *((*int16)(ptr)) = iter.ReadInt16()
  53. }
  54. func (codec *int16Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  55. stream.WriteInt16(*((*int16)(ptr)))
  56. }
  57. func (encoder *int16Codec) encodeInterface(val interface{}, stream *Stream) {
  58. writeToStream(val, stream, encoder)
  59. }
  60. func (codec *int16Codec) isEmpty(ptr unsafe.Pointer) bool {
  61. return *((*int16)(ptr)) == 0
  62. }
  63. type int32Codec struct {
  64. }
  65. func (codec *int32Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  66. *((*int32)(ptr)) = iter.ReadInt32()
  67. }
  68. func (codec *int32Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  69. stream.WriteInt32(*((*int32)(ptr)))
  70. }
  71. func (encoder *int32Codec) encodeInterface(val interface{}, stream *Stream) {
  72. writeToStream(val, stream, encoder)
  73. }
  74. func (codec *int32Codec) isEmpty(ptr unsafe.Pointer) bool {
  75. return *((*int32)(ptr)) == 0
  76. }
  77. type int64Codec struct {
  78. }
  79. func (codec *int64Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  80. *((*int64)(ptr)) = iter.ReadInt64()
  81. }
  82. func (codec *int64Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  83. stream.WriteInt64(*((*int64)(ptr)))
  84. }
  85. func (encoder *int64Codec) encodeInterface(val interface{}, stream *Stream) {
  86. writeToStream(val, stream, encoder)
  87. }
  88. func (codec *int64Codec) isEmpty(ptr unsafe.Pointer) bool {
  89. return *((*int64)(ptr)) == 0
  90. }
  91. type uintCodec struct {
  92. }
  93. func (codec *uintCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  94. *((*uint)(ptr)) = iter.ReadUint()
  95. }
  96. func (codec *uintCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  97. stream.WriteUint(*((*uint)(ptr)))
  98. }
  99. func (encoder *uintCodec) encodeInterface(val interface{}, stream *Stream) {
  100. writeToStream(val, stream, encoder)
  101. }
  102. func (codec *uintCodec) isEmpty(ptr unsafe.Pointer) bool {
  103. return *((*uint)(ptr)) == 0
  104. }
  105. type uint8Codec struct {
  106. }
  107. func (codec *uint8Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  108. *((*uint8)(ptr)) = iter.ReadUint8()
  109. }
  110. func (codec *uint8Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  111. stream.WriteUint8(*((*uint8)(ptr)))
  112. }
  113. func (encoder *uint8Codec) encodeInterface(val interface{}, stream *Stream) {
  114. writeToStream(val, stream, encoder)
  115. }
  116. func (codec *uint8Codec) isEmpty(ptr unsafe.Pointer) bool {
  117. return *((*uint8)(ptr)) == 0
  118. }
  119. type uint16Codec struct {
  120. }
  121. func (decoder *uint16Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  122. *((*uint16)(ptr)) = iter.ReadUint16()
  123. }
  124. func (codec *uint16Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  125. stream.WriteUint16(*((*uint16)(ptr)))
  126. }
  127. func (encoder *uint16Codec) encodeInterface(val interface{}, stream *Stream) {
  128. writeToStream(val, stream, encoder)
  129. }
  130. func (codec *uint16Codec) isEmpty(ptr unsafe.Pointer) bool {
  131. return *((*uint16)(ptr)) == 0
  132. }
  133. type uint32Codec struct {
  134. }
  135. func (codec *uint32Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  136. *((*uint32)(ptr)) = iter.ReadUint32()
  137. }
  138. func (codec *uint32Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  139. stream.WriteUint32(*((*uint32)(ptr)))
  140. }
  141. func (encoder *uint32Codec) encodeInterface(val interface{}, stream *Stream) {
  142. writeToStream(val, stream, encoder)
  143. }
  144. func (codec *uint32Codec) isEmpty(ptr unsafe.Pointer) bool {
  145. return *((*uint32)(ptr)) == 0
  146. }
  147. type uint64Codec struct {
  148. }
  149. func (codec *uint64Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  150. *((*uint64)(ptr)) = iter.ReadUint64()
  151. }
  152. func (codec *uint64Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  153. stream.WriteUint64(*((*uint64)(ptr)))
  154. }
  155. func (encoder *uint64Codec) encodeInterface(val interface{}, stream *Stream) {
  156. writeToStream(val, stream, encoder)
  157. }
  158. func (codec *uint64Codec) isEmpty(ptr unsafe.Pointer) bool {
  159. return *((*uint64)(ptr)) == 0
  160. }
  161. type float32Codec struct {
  162. }
  163. func (codec *float32Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  164. *((*float32)(ptr)) = iter.ReadFloat32()
  165. }
  166. func (codec *float32Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  167. stream.WriteFloat32(*((*float32)(ptr)))
  168. }
  169. func (encoder *float32Codec) encodeInterface(val interface{}, stream *Stream) {
  170. writeToStream(val, stream, encoder)
  171. }
  172. func (codec *float32Codec) isEmpty(ptr unsafe.Pointer) bool {
  173. return *((*float32)(ptr)) == 0
  174. }
  175. type float64Codec struct {
  176. }
  177. func (codec *float64Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  178. *((*float64)(ptr)) = iter.ReadFloat64()
  179. }
  180. func (codec *float64Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  181. stream.WriteFloat64(*((*float64)(ptr)))
  182. }
  183. func (encoder *float64Codec) encodeInterface(val interface{}, stream *Stream) {
  184. writeToStream(val, stream, encoder)
  185. }
  186. func (codec *float64Codec) isEmpty(ptr unsafe.Pointer) bool {
  187. return *((*float64)(ptr)) == 0
  188. }
  189. type boolCodec struct {
  190. }
  191. func (codec *boolCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  192. *((*bool)(ptr)) = iter.ReadBool()
  193. }
  194. func (codec *boolCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  195. stream.WriteBool(*((*bool)(ptr)))
  196. }
  197. func (encoder *boolCodec) encodeInterface(val interface{}, stream *Stream) {
  198. writeToStream(val, stream, encoder)
  199. }
  200. func (codec *boolCodec) isEmpty(ptr unsafe.Pointer) bool {
  201. return !(*((*bool)(ptr)))
  202. }
  203. type emptyInterfaceCodec struct {
  204. }
  205. func (codec *emptyInterfaceCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  206. *((*interface{})(ptr)) = iter.Read()
  207. }
  208. func (codec *emptyInterfaceCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  209. stream.WriteVal(*((*interface{})(ptr)))
  210. }
  211. func (encoder *emptyInterfaceCodec) encodeInterface(val interface{}, stream *Stream) {
  212. stream.WriteVal(val)
  213. }
  214. func (codec *emptyInterfaceCodec) isEmpty(ptr unsafe.Pointer) bool {
  215. return ptr == nil
  216. }
  217. type nonEmptyInterfaceCodec struct {
  218. }
  219. func (codec *nonEmptyInterfaceCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  220. nonEmptyInterface := (*nonEmptyInterface)(ptr)
  221. var i interface{}
  222. e := (*emptyInterface)(unsafe.Pointer(&i))
  223. e.typ = nonEmptyInterface.itab.typ
  224. e.word = nonEmptyInterface.word
  225. iter.ReadVal(&i)
  226. nonEmptyInterface.word = e.word
  227. }
  228. func (codec *nonEmptyInterfaceCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  229. nonEmptyInterface := (*nonEmptyInterface)(ptr)
  230. var i interface{}
  231. e := (*emptyInterface)(unsafe.Pointer(&i))
  232. e.typ = nonEmptyInterface.itab.typ
  233. e.word = nonEmptyInterface.word
  234. stream.WriteVal(i)
  235. }
  236. func (encoder *nonEmptyInterfaceCodec) encodeInterface(val interface{}, stream *Stream) {
  237. stream.WriteVal(val)
  238. }
  239. func (codec *nonEmptyInterfaceCodec) isEmpty(ptr unsafe.Pointer) bool {
  240. nonEmptyInterface := (*nonEmptyInterface)(ptr)
  241. return nonEmptyInterface.word == nil
  242. }
  243. type anyCodec struct {
  244. }
  245. func (codec *anyCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  246. *((*Any)(ptr)) = iter.ReadAny()
  247. }
  248. func (codec *anyCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  249. (*((*Any)(ptr))).WriteTo(stream)
  250. }
  251. func (encoder *anyCodec) encodeInterface(val interface{}, stream *Stream) {
  252. (val.(Any)).WriteTo(stream)
  253. }
  254. func (encoder *anyCodec) isEmpty(ptr unsafe.Pointer) bool {
  255. return (*((*Any)(ptr))).Size() == 0
  256. }
  257. type jsonNumberCodec struct {
  258. }
  259. func (codec *jsonNumberCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  260. *((*json.Number)(ptr)) = json.Number([]byte(iter.readNumberAsString()))
  261. }
  262. func (codec *jsonNumberCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  263. stream.WriteRaw(string(*((*json.Number)(ptr))))
  264. }
  265. func (encoder *jsonNumberCodec) encodeInterface(val interface{}, stream *Stream) {
  266. stream.WriteRaw(string(val.(json.Number)))
  267. }
  268. func (encoder *jsonNumberCodec) isEmpty(ptr unsafe.Pointer) bool {
  269. return len(*((*json.Number)(ptr))) == 0
  270. }
  271. type jsonRawMessageCodec struct {
  272. }
  273. func (codec *jsonRawMessageCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  274. *((*json.RawMessage)(ptr)) = json.RawMessage(iter.SkipAndReturnBytes())
  275. }
  276. func (codec *jsonRawMessageCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  277. stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
  278. }
  279. func (encoder *jsonRawMessageCodec) encodeInterface(val interface{}, stream *Stream) {
  280. stream.WriteRaw(string(val.(json.RawMessage)))
  281. }
  282. func (encoder *jsonRawMessageCodec) isEmpty(ptr unsafe.Pointer) bool {
  283. return len(*((*json.RawMessage)(ptr))) == 0
  284. }
  285. type stringNumberDecoder struct {
  286. elemDecoder Decoder
  287. }
  288. func (decoder *stringNumberDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
  289. c := iter.nextToken()
  290. if c != '"' {
  291. iter.reportError("stringNumberDecoder", `expect "`)
  292. return
  293. }
  294. decoder.elemDecoder.decode(ptr, iter)
  295. if iter.Error != nil {
  296. return
  297. }
  298. c = iter.readByte()
  299. if c != '"' {
  300. iter.reportError("stringNumberDecoder", `expect "`)
  301. return
  302. }
  303. }
  304. type marshalerEncoder struct {
  305. templateInterface emptyInterface
  306. }
  307. func (encoder *marshalerEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
  308. templateInterface := encoder.templateInterface
  309. templateInterface.word = ptr
  310. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  311. marshaler := (*realInterface).(json.Marshaler)
  312. bytes, err := marshaler.MarshalJSON()
  313. if err != nil {
  314. stream.Error = err
  315. } else {
  316. stream.Write(bytes)
  317. }
  318. }
  319. func (encoder *marshalerEncoder) encodeInterface(val interface{}, stream *Stream) {
  320. writeToStream(val, stream, encoder)
  321. }
  322. func (encoder *marshalerEncoder) isEmpty(ptr unsafe.Pointer) bool {
  323. templateInterface := encoder.templateInterface
  324. templateInterface.word = ptr
  325. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  326. marshaler := (*realInterface).(json.Marshaler)
  327. bytes, err := marshaler.MarshalJSON()
  328. if err != nil {
  329. return true
  330. } else {
  331. return len(bytes) > 0
  332. }
  333. }
  334. type unmarshalerDecoder struct {
  335. templateInterface emptyInterface
  336. }
  337. func (decoder *unmarshalerDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
  338. templateInterface := decoder.templateInterface
  339. templateInterface.word = ptr
  340. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  341. unmarshaler := (*realInterface).(json.Unmarshaler)
  342. bytes := iter.SkipAndReturnBytes()
  343. err := unmarshaler.UnmarshalJSON(bytes)
  344. if err != nil {
  345. iter.reportError("unmarshaler", err.Error())
  346. }
  347. }