feature_reflect_native.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. valType reflect2.Type
  296. }
  297. func (codec *anyCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  298. panic("not implemented")
  299. }
  300. func (codec *anyCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  301. obj := codec.valType.UnsafeIndirect(ptr)
  302. any := obj.(Any)
  303. any.WriteTo(stream)
  304. }
  305. func (codec *anyCodec) IsEmpty(ptr unsafe.Pointer) bool {
  306. obj := codec.valType.UnsafeIndirect(ptr)
  307. any := obj.(Any)
  308. return any.Size() == 0
  309. }
  310. type directAnyCodec struct {
  311. }
  312. func (codec *directAnyCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  313. *(*Any)(ptr) = iter.readAny()
  314. }
  315. func (codec *directAnyCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  316. any := *(*Any)(ptr)
  317. any.WriteTo(stream)
  318. }
  319. func (codec *directAnyCodec) IsEmpty(ptr unsafe.Pointer) bool {
  320. any := *(*Any)(ptr)
  321. return any.Size() == 0
  322. }
  323. type jsonNumberCodec struct {
  324. }
  325. func (codec *jsonNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  326. switch iter.WhatIsNext() {
  327. case StringValue:
  328. *((*json.Number)(ptr)) = json.Number(iter.ReadString())
  329. case NilValue:
  330. iter.skipFourBytes('n', 'u', 'l', 'l')
  331. *((*json.Number)(ptr)) = ""
  332. default:
  333. *((*json.Number)(ptr)) = json.Number([]byte(iter.readNumberAsString()))
  334. }
  335. }
  336. func (codec *jsonNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  337. number := *((*json.Number)(ptr))
  338. if len(number) == 0 {
  339. stream.writeByte('0')
  340. } else {
  341. stream.WriteRaw(string(number))
  342. }
  343. }
  344. func (codec *jsonNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
  345. return len(*((*json.Number)(ptr))) == 0
  346. }
  347. type jsoniterNumberCodec struct {
  348. }
  349. func (codec *jsoniterNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  350. switch iter.WhatIsNext() {
  351. case StringValue:
  352. *((*Number)(ptr)) = Number(iter.ReadString())
  353. case NilValue:
  354. iter.skipFourBytes('n', 'u', 'l', 'l')
  355. *((*Number)(ptr)) = ""
  356. default:
  357. *((*Number)(ptr)) = Number([]byte(iter.readNumberAsString()))
  358. }
  359. }
  360. func (codec *jsoniterNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  361. number := *((*Number)(ptr))
  362. if len(number) == 0 {
  363. stream.writeByte('0')
  364. } else {
  365. stream.WriteRaw(string(number))
  366. }
  367. }
  368. func (codec *jsoniterNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
  369. return len(*((*Number)(ptr))) == 0
  370. }
  371. type jsonRawMessageCodec struct {
  372. }
  373. func (codec *jsonRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  374. *((*json.RawMessage)(ptr)) = json.RawMessage(iter.SkipAndReturnBytes())
  375. }
  376. func (codec *jsonRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  377. stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
  378. }
  379. func (codec *jsonRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
  380. return len(*((*json.RawMessage)(ptr))) == 0
  381. }
  382. type jsoniterRawMessageCodec struct {
  383. }
  384. func (codec *jsoniterRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  385. *((*RawMessage)(ptr)) = RawMessage(iter.SkipAndReturnBytes())
  386. }
  387. func (codec *jsoniterRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  388. stream.WriteRaw(string(*((*RawMessage)(ptr))))
  389. }
  390. func (codec *jsoniterRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
  391. return len(*((*RawMessage)(ptr))) == 0
  392. }
  393. type base64Codec struct {
  394. sliceDecoder ValDecoder
  395. }
  396. func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  397. if iter.ReadNil() {
  398. ptrSlice := (*sliceHeader)(ptr)
  399. ptrSlice.Len = 0
  400. ptrSlice.Cap = 0
  401. ptrSlice.Data = nil
  402. return
  403. }
  404. switch iter.WhatIsNext() {
  405. case StringValue:
  406. encoding := base64.StdEncoding
  407. src := iter.SkipAndReturnBytes()
  408. src = src[1: len(src)-1]
  409. decodedLen := encoding.DecodedLen(len(src))
  410. dst := make([]byte, decodedLen)
  411. len, err := encoding.Decode(dst, src)
  412. if err != nil {
  413. iter.ReportError("decode base64", err.Error())
  414. } else {
  415. dst = dst[:len]
  416. dstSlice := (*sliceHeader)(unsafe.Pointer(&dst))
  417. ptrSlice := (*sliceHeader)(ptr)
  418. ptrSlice.Data = dstSlice.Data
  419. ptrSlice.Cap = dstSlice.Cap
  420. ptrSlice.Len = dstSlice.Len
  421. }
  422. case ArrayValue:
  423. codec.sliceDecoder.Decode(ptr, iter)
  424. default:
  425. iter.ReportError("base64Codec", "invalid input")
  426. }
  427. }
  428. func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  429. src := *((*[]byte)(ptr))
  430. if len(src) == 0 {
  431. stream.WriteNil()
  432. return
  433. }
  434. encoding := base64.StdEncoding
  435. stream.writeByte('"')
  436. size := encoding.EncodedLen(len(src))
  437. buf := make([]byte, size)
  438. encoding.Encode(buf, src)
  439. stream.buf = append(stream.buf, buf...)
  440. stream.writeByte('"')
  441. }
  442. func (codec *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  443. return len(*((*[]byte)(ptr))) == 0
  444. }
  445. type stringModeNumberDecoder struct {
  446. elemDecoder ValDecoder
  447. }
  448. func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  449. c := iter.nextToken()
  450. if c != '"' {
  451. iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
  452. return
  453. }
  454. decoder.elemDecoder.Decode(ptr, iter)
  455. if iter.Error != nil {
  456. return
  457. }
  458. c = iter.readByte()
  459. if c != '"' {
  460. iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
  461. return
  462. }
  463. }
  464. type stringModeStringDecoder struct {
  465. elemDecoder ValDecoder
  466. cfg *frozenConfig
  467. }
  468. func (decoder *stringModeStringDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  469. decoder.elemDecoder.Decode(ptr, iter)
  470. str := *((*string)(ptr))
  471. tempIter := decoder.cfg.BorrowIterator([]byte(str))
  472. defer decoder.cfg.ReturnIterator(tempIter)
  473. *((*string)(ptr)) = tempIter.ReadString()
  474. }
  475. type stringModeNumberEncoder struct {
  476. elemEncoder ValEncoder
  477. }
  478. func (encoder *stringModeNumberEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  479. stream.writeByte('"')
  480. encoder.elemEncoder.Encode(ptr, stream)
  481. stream.writeByte('"')
  482. }
  483. func (encoder *stringModeNumberEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  484. return encoder.elemEncoder.IsEmpty(ptr)
  485. }
  486. type stringModeStringEncoder struct {
  487. elemEncoder ValEncoder
  488. cfg *frozenConfig
  489. }
  490. func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  491. tempStream := encoder.cfg.BorrowStream(nil)
  492. defer encoder.cfg.ReturnStream(tempStream)
  493. encoder.elemEncoder.Encode(ptr, tempStream)
  494. stream.WriteString(string(tempStream.Buffer()))
  495. }
  496. func (encoder *stringModeStringEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  497. return encoder.elemEncoder.IsEmpty(ptr)
  498. }