feature_reflect_native.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. package jsoniter
  2. import (
  3. "encoding"
  4. "encoding/base64"
  5. "encoding/json"
  6. "reflect"
  7. "unsafe"
  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) EncodeInterface(val interface{}, stream *Stream) {
  19. WriteToStream(val, stream, codec)
  20. }
  21. func (codec *stringCodec) IsEmpty(ptr unsafe.Pointer) bool {
  22. return *((*string)(ptr)) == ""
  23. }
  24. type intCodec struct {
  25. }
  26. func (codec *intCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  27. if !iter.ReadNil() {
  28. *((*int)(ptr)) = iter.ReadInt()
  29. }
  30. }
  31. func (codec *intCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  32. stream.WriteInt(*((*int)(ptr)))
  33. }
  34. func (codec *intCodec) EncodeInterface(val interface{}, stream *Stream) {
  35. WriteToStream(val, stream, codec)
  36. }
  37. func (codec *intCodec) IsEmpty(ptr unsafe.Pointer) bool {
  38. return *((*int)(ptr)) == 0
  39. }
  40. type uintptrCodec struct {
  41. }
  42. func (codec *uintptrCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  43. if !iter.ReadNil() {
  44. *((*uintptr)(ptr)) = uintptr(iter.ReadUint64())
  45. }
  46. }
  47. func (codec *uintptrCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  48. stream.WriteUint64(uint64(*((*uintptr)(ptr))))
  49. }
  50. func (codec *uintptrCodec) EncodeInterface(val interface{}, stream *Stream) {
  51. WriteToStream(val, stream, codec)
  52. }
  53. func (codec *uintptrCodec) IsEmpty(ptr unsafe.Pointer) bool {
  54. return *((*uintptr)(ptr)) == 0
  55. }
  56. type int8Codec struct {
  57. }
  58. func (codec *int8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  59. if !iter.ReadNil() {
  60. *((*int8)(ptr)) = iter.ReadInt8()
  61. }
  62. }
  63. func (codec *int8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  64. stream.WriteInt8(*((*int8)(ptr)))
  65. }
  66. func (codec *int8Codec) EncodeInterface(val interface{}, stream *Stream) {
  67. WriteToStream(val, stream, codec)
  68. }
  69. func (codec *int8Codec) IsEmpty(ptr unsafe.Pointer) bool {
  70. return *((*int8)(ptr)) == 0
  71. }
  72. type int16Codec struct {
  73. }
  74. func (codec *int16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  75. if !iter.ReadNil() {
  76. *((*int16)(ptr)) = iter.ReadInt16()
  77. }
  78. }
  79. func (codec *int16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  80. stream.WriteInt16(*((*int16)(ptr)))
  81. }
  82. func (codec *int16Codec) EncodeInterface(val interface{}, stream *Stream) {
  83. WriteToStream(val, stream, codec)
  84. }
  85. func (codec *int16Codec) IsEmpty(ptr unsafe.Pointer) bool {
  86. return *((*int16)(ptr)) == 0
  87. }
  88. type int32Codec struct {
  89. }
  90. func (codec *int32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  91. if !iter.ReadNil() {
  92. *((*int32)(ptr)) = iter.ReadInt32()
  93. }
  94. }
  95. func (codec *int32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  96. stream.WriteInt32(*((*int32)(ptr)))
  97. }
  98. func (codec *int32Codec) EncodeInterface(val interface{}, stream *Stream) {
  99. WriteToStream(val, stream, codec)
  100. }
  101. func (codec *int32Codec) IsEmpty(ptr unsafe.Pointer) bool {
  102. return *((*int32)(ptr)) == 0
  103. }
  104. type int64Codec struct {
  105. }
  106. func (codec *int64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  107. if !iter.ReadNil() {
  108. *((*int64)(ptr)) = iter.ReadInt64()
  109. }
  110. }
  111. func (codec *int64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  112. stream.WriteInt64(*((*int64)(ptr)))
  113. }
  114. func (codec *int64Codec) EncodeInterface(val interface{}, stream *Stream) {
  115. WriteToStream(val, stream, codec)
  116. }
  117. func (codec *int64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  118. return *((*int64)(ptr)) == 0
  119. }
  120. type uintCodec struct {
  121. }
  122. func (codec *uintCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  123. if !iter.ReadNil() {
  124. *((*uint)(ptr)) = iter.ReadUint()
  125. return
  126. }
  127. }
  128. func (codec *uintCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  129. stream.WriteUint(*((*uint)(ptr)))
  130. }
  131. func (codec *uintCodec) EncodeInterface(val interface{}, stream *Stream) {
  132. WriteToStream(val, stream, codec)
  133. }
  134. func (codec *uintCodec) IsEmpty(ptr unsafe.Pointer) bool {
  135. return *((*uint)(ptr)) == 0
  136. }
  137. type uint8Codec struct {
  138. }
  139. func (codec *uint8Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  140. if !iter.ReadNil() {
  141. *((*uint8)(ptr)) = iter.ReadUint8()
  142. }
  143. }
  144. func (codec *uint8Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  145. stream.WriteUint8(*((*uint8)(ptr)))
  146. }
  147. func (codec *uint8Codec) EncodeInterface(val interface{}, stream *Stream) {
  148. WriteToStream(val, stream, codec)
  149. }
  150. func (codec *uint8Codec) IsEmpty(ptr unsafe.Pointer) bool {
  151. return *((*uint8)(ptr)) == 0
  152. }
  153. type uint16Codec struct {
  154. }
  155. func (codec *uint16Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  156. if !iter.ReadNil() {
  157. *((*uint16)(ptr)) = iter.ReadUint16()
  158. }
  159. }
  160. func (codec *uint16Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  161. stream.WriteUint16(*((*uint16)(ptr)))
  162. }
  163. func (codec *uint16Codec) EncodeInterface(val interface{}, stream *Stream) {
  164. WriteToStream(val, stream, codec)
  165. }
  166. func (codec *uint16Codec) IsEmpty(ptr unsafe.Pointer) bool {
  167. return *((*uint16)(ptr)) == 0
  168. }
  169. type uint32Codec struct {
  170. }
  171. func (codec *uint32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  172. if !iter.ReadNil() {
  173. *((*uint32)(ptr)) = iter.ReadUint32()
  174. }
  175. }
  176. func (codec *uint32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  177. stream.WriteUint32(*((*uint32)(ptr)))
  178. }
  179. func (codec *uint32Codec) EncodeInterface(val interface{}, stream *Stream) {
  180. WriteToStream(val, stream, codec)
  181. }
  182. func (codec *uint32Codec) IsEmpty(ptr unsafe.Pointer) bool {
  183. return *((*uint32)(ptr)) == 0
  184. }
  185. type uint64Codec struct {
  186. }
  187. func (codec *uint64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  188. if !iter.ReadNil() {
  189. *((*uint64)(ptr)) = iter.ReadUint64()
  190. }
  191. }
  192. func (codec *uint64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  193. stream.WriteUint64(*((*uint64)(ptr)))
  194. }
  195. func (codec *uint64Codec) EncodeInterface(val interface{}, stream *Stream) {
  196. WriteToStream(val, stream, codec)
  197. }
  198. func (codec *uint64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  199. return *((*uint64)(ptr)) == 0
  200. }
  201. type float32Codec struct {
  202. }
  203. func (codec *float32Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  204. if !iter.ReadNil() {
  205. *((*float32)(ptr)) = iter.ReadFloat32()
  206. }
  207. }
  208. func (codec *float32Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  209. stream.WriteFloat32(*((*float32)(ptr)))
  210. }
  211. func (codec *float32Codec) EncodeInterface(val interface{}, stream *Stream) {
  212. WriteToStream(val, stream, codec)
  213. }
  214. func (codec *float32Codec) IsEmpty(ptr unsafe.Pointer) bool {
  215. return *((*float32)(ptr)) == 0
  216. }
  217. type float64Codec struct {
  218. }
  219. func (codec *float64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  220. if !iter.ReadNil() {
  221. *((*float64)(ptr)) = iter.ReadFloat64()
  222. }
  223. }
  224. func (codec *float64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  225. stream.WriteFloat64(*((*float64)(ptr)))
  226. }
  227. func (codec *float64Codec) EncodeInterface(val interface{}, stream *Stream) {
  228. WriteToStream(val, stream, codec)
  229. }
  230. func (codec *float64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  231. return *((*float64)(ptr)) == 0
  232. }
  233. type boolCodec struct {
  234. }
  235. func (codec *boolCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  236. if !iter.ReadNil() {
  237. *((*bool)(ptr)) = iter.ReadBool()
  238. }
  239. }
  240. func (codec *boolCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  241. stream.WriteBool(*((*bool)(ptr)))
  242. }
  243. func (codec *boolCodec) EncodeInterface(val interface{}, stream *Stream) {
  244. WriteToStream(val, stream, codec)
  245. }
  246. func (codec *boolCodec) IsEmpty(ptr unsafe.Pointer) bool {
  247. return !(*((*bool)(ptr)))
  248. }
  249. type emptyInterfaceCodec struct {
  250. }
  251. func (codec *emptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  252. existing := *((*interface{})(ptr))
  253. // Checking for both typed and untyped nil pointers.
  254. if existing != nil &&
  255. reflect.TypeOf(existing).Kind() == reflect.Ptr &&
  256. !reflect.ValueOf(existing).IsNil() {
  257. var ptrToExisting interface{}
  258. for {
  259. elem := reflect.ValueOf(existing).Elem()
  260. if elem.Kind() != reflect.Ptr || elem.IsNil() {
  261. break
  262. }
  263. ptrToExisting = existing
  264. existing = elem.Interface()
  265. }
  266. if iter.ReadNil() {
  267. if ptrToExisting != nil {
  268. nilPtr := reflect.Zero(reflect.TypeOf(ptrToExisting).Elem())
  269. reflect.ValueOf(ptrToExisting).Elem().Set(nilPtr)
  270. } else {
  271. *((*interface{})(ptr)) = nil
  272. }
  273. } else {
  274. iter.ReadVal(existing)
  275. }
  276. return
  277. }
  278. if iter.ReadNil() {
  279. *((*interface{})(ptr)) = nil
  280. } else {
  281. *((*interface{})(ptr)) = iter.Read()
  282. }
  283. }
  284. func (codec *emptyInterfaceCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  285. stream.WriteVal(*((*interface{})(ptr)))
  286. }
  287. func (codec *emptyInterfaceCodec) EncodeInterface(val interface{}, stream *Stream) {
  288. stream.WriteVal(val)
  289. }
  290. func (codec *emptyInterfaceCodec) IsEmpty(ptr unsafe.Pointer) bool {
  291. emptyInterface := (*emptyInterface)(ptr)
  292. return emptyInterface.typ == nil
  293. }
  294. type nonEmptyInterfaceCodec struct {
  295. }
  296. func (codec *nonEmptyInterfaceCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  297. if iter.WhatIsNext() == NilValue {
  298. iter.skipFourBytes('n', 'u', 'l', 'l')
  299. *((*interface{})(ptr)) = nil
  300. return
  301. }
  302. nonEmptyInterface := (*nonEmptyInterface)(ptr)
  303. if nonEmptyInterface.itab == nil {
  304. iter.ReportError("read non-empty interface", "do not know which concrete type to decode to")
  305. return
  306. }
  307. var i interface{}
  308. e := (*emptyInterface)(unsafe.Pointer(&i))
  309. e.typ = nonEmptyInterface.itab.typ
  310. e.word = nonEmptyInterface.word
  311. iter.ReadVal(&i)
  312. if e.word == nil {
  313. nonEmptyInterface.itab = nil
  314. }
  315. nonEmptyInterface.word = e.word
  316. }
  317. func (codec *nonEmptyInterfaceCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  318. nonEmptyInterface := (*nonEmptyInterface)(ptr)
  319. var i interface{}
  320. if nonEmptyInterface.itab != nil {
  321. e := (*emptyInterface)(unsafe.Pointer(&i))
  322. e.typ = nonEmptyInterface.itab.typ
  323. e.word = nonEmptyInterface.word
  324. }
  325. stream.WriteVal(i)
  326. }
  327. func (codec *nonEmptyInterfaceCodec) EncodeInterface(val interface{}, stream *Stream) {
  328. stream.WriteVal(val)
  329. }
  330. func (codec *nonEmptyInterfaceCodec) IsEmpty(ptr unsafe.Pointer) bool {
  331. nonEmptyInterface := (*nonEmptyInterface)(ptr)
  332. return nonEmptyInterface.word == nil
  333. }
  334. type anyCodec struct {
  335. }
  336. func (codec *anyCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  337. *((*Any)(ptr)) = iter.ReadAny()
  338. }
  339. func (codec *anyCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  340. (*((*Any)(ptr))).WriteTo(stream)
  341. }
  342. func (codec *anyCodec) EncodeInterface(val interface{}, stream *Stream) {
  343. (val.(Any)).WriteTo(stream)
  344. }
  345. func (codec *anyCodec) IsEmpty(ptr unsafe.Pointer) bool {
  346. return (*((*Any)(ptr))).Size() == 0
  347. }
  348. type jsonNumberCodec struct {
  349. }
  350. func (codec *jsonNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  351. switch iter.WhatIsNext() {
  352. case StringValue:
  353. *((*json.Number)(ptr)) = json.Number(iter.ReadString())
  354. case NilValue:
  355. iter.skipFourBytes('n', 'u', 'l', 'l')
  356. *((*json.Number)(ptr)) = ""
  357. default:
  358. *((*json.Number)(ptr)) = json.Number([]byte(iter.readNumberAsString()))
  359. }
  360. }
  361. func (codec *jsonNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  362. number := *((*json.Number)(ptr))
  363. if len(number) == 0 {
  364. stream.WriteRaw("0")
  365. } else {
  366. stream.WriteRaw(string(number))
  367. }
  368. }
  369. func (codec *jsonNumberCodec) EncodeInterface(val interface{}, stream *Stream) {
  370. number := val.(json.Number)
  371. if len(number) == 0 {
  372. stream.WriteRaw("0")
  373. } else {
  374. stream.WriteRaw(string(number))
  375. }
  376. }
  377. func (codec *jsonNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
  378. return len(*((*json.Number)(ptr))) == 0
  379. }
  380. type jsoniterNumberCodec struct {
  381. }
  382. func (codec *jsoniterNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  383. switch iter.WhatIsNext() {
  384. case StringValue:
  385. *((*Number)(ptr)) = Number(iter.ReadString())
  386. case NilValue:
  387. iter.skipFourBytes('n', 'u', 'l', 'l')
  388. *((*Number)(ptr)) = ""
  389. default:
  390. *((*Number)(ptr)) = Number([]byte(iter.readNumberAsString()))
  391. }
  392. }
  393. func (codec *jsoniterNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  394. number := *((*Number)(ptr))
  395. if len(number) == 0 {
  396. stream.WriteRaw("0")
  397. } else {
  398. stream.WriteRaw(string(number))
  399. }
  400. }
  401. func (codec *jsoniterNumberCodec) EncodeInterface(val interface{}, stream *Stream) {
  402. number := val.(Number)
  403. if len(number) == 0 {
  404. stream.WriteRaw("0")
  405. } else {
  406. stream.WriteRaw(string(number))
  407. }
  408. }
  409. func (codec *jsoniterNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
  410. return len(*((*Number)(ptr))) == 0
  411. }
  412. type jsonRawMessageCodec struct {
  413. }
  414. func (codec *jsonRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  415. *((*json.RawMessage)(ptr)) = json.RawMessage(iter.SkipAndReturnBytes())
  416. }
  417. func (codec *jsonRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  418. stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
  419. }
  420. func (codec *jsonRawMessageCodec) EncodeInterface(val interface{}, stream *Stream) {
  421. stream.WriteRaw(string(val.(json.RawMessage)))
  422. }
  423. func (codec *jsonRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
  424. return len(*((*json.RawMessage)(ptr))) == 0
  425. }
  426. type jsoniterRawMessageCodec struct {
  427. }
  428. func (codec *jsoniterRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  429. *((*RawMessage)(ptr)) = RawMessage(iter.SkipAndReturnBytes())
  430. }
  431. func (codec *jsoniterRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  432. stream.WriteRaw(string(*((*RawMessage)(ptr))))
  433. }
  434. func (codec *jsoniterRawMessageCodec) EncodeInterface(val interface{}, stream *Stream) {
  435. stream.WriteRaw(string(val.(RawMessage)))
  436. }
  437. func (codec *jsoniterRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
  438. return len(*((*RawMessage)(ptr))) == 0
  439. }
  440. type base64Codec struct {
  441. sliceDecoder ValDecoder
  442. }
  443. func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  444. if iter.ReadNil() {
  445. ptrSlice := (*sliceHeader)(ptr)
  446. ptrSlice.Len = 0
  447. ptrSlice.Cap = 0
  448. ptrSlice.Data = nil
  449. return
  450. }
  451. switch iter.WhatIsNext() {
  452. case StringValue:
  453. encoding := base64.StdEncoding
  454. src := iter.SkipAndReturnBytes()
  455. src = src[1 : len(src)-1]
  456. decodedLen := encoding.DecodedLen(len(src))
  457. dst := make([]byte, decodedLen)
  458. len, err := encoding.Decode(dst, src)
  459. if err != nil {
  460. iter.ReportError("decode base64", err.Error())
  461. } else {
  462. dst = dst[:len]
  463. dstSlice := (*sliceHeader)(unsafe.Pointer(&dst))
  464. ptrSlice := (*sliceHeader)(ptr)
  465. ptrSlice.Data = dstSlice.Data
  466. ptrSlice.Cap = dstSlice.Cap
  467. ptrSlice.Len = dstSlice.Len
  468. }
  469. case ArrayValue:
  470. codec.sliceDecoder.Decode(ptr, iter)
  471. default:
  472. iter.ReportError("base64Codec", "invalid input")
  473. }
  474. }
  475. func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  476. src := *((*[]byte)(ptr))
  477. if len(src) == 0 {
  478. stream.WriteNil()
  479. return
  480. }
  481. encoding := base64.StdEncoding
  482. stream.writeByte('"')
  483. toGrow := encoding.EncodedLen(len(src))
  484. stream.ensure(toGrow)
  485. encoding.Encode(stream.buf[stream.n:], src)
  486. stream.n += toGrow
  487. stream.writeByte('"')
  488. }
  489. func (codec *base64Codec) EncodeInterface(val interface{}, stream *Stream) {
  490. ptr := extractInterface(val).word
  491. src := *((*[]byte)(ptr))
  492. if len(src) == 0 {
  493. stream.WriteNil()
  494. return
  495. }
  496. encoding := base64.StdEncoding
  497. stream.writeByte('"')
  498. toGrow := encoding.EncodedLen(len(src))
  499. stream.ensure(toGrow)
  500. encoding.Encode(stream.buf[stream.n:], src)
  501. stream.n += toGrow
  502. stream.writeByte('"')
  503. }
  504. func (codec *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  505. return len(*((*[]byte)(ptr))) == 0
  506. }
  507. type stringModeNumberDecoder struct {
  508. elemDecoder ValDecoder
  509. }
  510. func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  511. c := iter.nextToken()
  512. if c != '"' {
  513. iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
  514. return
  515. }
  516. decoder.elemDecoder.Decode(ptr, iter)
  517. if iter.Error != nil {
  518. return
  519. }
  520. c = iter.readByte()
  521. if c != '"' {
  522. iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
  523. return
  524. }
  525. }
  526. type stringModeStringDecoder struct {
  527. elemDecoder ValDecoder
  528. cfg *frozenConfig
  529. }
  530. func (decoder *stringModeStringDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  531. decoder.elemDecoder.Decode(ptr, iter)
  532. str := *((*string)(ptr))
  533. tempIter := decoder.cfg.BorrowIterator([]byte(str))
  534. defer decoder.cfg.ReturnIterator(tempIter)
  535. *((*string)(ptr)) = tempIter.ReadString()
  536. }
  537. type stringModeNumberEncoder struct {
  538. elemEncoder ValEncoder
  539. }
  540. func (encoder *stringModeNumberEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  541. stream.writeByte('"')
  542. encoder.elemEncoder.Encode(ptr, stream)
  543. stream.writeByte('"')
  544. }
  545. func (encoder *stringModeNumberEncoder) EncodeInterface(val interface{}, stream *Stream) {
  546. WriteToStream(val, stream, encoder)
  547. }
  548. func (encoder *stringModeNumberEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  549. return encoder.elemEncoder.IsEmpty(ptr)
  550. }
  551. type stringModeStringEncoder struct {
  552. elemEncoder ValEncoder
  553. cfg *frozenConfig
  554. }
  555. func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  556. tempStream := encoder.cfg.BorrowStream(nil)
  557. defer encoder.cfg.ReturnStream(tempStream)
  558. encoder.elemEncoder.Encode(ptr, tempStream)
  559. stream.WriteString(string(tempStream.Buffer()))
  560. }
  561. func (encoder *stringModeStringEncoder) EncodeInterface(val interface{}, stream *Stream) {
  562. WriteToStream(val, stream, encoder)
  563. }
  564. func (encoder *stringModeStringEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  565. return encoder.elemEncoder.IsEmpty(ptr)
  566. }
  567. type marshalerEncoder struct {
  568. templateInterface emptyInterface
  569. checkIsEmpty checkIsEmpty
  570. }
  571. func (encoder *marshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  572. templateInterface := encoder.templateInterface
  573. templateInterface.word = ptr
  574. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  575. marshaler, ok := (*realInterface).(json.Marshaler)
  576. if !ok {
  577. stream.WriteVal(nil)
  578. return
  579. }
  580. bytes, err := marshaler.MarshalJSON()
  581. if err != nil {
  582. stream.Error = err
  583. } else {
  584. stream.Write(bytes)
  585. }
  586. }
  587. func (encoder *marshalerEncoder) EncodeInterface(val interface{}, stream *Stream) {
  588. WriteToStream(val, stream, encoder)
  589. }
  590. func (encoder *marshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  591. return encoder.checkIsEmpty.IsEmpty(ptr)
  592. }
  593. type textMarshalerEncoder struct {
  594. templateInterface emptyInterface
  595. checkIsEmpty checkIsEmpty
  596. }
  597. func (encoder *textMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  598. templateInterface := encoder.templateInterface
  599. templateInterface.word = ptr
  600. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  601. marshaler := (*realInterface).(encoding.TextMarshaler)
  602. bytes, err := marshaler.MarshalText()
  603. if err != nil {
  604. stream.Error = err
  605. } else {
  606. stream.WriteString(string(bytes))
  607. }
  608. }
  609. func (encoder *textMarshalerEncoder) EncodeInterface(val interface{}, stream *Stream) {
  610. WriteToStream(val, stream, encoder)
  611. }
  612. func (encoder *textMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  613. return encoder.checkIsEmpty.IsEmpty(ptr)
  614. }
  615. type unmarshalerDecoder struct {
  616. templateInterface emptyInterface
  617. }
  618. func (decoder *unmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  619. templateInterface := decoder.templateInterface
  620. templateInterface.word = ptr
  621. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  622. unmarshaler := (*realInterface).(json.Unmarshaler)
  623. iter.nextToken()
  624. iter.unreadByte() // skip spaces
  625. bytes := iter.SkipAndReturnBytes()
  626. err := unmarshaler.UnmarshalJSON(bytes)
  627. if err != nil {
  628. iter.ReportError("unmarshalerDecoder", err.Error())
  629. }
  630. }
  631. type textUnmarshalerDecoder struct {
  632. templateInterface emptyInterface
  633. }
  634. func (decoder *textUnmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  635. templateInterface := decoder.templateInterface
  636. templateInterface.word = ptr
  637. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  638. unmarshaler := (*realInterface).(encoding.TextUnmarshaler)
  639. str := iter.ReadString()
  640. err := unmarshaler.UnmarshalText([]byte(str))
  641. if err != nil {
  642. iter.ReportError("textUnmarshalerDecoder", err.Error())
  643. }
  644. }