feature_reflect_native.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. if nonEmptyInterface.itab == nil {
  223. iter.reportError("read non-empty interface", "do not know which concrete type to decode to")
  224. return
  225. }
  226. var i interface{}
  227. e := (*emptyInterface)(unsafe.Pointer(&i))
  228. e.typ = nonEmptyInterface.itab.typ
  229. e.word = nonEmptyInterface.word
  230. iter.ReadVal(&i)
  231. nonEmptyInterface.word = e.word
  232. }
  233. func (codec *nonEmptyInterfaceCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  234. nonEmptyInterface := (*nonEmptyInterface)(ptr)
  235. var i interface{}
  236. e := (*emptyInterface)(unsafe.Pointer(&i))
  237. e.typ = nonEmptyInterface.itab.typ
  238. e.word = nonEmptyInterface.word
  239. stream.WriteVal(i)
  240. }
  241. func (encoder *nonEmptyInterfaceCodec) encodeInterface(val interface{}, stream *Stream) {
  242. stream.WriteVal(val)
  243. }
  244. func (codec *nonEmptyInterfaceCodec) isEmpty(ptr unsafe.Pointer) bool {
  245. nonEmptyInterface := (*nonEmptyInterface)(ptr)
  246. return nonEmptyInterface.word == nil
  247. }
  248. type anyCodec struct {
  249. }
  250. func (codec *anyCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  251. *((*Any)(ptr)) = iter.ReadAny()
  252. }
  253. func (codec *anyCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  254. (*((*Any)(ptr))).WriteTo(stream)
  255. }
  256. func (encoder *anyCodec) encodeInterface(val interface{}, stream *Stream) {
  257. (val.(Any)).WriteTo(stream)
  258. }
  259. func (encoder *anyCodec) isEmpty(ptr unsafe.Pointer) bool {
  260. return (*((*Any)(ptr))).Size() == 0
  261. }
  262. type jsonNumberCodec struct {
  263. }
  264. func (codec *jsonNumberCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  265. *((*json.Number)(ptr)) = json.Number([]byte(iter.readNumberAsString()))
  266. }
  267. func (codec *jsonNumberCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  268. stream.WriteRaw(string(*((*json.Number)(ptr))))
  269. }
  270. func (encoder *jsonNumberCodec) encodeInterface(val interface{}, stream *Stream) {
  271. stream.WriteRaw(string(val.(json.Number)))
  272. }
  273. func (encoder *jsonNumberCodec) isEmpty(ptr unsafe.Pointer) bool {
  274. return len(*((*json.Number)(ptr))) == 0
  275. }
  276. type jsonRawMessageCodec struct {
  277. }
  278. func (codec *jsonRawMessageCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
  279. *((*json.RawMessage)(ptr)) = json.RawMessage(iter.SkipAndReturnBytes())
  280. }
  281. func (codec *jsonRawMessageCodec) encode(ptr unsafe.Pointer, stream *Stream) {
  282. stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
  283. }
  284. func (encoder *jsonRawMessageCodec) encodeInterface(val interface{}, stream *Stream) {
  285. stream.WriteRaw(string(val.(json.RawMessage)))
  286. }
  287. func (encoder *jsonRawMessageCodec) isEmpty(ptr unsafe.Pointer) bool {
  288. return len(*((*json.RawMessage)(ptr))) == 0
  289. }
  290. type base64Codec struct {
  291. }
  292. func (codec *base64Codec) decode(ptr unsafe.Pointer, iter *Iterator) {
  293. encoding := base64.StdEncoding
  294. src := iter.SkipAndReturnBytes()
  295. src = src[1 : len(src)-1]
  296. decodedLen := encoding.DecodedLen(len(src))
  297. dst := make([]byte, decodedLen)
  298. _, err := encoding.Decode(dst, src)
  299. if err != nil {
  300. iter.reportError("decode base64", err.Error())
  301. } else {
  302. *((*[]byte)(ptr)) = dst
  303. }
  304. }
  305. func (codec *base64Codec) encode(ptr unsafe.Pointer, stream *Stream) {
  306. encoding := base64.StdEncoding
  307. stream.writeByte('"')
  308. src := *((*[]byte)(ptr))
  309. toGrow := encoding.EncodedLen(len(src))
  310. stream.ensure(toGrow)
  311. encoding.Encode(stream.buf[stream.n:], src)
  312. stream.n += toGrow
  313. stream.writeByte('"')
  314. }
  315. func (encoder *base64Codec) encodeInterface(val interface{}, stream *Stream) {
  316. encoding := base64.StdEncoding
  317. stream.writeByte('"')
  318. src := val.([]byte)
  319. toGrow := encoding.EncodedLen(len(src))
  320. stream.ensure(toGrow)
  321. encoding.Encode(stream.buf[stream.n:], src)
  322. stream.n += toGrow
  323. stream.writeByte('"')
  324. }
  325. func (encoder *base64Codec) isEmpty(ptr unsafe.Pointer) bool {
  326. return len(*((*[]byte)(ptr))) == 0
  327. }
  328. type stringNumberDecoder struct {
  329. elemDecoder Decoder
  330. }
  331. func (decoder *stringNumberDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
  332. c := iter.nextToken()
  333. if c != '"' {
  334. iter.reportError("stringNumberDecoder", `expect "`)
  335. return
  336. }
  337. decoder.elemDecoder.decode(ptr, iter)
  338. if iter.Error != nil {
  339. return
  340. }
  341. c = iter.readByte()
  342. if c != '"' {
  343. iter.reportError("stringNumberDecoder", `expect "`)
  344. return
  345. }
  346. }
  347. type marshalerEncoder struct {
  348. templateInterface emptyInterface
  349. }
  350. func (encoder *marshalerEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
  351. templateInterface := encoder.templateInterface
  352. templateInterface.word = ptr
  353. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  354. marshaler := (*realInterface).(json.Marshaler)
  355. bytes, err := marshaler.MarshalJSON()
  356. if err != nil {
  357. stream.Error = err
  358. } else {
  359. stream.Write(bytes)
  360. }
  361. }
  362. func (encoder *marshalerEncoder) encodeInterface(val interface{}, stream *Stream) {
  363. writeToStream(val, stream, encoder)
  364. }
  365. func (encoder *marshalerEncoder) isEmpty(ptr unsafe.Pointer) bool {
  366. templateInterface := encoder.templateInterface
  367. templateInterface.word = ptr
  368. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  369. marshaler := (*realInterface).(json.Marshaler)
  370. bytes, err := marshaler.MarshalJSON()
  371. if err != nil {
  372. return true
  373. } else {
  374. return len(bytes) > 0
  375. }
  376. }
  377. type unmarshalerDecoder struct {
  378. templateInterface emptyInterface
  379. }
  380. func (decoder *unmarshalerDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
  381. templateInterface := decoder.templateInterface
  382. templateInterface.word = ptr
  383. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  384. unmarshaler := (*realInterface).(json.Unmarshaler)
  385. bytes := iter.SkipAndReturnBytes()
  386. err := unmarshaler.UnmarshalJSON(bytes)
  387. if err != nil {
  388. iter.reportError("unmarshaler", err.Error())
  389. }
  390. }