feature_reflect_native.go 17 KB

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