feature_reflect_native.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. package jsoniter
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "reflect"
  6. "unsafe"
  7. "github.com/v2pro/plz/reflect2"
  8. )
  9. type stringCodec struct {
  10. }
  11. func (codec *stringCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  12. *((*string)(ptr)) = iter.ReadString()
  13. }
  14. func (codec *stringCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  15. str := *((*string)(ptr))
  16. stream.WriteString(str)
  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. if !iter.ReadNil() {
  25. *((*int)(ptr)) = iter.ReadInt()
  26. }
  27. }
  28. func (codec *intCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  29. stream.WriteInt(*((*int)(ptr)))
  30. }
  31. func (codec *intCodec) IsEmpty(ptr unsafe.Pointer) bool {
  32. return *((*int)(ptr)) == 0
  33. }
  34. type uintptrCodec struct {
  35. }
  36. func (codec *uintptrCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  37. if !iter.ReadNil() {
  38. *((*uintptr)(ptr)) = uintptr(iter.ReadUint64())
  39. }
  40. }
  41. func (codec *uintptrCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  42. stream.WriteUint64(uint64(*((*uintptr)(ptr))))
  43. }
  44. func (codec *uintptrCodec) IsEmpty(ptr unsafe.Pointer) bool {
  45. return *((*uintptr)(ptr)) == 0
  46. }
  47. type int8Codec struct {
  48. }
  49. func (codec *int8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  50. if !iter.ReadNil() {
  51. *((*int8)(ptr)) = iter.ReadInt8()
  52. }
  53. }
  54. func (codec *int8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  55. stream.WriteInt8(*((*int8)(ptr)))
  56. }
  57. func (codec *int8Codec) IsEmpty(ptr unsafe.Pointer) bool {
  58. return *((*int8)(ptr)) == 0
  59. }
  60. type int16Codec struct {
  61. }
  62. func (codec *int16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  63. if !iter.ReadNil() {
  64. *((*int16)(ptr)) = iter.ReadInt16()
  65. }
  66. }
  67. func (codec *int16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  68. stream.WriteInt16(*((*int16)(ptr)))
  69. }
  70. func (codec *int16Codec) IsEmpty(ptr unsafe.Pointer) bool {
  71. return *((*int16)(ptr)) == 0
  72. }
  73. type int32Codec struct {
  74. }
  75. func (codec *int32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  76. if !iter.ReadNil() {
  77. *((*int32)(ptr)) = iter.ReadInt32()
  78. }
  79. }
  80. func (codec *int32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  81. stream.WriteInt32(*((*int32)(ptr)))
  82. }
  83. func (codec *int32Codec) IsEmpty(ptr unsafe.Pointer) bool {
  84. return *((*int32)(ptr)) == 0
  85. }
  86. type int64Codec struct {
  87. }
  88. func (codec *int64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  89. if !iter.ReadNil() {
  90. *((*int64)(ptr)) = iter.ReadInt64()
  91. }
  92. }
  93. func (codec *int64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  94. stream.WriteInt64(*((*int64)(ptr)))
  95. }
  96. func (codec *int64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  97. return *((*int64)(ptr)) == 0
  98. }
  99. type uintCodec struct {
  100. }
  101. func (codec *uintCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  102. if !iter.ReadNil() {
  103. *((*uint)(ptr)) = iter.ReadUint()
  104. return
  105. }
  106. }
  107. func (codec *uintCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  108. stream.WriteUint(*((*uint)(ptr)))
  109. }
  110. func (codec *uintCodec) IsEmpty(ptr unsafe.Pointer) bool {
  111. return *((*uint)(ptr)) == 0
  112. }
  113. type uint8Codec struct {
  114. }
  115. func (codec *uint8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  116. if !iter.ReadNil() {
  117. *((*uint8)(ptr)) = iter.ReadUint8()
  118. }
  119. }
  120. func (codec *uint8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  121. stream.WriteUint8(*((*uint8)(ptr)))
  122. }
  123. func (codec *uint8Codec) IsEmpty(ptr unsafe.Pointer) bool {
  124. return *((*uint8)(ptr)) == 0
  125. }
  126. type uint16Codec struct {
  127. }
  128. func (codec *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  129. if !iter.ReadNil() {
  130. *((*uint16)(ptr)) = iter.ReadUint16()
  131. }
  132. }
  133. func (codec *uint16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  134. stream.WriteUint16(*((*uint16)(ptr)))
  135. }
  136. func (codec *uint16Codec) IsEmpty(ptr unsafe.Pointer) bool {
  137. return *((*uint16)(ptr)) == 0
  138. }
  139. type uint32Codec struct {
  140. }
  141. func (codec *uint32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  142. if !iter.ReadNil() {
  143. *((*uint32)(ptr)) = iter.ReadUint32()
  144. }
  145. }
  146. func (codec *uint32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  147. stream.WriteUint32(*((*uint32)(ptr)))
  148. }
  149. func (codec *uint32Codec) IsEmpty(ptr unsafe.Pointer) bool {
  150. return *((*uint32)(ptr)) == 0
  151. }
  152. type uint64Codec struct {
  153. }
  154. func (codec *uint64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  155. if !iter.ReadNil() {
  156. *((*uint64)(ptr)) = iter.ReadUint64()
  157. }
  158. }
  159. func (codec *uint64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  160. stream.WriteUint64(*((*uint64)(ptr)))
  161. }
  162. func (codec *uint64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  163. return *((*uint64)(ptr)) == 0
  164. }
  165. type float32Codec struct {
  166. }
  167. func (codec *float32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  168. if !iter.ReadNil() {
  169. *((*float32)(ptr)) = iter.ReadFloat32()
  170. }
  171. }
  172. func (codec *float32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  173. stream.WriteFloat32(*((*float32)(ptr)))
  174. }
  175. func (codec *float32Codec) IsEmpty(ptr unsafe.Pointer) bool {
  176. return *((*float32)(ptr)) == 0
  177. }
  178. type float64Codec struct {
  179. }
  180. func (codec *float64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  181. if !iter.ReadNil() {
  182. *((*float64)(ptr)) = iter.ReadFloat64()
  183. }
  184. }
  185. func (codec *float64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  186. stream.WriteFloat64(*((*float64)(ptr)))
  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. if !iter.ReadNil() {
  195. *((*bool)(ptr)) = iter.ReadBool()
  196. }
  197. }
  198. func (codec *boolCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  199. stream.WriteBool(*((*bool)(ptr)))
  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. existing := *((*interface{})(ptr))
  208. // Checking for both typed and untyped nil pointers.
  209. if existing != nil &&
  210. reflect.TypeOf(existing).Kind() == reflect.Ptr &&
  211. !reflect.ValueOf(existing).IsNil() {
  212. var ptrToExisting interface{}
  213. for {
  214. elem := reflect.ValueOf(existing).Elem()
  215. if elem.Kind() != reflect.Ptr || elem.IsNil() {
  216. break
  217. }
  218. ptrToExisting = existing
  219. existing = elem.Interface()
  220. }
  221. if iter.ReadNil() {
  222. if ptrToExisting != nil {
  223. nilPtr := reflect.Zero(reflect.TypeOf(ptrToExisting).Elem())
  224. reflect.ValueOf(ptrToExisting).Elem().Set(nilPtr)
  225. } else {
  226. *((*interface{})(ptr)) = nil
  227. }
  228. } else {
  229. iter.ReadVal(existing)
  230. }
  231. return
  232. }
  233. if iter.ReadNil() {
  234. *((*interface{})(ptr)) = nil
  235. } else {
  236. *((*interface{})(ptr)) = iter.Read()
  237. }
  238. }
  239. func (codec *emptyInterfaceCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  240. obj := *((*interface{})(ptr))
  241. stream.WriteVal(obj)
  242. }
  243. func (codec *emptyInterfaceCodec) IsEmpty(ptr unsafe.Pointer) bool {
  244. emptyInterface := (*emptyInterface)(ptr)
  245. return emptyInterface.typ == nil
  246. }
  247. type nonEmptyInterfaceCodec struct {
  248. }
  249. func (codec *nonEmptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  250. if iter.WhatIsNext() == NilValue {
  251. iter.skipFourBytes('n', 'u', 'l', 'l')
  252. *((*interface{})(ptr)) = nil
  253. return
  254. }
  255. nonEmptyInterface := (*nonEmptyInterface)(ptr)
  256. if nonEmptyInterface.itab == nil {
  257. iter.ReportError("read non-empty interface", "do not know which concrete type to decode to")
  258. return
  259. }
  260. var i interface{}
  261. e := (*emptyInterface)(unsafe.Pointer(&i))
  262. e.typ = nonEmptyInterface.itab.typ
  263. e.word = nonEmptyInterface.word
  264. iter.ReadVal(&i)
  265. if e.word == nil {
  266. nonEmptyInterface.itab = nil
  267. }
  268. nonEmptyInterface.word = e.word
  269. }
  270. func (codec *nonEmptyInterfaceCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  271. nonEmptyInterface := (*nonEmptyInterface)(ptr)
  272. var i interface{}
  273. if nonEmptyInterface.itab != nil {
  274. e := (*emptyInterface)(unsafe.Pointer(&i))
  275. e.typ = nonEmptyInterface.itab.typ
  276. e.word = nonEmptyInterface.word
  277. }
  278. stream.WriteVal(i)
  279. }
  280. func (codec *nonEmptyInterfaceCodec) IsEmpty(ptr unsafe.Pointer) bool {
  281. nonEmptyInterface := (*nonEmptyInterface)(ptr)
  282. return nonEmptyInterface.word == nil
  283. }
  284. type dynamicEncoder struct {
  285. valType reflect2.Type
  286. }
  287. func (encoder *dynamicEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  288. obj := encoder.valType.UnsafeIndirect(ptr)
  289. stream.WriteVal(obj)
  290. }
  291. func (encoder *dynamicEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  292. return encoder.valType.UnsafeIndirect(ptr) == nil
  293. }
  294. type anyCodec struct {
  295. }
  296. func (codec *anyCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  297. *((*Any)(ptr)) = iter.ReadAny()
  298. }
  299. func (codec *anyCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  300. (*((*Any)(ptr))).WriteTo(stream)
  301. }
  302. func (codec *anyCodec) IsEmpty(ptr unsafe.Pointer) bool {
  303. return (*((*Any)(ptr))).Size() == 0
  304. }
  305. type jsonNumberCodec struct {
  306. }
  307. func (codec *jsonNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  308. switch iter.WhatIsNext() {
  309. case StringValue:
  310. *((*json.Number)(ptr)) = json.Number(iter.ReadString())
  311. case NilValue:
  312. iter.skipFourBytes('n', 'u', 'l', 'l')
  313. *((*json.Number)(ptr)) = ""
  314. default:
  315. *((*json.Number)(ptr)) = json.Number([]byte(iter.readNumberAsString()))
  316. }
  317. }
  318. func (codec *jsonNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  319. number := *((*json.Number)(ptr))
  320. if len(number) == 0 {
  321. stream.writeByte('0')
  322. } else {
  323. stream.WriteRaw(string(number))
  324. }
  325. }
  326. func (codec *jsonNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
  327. return len(*((*json.Number)(ptr))) == 0
  328. }
  329. type jsoniterNumberCodec struct {
  330. }
  331. func (codec *jsoniterNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  332. switch iter.WhatIsNext() {
  333. case StringValue:
  334. *((*Number)(ptr)) = Number(iter.ReadString())
  335. case NilValue:
  336. iter.skipFourBytes('n', 'u', 'l', 'l')
  337. *((*Number)(ptr)) = ""
  338. default:
  339. *((*Number)(ptr)) = Number([]byte(iter.readNumberAsString()))
  340. }
  341. }
  342. func (codec *jsoniterNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  343. number := *((*Number)(ptr))
  344. if len(number) == 0 {
  345. stream.writeByte('0')
  346. } else {
  347. stream.WriteRaw(string(number))
  348. }
  349. }
  350. func (codec *jsoniterNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
  351. return len(*((*Number)(ptr))) == 0
  352. }
  353. type jsonRawMessageCodec struct {
  354. }
  355. func (codec *jsonRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  356. *((*json.RawMessage)(ptr)) = json.RawMessage(iter.SkipAndReturnBytes())
  357. }
  358. func (codec *jsonRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  359. stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
  360. }
  361. func (codec *jsonRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
  362. return len(*((*json.RawMessage)(ptr))) == 0
  363. }
  364. type jsoniterRawMessageCodec struct {
  365. }
  366. func (codec *jsoniterRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  367. *((*RawMessage)(ptr)) = RawMessage(iter.SkipAndReturnBytes())
  368. }
  369. func (codec *jsoniterRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  370. stream.WriteRaw(string(*((*RawMessage)(ptr))))
  371. }
  372. func (codec *jsoniterRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
  373. return len(*((*RawMessage)(ptr))) == 0
  374. }
  375. type base64Codec struct {
  376. sliceDecoder ValDecoder
  377. }
  378. func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  379. if iter.ReadNil() {
  380. ptrSlice := (*sliceHeader)(ptr)
  381. ptrSlice.Len = 0
  382. ptrSlice.Cap = 0
  383. ptrSlice.Data = nil
  384. return
  385. }
  386. switch iter.WhatIsNext() {
  387. case StringValue:
  388. encoding := base64.StdEncoding
  389. src := iter.SkipAndReturnBytes()
  390. src = src[1: len(src)-1]
  391. decodedLen := encoding.DecodedLen(len(src))
  392. dst := make([]byte, decodedLen)
  393. len, err := encoding.Decode(dst, src)
  394. if err != nil {
  395. iter.ReportError("decode base64", err.Error())
  396. } else {
  397. dst = dst[:len]
  398. dstSlice := (*sliceHeader)(unsafe.Pointer(&dst))
  399. ptrSlice := (*sliceHeader)(ptr)
  400. ptrSlice.Data = dstSlice.Data
  401. ptrSlice.Cap = dstSlice.Cap
  402. ptrSlice.Len = dstSlice.Len
  403. }
  404. case ArrayValue:
  405. codec.sliceDecoder.Decode(ptr, iter)
  406. default:
  407. iter.ReportError("base64Codec", "invalid input")
  408. }
  409. }
  410. func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  411. src := *((*[]byte)(ptr))
  412. if len(src) == 0 {
  413. stream.WriteNil()
  414. return
  415. }
  416. encoding := base64.StdEncoding
  417. stream.writeByte('"')
  418. size := encoding.EncodedLen(len(src))
  419. buf := make([]byte, size)
  420. encoding.Encode(buf, src)
  421. stream.buf = append(stream.buf, buf...)
  422. stream.writeByte('"')
  423. }
  424. func (codec *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  425. return len(*((*[]byte)(ptr))) == 0
  426. }
  427. type stringModeNumberDecoder struct {
  428. elemDecoder ValDecoder
  429. }
  430. func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  431. c := iter.nextToken()
  432. if c != '"' {
  433. iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
  434. return
  435. }
  436. decoder.elemDecoder.Decode(ptr, iter)
  437. if iter.Error != nil {
  438. return
  439. }
  440. c = iter.readByte()
  441. if c != '"' {
  442. iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
  443. return
  444. }
  445. }
  446. type stringModeStringDecoder struct {
  447. elemDecoder ValDecoder
  448. cfg *frozenConfig
  449. }
  450. func (decoder *stringModeStringDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  451. decoder.elemDecoder.Decode(ptr, iter)
  452. str := *((*string)(ptr))
  453. tempIter := decoder.cfg.BorrowIterator([]byte(str))
  454. defer decoder.cfg.ReturnIterator(tempIter)
  455. *((*string)(ptr)) = tempIter.ReadString()
  456. }
  457. type stringModeNumberEncoder struct {
  458. elemEncoder ValEncoder
  459. }
  460. func (encoder *stringModeNumberEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  461. stream.writeByte('"')
  462. encoder.elemEncoder.Encode(ptr, stream)
  463. stream.writeByte('"')
  464. }
  465. func (encoder *stringModeNumberEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  466. return encoder.elemEncoder.IsEmpty(ptr)
  467. }
  468. type stringModeStringEncoder struct {
  469. elemEncoder ValEncoder
  470. cfg *frozenConfig
  471. }
  472. func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  473. tempStream := encoder.cfg.BorrowStream(nil)
  474. defer encoder.cfg.ReturnStream(tempStream)
  475. encoder.elemEncoder.Encode(ptr, tempStream)
  476. stream.WriteString(string(tempStream.Buffer()))
  477. }
  478. func (encoder *stringModeStringEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  479. return encoder.elemEncoder.IsEmpty(ptr)
  480. }