feature_reflect_native.go 18 KB

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