feature_reflect_native.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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. if e.word == nil {
  312. nonEmptyInterface.itab = nil
  313. }
  314. nonEmptyInterface.word = e.word
  315. }
  316. func (codec *nonEmptyInterfaceCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  317. nonEmptyInterface := (*nonEmptyInterface)(ptr)
  318. var i interface{}
  319. if nonEmptyInterface.itab != nil {
  320. e := (*emptyInterface)(unsafe.Pointer(&i))
  321. e.typ = nonEmptyInterface.itab.typ
  322. e.word = nonEmptyInterface.word
  323. }
  324. stream.WriteVal(i)
  325. }
  326. func (codec *nonEmptyInterfaceCodec) EncodeInterface(val interface{}, stream *Stream) {
  327. stream.WriteVal(val)
  328. }
  329. func (codec *nonEmptyInterfaceCodec) IsEmpty(ptr unsafe.Pointer) bool {
  330. nonEmptyInterface := (*nonEmptyInterface)(ptr)
  331. return nonEmptyInterface.word == nil
  332. }
  333. type anyCodec struct {
  334. }
  335. func (codec *anyCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  336. *((*Any)(ptr)) = iter.ReadAny()
  337. }
  338. func (codec *anyCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  339. (*((*Any)(ptr))).WriteTo(stream)
  340. }
  341. func (codec *anyCodec) EncodeInterface(val interface{}, stream *Stream) {
  342. (val.(Any)).WriteTo(stream)
  343. }
  344. func (codec *anyCodec) IsEmpty(ptr unsafe.Pointer) bool {
  345. return (*((*Any)(ptr))).Size() == 0
  346. }
  347. type jsonNumberCodec struct {
  348. }
  349. func (codec *jsonNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  350. *((*json.Number)(ptr)) = json.Number([]byte(iter.readNumberAsString()))
  351. }
  352. func (codec *jsonNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  353. stream.WriteRaw(string(*((*json.Number)(ptr))))
  354. }
  355. func (codec *jsonNumberCodec) EncodeInterface(val interface{}, stream *Stream) {
  356. stream.WriteRaw(string(val.(json.Number)))
  357. }
  358. func (codec *jsonNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
  359. return len(*((*json.Number)(ptr))) == 0
  360. }
  361. type jsoniterNumberCodec struct {
  362. }
  363. func (codec *jsoniterNumberCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  364. *((*Number)(ptr)) = Number([]byte(iter.readNumberAsString()))
  365. }
  366. func (codec *jsoniterNumberCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  367. stream.WriteRaw(string(*((*Number)(ptr))))
  368. }
  369. func (codec *jsoniterNumberCodec) EncodeInterface(val interface{}, stream *Stream) {
  370. stream.WriteRaw(string(val.(Number)))
  371. }
  372. func (codec *jsoniterNumberCodec) IsEmpty(ptr unsafe.Pointer) bool {
  373. return len(*((*Number)(ptr))) == 0
  374. }
  375. type jsonRawMessageCodec struct {
  376. }
  377. func (codec *jsonRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  378. *((*json.RawMessage)(ptr)) = json.RawMessage(iter.SkipAndReturnBytes())
  379. }
  380. func (codec *jsonRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  381. stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
  382. }
  383. func (codec *jsonRawMessageCodec) EncodeInterface(val interface{}, stream *Stream) {
  384. stream.WriteRaw(string(val.(json.RawMessage)))
  385. }
  386. func (codec *jsonRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
  387. return len(*((*json.RawMessage)(ptr))) == 0
  388. }
  389. type jsoniterRawMessageCodec struct {
  390. }
  391. func (codec *jsoniterRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  392. *((*RawMessage)(ptr)) = RawMessage(iter.SkipAndReturnBytes())
  393. }
  394. func (codec *jsoniterRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  395. stream.WriteRaw(string(*((*RawMessage)(ptr))))
  396. }
  397. func (codec *jsoniterRawMessageCodec) EncodeInterface(val interface{}, stream *Stream) {
  398. stream.WriteRaw(string(val.(RawMessage)))
  399. }
  400. func (codec *jsoniterRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
  401. return len(*((*RawMessage)(ptr))) == 0
  402. }
  403. type base64Codec struct {
  404. sliceDecoder ValDecoder
  405. }
  406. func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  407. if iter.ReadNil() {
  408. ptrSlice := (*sliceHeader)(ptr)
  409. ptrSlice.Len = 0
  410. ptrSlice.Cap = 0
  411. ptrSlice.Data = nil
  412. return
  413. }
  414. switch iter.WhatIsNext() {
  415. case StringValue:
  416. encoding := base64.StdEncoding
  417. src := iter.SkipAndReturnBytes()
  418. src = src[1 : len(src)-1]
  419. decodedLen := encoding.DecodedLen(len(src))
  420. dst := make([]byte, decodedLen)
  421. len, err := encoding.Decode(dst, src)
  422. if err != nil {
  423. iter.ReportError("decode base64", err.Error())
  424. } else {
  425. dst = dst[:len]
  426. dstSlice := (*sliceHeader)(unsafe.Pointer(&dst))
  427. ptrSlice := (*sliceHeader)(ptr)
  428. ptrSlice.Data = dstSlice.Data
  429. ptrSlice.Cap = dstSlice.Cap
  430. ptrSlice.Len = dstSlice.Len
  431. }
  432. case ArrayValue:
  433. codec.sliceDecoder.Decode(ptr, iter)
  434. default:
  435. iter.ReportError("base64Codec", "invalid input")
  436. }
  437. }
  438. func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  439. src := *((*[]byte)(ptr))
  440. if len(src) == 0 {
  441. stream.WriteNil()
  442. return
  443. }
  444. encoding := base64.StdEncoding
  445. stream.writeByte('"')
  446. toGrow := encoding.EncodedLen(len(src))
  447. stream.ensure(toGrow)
  448. encoding.Encode(stream.buf[stream.n:], src)
  449. stream.n += toGrow
  450. stream.writeByte('"')
  451. }
  452. func (codec *base64Codec) EncodeInterface(val interface{}, stream *Stream) {
  453. ptr := extractInterface(val).word
  454. src := *((*[]byte)(ptr))
  455. if len(src) == 0 {
  456. stream.WriteNil()
  457. return
  458. }
  459. encoding := base64.StdEncoding
  460. stream.writeByte('"')
  461. toGrow := encoding.EncodedLen(len(src))
  462. stream.ensure(toGrow)
  463. encoding.Encode(stream.buf[stream.n:], src)
  464. stream.n += toGrow
  465. stream.writeByte('"')
  466. }
  467. func (codec *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  468. return len(*((*[]byte)(ptr))) == 0
  469. }
  470. type stringModeNumberDecoder struct {
  471. elemDecoder ValDecoder
  472. }
  473. func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  474. c := iter.nextToken()
  475. if c != '"' {
  476. iter.ReportError("stringModeNumberDecoder", `expect "`)
  477. return
  478. }
  479. decoder.elemDecoder.Decode(ptr, iter)
  480. if iter.Error != nil {
  481. return
  482. }
  483. c = iter.readByte()
  484. if c != '"' {
  485. iter.ReportError("stringModeNumberDecoder", `expect "`)
  486. return
  487. }
  488. }
  489. type stringModeStringDecoder struct {
  490. elemDecoder ValDecoder
  491. cfg *frozenConfig
  492. }
  493. func (decoder *stringModeStringDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  494. decoder.elemDecoder.Decode(ptr, iter)
  495. str := *((*string)(ptr))
  496. tempIter := decoder.cfg.BorrowIterator([]byte(str))
  497. defer decoder.cfg.ReturnIterator(tempIter)
  498. *((*string)(ptr)) = tempIter.ReadString()
  499. }
  500. type stringModeNumberEncoder struct {
  501. elemEncoder ValEncoder
  502. }
  503. func (encoder *stringModeNumberEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  504. stream.writeByte('"')
  505. encoder.elemEncoder.Encode(ptr, stream)
  506. stream.writeByte('"')
  507. }
  508. func (encoder *stringModeNumberEncoder) EncodeInterface(val interface{}, stream *Stream) {
  509. WriteToStream(val, stream, encoder)
  510. }
  511. func (encoder *stringModeNumberEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  512. return encoder.elemEncoder.IsEmpty(ptr)
  513. }
  514. type stringModeStringEncoder struct {
  515. elemEncoder ValEncoder
  516. cfg *frozenConfig
  517. }
  518. func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  519. tempStream := encoder.cfg.BorrowStream(nil)
  520. defer encoder.cfg.ReturnStream(tempStream)
  521. encoder.elemEncoder.Encode(ptr, tempStream)
  522. stream.WriteString(string(tempStream.Buffer()))
  523. }
  524. func (encoder *stringModeStringEncoder) EncodeInterface(val interface{}, stream *Stream) {
  525. WriteToStream(val, stream, encoder)
  526. }
  527. func (encoder *stringModeStringEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  528. return encoder.elemEncoder.IsEmpty(ptr)
  529. }
  530. type marshalerEncoder struct {
  531. templateInterface emptyInterface
  532. checkIsEmpty checkIsEmpty
  533. }
  534. func (encoder *marshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  535. templateInterface := encoder.templateInterface
  536. templateInterface.word = ptr
  537. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  538. marshaler, ok := (*realInterface).(json.Marshaler)
  539. if !ok {
  540. stream.WriteVal(nil)
  541. return
  542. }
  543. bytes, err := marshaler.MarshalJSON()
  544. if err != nil {
  545. stream.Error = err
  546. } else {
  547. stream.Write(bytes)
  548. }
  549. }
  550. func (encoder *marshalerEncoder) EncodeInterface(val interface{}, stream *Stream) {
  551. WriteToStream(val, stream, encoder)
  552. }
  553. func (encoder *marshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  554. return encoder.checkIsEmpty.IsEmpty(ptr)
  555. }
  556. type textMarshalerEncoder struct {
  557. templateInterface emptyInterface
  558. checkIsEmpty checkIsEmpty
  559. }
  560. func (encoder *textMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  561. templateInterface := encoder.templateInterface
  562. templateInterface.word = ptr
  563. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  564. marshaler := (*realInterface).(encoding.TextMarshaler)
  565. bytes, err := marshaler.MarshalText()
  566. if err != nil {
  567. stream.Error = err
  568. } else {
  569. stream.WriteString(string(bytes))
  570. }
  571. }
  572. func (encoder *textMarshalerEncoder) EncodeInterface(val interface{}, stream *Stream) {
  573. WriteToStream(val, stream, encoder)
  574. }
  575. func (encoder *textMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  576. return encoder.checkIsEmpty.IsEmpty(ptr)
  577. }
  578. type unmarshalerDecoder struct {
  579. templateInterface emptyInterface
  580. }
  581. func (decoder *unmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  582. templateInterface := decoder.templateInterface
  583. templateInterface.word = ptr
  584. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  585. unmarshaler := (*realInterface).(json.Unmarshaler)
  586. iter.nextToken()
  587. iter.unreadByte() // skip spaces
  588. bytes := iter.SkipAndReturnBytes()
  589. err := unmarshaler.UnmarshalJSON(bytes)
  590. if err != nil {
  591. iter.ReportError("unmarshalerDecoder", err.Error())
  592. }
  593. }
  594. type textUnmarshalerDecoder struct {
  595. templateInterface emptyInterface
  596. }
  597. func (decoder *textUnmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  598. templateInterface := decoder.templateInterface
  599. templateInterface.word = ptr
  600. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  601. unmarshaler := (*realInterface).(encoding.TextUnmarshaler)
  602. str := iter.ReadString()
  603. err := unmarshaler.UnmarshalText([]byte(str))
  604. if err != nil {
  605. iter.ReportError("textUnmarshalerDecoder", err.Error())
  606. }
  607. }