feature_reflect_native.go 14 KB

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