feature_reflect_native.go 12 KB

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