feature_reflect_native.go 18 KB

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