feature_reflect_native.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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 (codec *stringCodec) EncodeInterface(val interface{}, stream *Stream) {
  17. writeToStream(val, stream, codec)
  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 jsoniterRawMessageCodec struct {
  291. }
  292. func (codec *jsoniterRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  293. *((*RawMessage)(ptr)) = RawMessage(iter.SkipAndReturnBytes())
  294. }
  295. func (codec *jsoniterRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  296. stream.WriteRaw(string(*((*RawMessage)(ptr))))
  297. }
  298. func (encoder *jsoniterRawMessageCodec) EncodeInterface(val interface{}, stream *Stream) {
  299. stream.WriteRaw(string(val.(RawMessage)))
  300. }
  301. func (encoder *jsoniterRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
  302. return len(*((*RawMessage)(ptr))) == 0
  303. }
  304. type base64Codec struct {
  305. }
  306. func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  307. encoding := base64.StdEncoding
  308. src := iter.SkipAndReturnBytes()
  309. src = src[1 : len(src)-1]
  310. decodedLen := encoding.DecodedLen(len(src))
  311. dst := make([]byte, decodedLen)
  312. _, err := encoding.Decode(dst, src)
  313. if err != nil {
  314. iter.ReportError("decode base64", err.Error())
  315. } else {
  316. *((*[]byte)(ptr)) = dst
  317. }
  318. }
  319. func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  320. encoding := base64.StdEncoding
  321. stream.writeByte('"')
  322. src := *((*[]byte)(ptr))
  323. toGrow := encoding.EncodedLen(len(src))
  324. stream.ensure(toGrow)
  325. encoding.Encode(stream.buf[stream.n:], src)
  326. stream.n += toGrow
  327. stream.writeByte('"')
  328. }
  329. func (encoder *base64Codec) EncodeInterface(val interface{}, stream *Stream) {
  330. encoding := base64.StdEncoding
  331. stream.writeByte('"')
  332. src := val.([]byte)
  333. toGrow := encoding.EncodedLen(len(src))
  334. stream.ensure(toGrow)
  335. encoding.Encode(stream.buf[stream.n:], src)
  336. stream.n += toGrow
  337. stream.writeByte('"')
  338. }
  339. func (encoder *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  340. return len(*((*[]byte)(ptr))) == 0
  341. }
  342. type stringModeDecoder struct {
  343. elemDecoder ValDecoder
  344. }
  345. func (decoder *stringModeDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  346. c := iter.nextToken()
  347. if c != '"' {
  348. iter.ReportError("stringModeDecoder", `expect "`)
  349. return
  350. }
  351. decoder.elemDecoder.Decode(ptr, iter)
  352. if iter.Error != nil {
  353. return
  354. }
  355. c = iter.readByte()
  356. if c != '"' {
  357. iter.ReportError("stringModeDecoder", `expect "`)
  358. return
  359. }
  360. }
  361. type stringModeEncoder struct {
  362. elemEncoder ValEncoder
  363. }
  364. func (encoder *stringModeEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  365. stream.writeByte('"')
  366. encoder.elemEncoder.Encode(ptr, stream)
  367. stream.writeByte('"')
  368. }
  369. func (encoder *stringModeEncoder) EncodeInterface(val interface{}, stream *Stream) {
  370. writeToStream(val, stream, encoder)
  371. }
  372. func (encoder *stringModeEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  373. return encoder.elemEncoder.IsEmpty(ptr)
  374. }
  375. type marshalerEncoder struct {
  376. templateInterface emptyInterface
  377. }
  378. func (encoder *marshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  379. templateInterface := encoder.templateInterface
  380. templateInterface.word = ptr
  381. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  382. marshaler := (*realInterface).(json.Marshaler)
  383. bytes, err := marshaler.MarshalJSON()
  384. if err != nil {
  385. stream.Error = err
  386. } else {
  387. stream.Write(bytes)
  388. }
  389. }
  390. func (encoder *marshalerEncoder) EncodeInterface(val interface{}, stream *Stream) {
  391. writeToStream(val, stream, encoder)
  392. }
  393. func (encoder *marshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  394. templateInterface := encoder.templateInterface
  395. templateInterface.word = ptr
  396. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  397. marshaler := (*realInterface).(json.Marshaler)
  398. bytes, err := marshaler.MarshalJSON()
  399. if err != nil {
  400. return true
  401. } else {
  402. return len(bytes) > 0
  403. }
  404. }
  405. type unmarshalerDecoder struct {
  406. templateInterface emptyInterface
  407. }
  408. func (decoder *unmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  409. templateInterface := decoder.templateInterface
  410. templateInterface.word = ptr
  411. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  412. unmarshaler := (*realInterface).(json.Unmarshaler)
  413. bytes := iter.SkipAndReturnBytes()
  414. err := unmarshaler.UnmarshalJSON(bytes)
  415. if err != nil {
  416. iter.ReportError("unmarshaler", err.Error())
  417. }
  418. }