reflect_native.go 13 KB

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