feature_reflect_native.go 18 KB

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