feature_reflect_native.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  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 jsonRawMessageCodec struct {
  292. }
  293. func (codec *jsonRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  294. *((*json.RawMessage)(ptr)) = json.RawMessage(iter.SkipAndReturnBytes())
  295. }
  296. func (codec *jsonRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  297. stream.WriteRaw(string(*((*json.RawMessage)(ptr))))
  298. }
  299. func (codec *jsonRawMessageCodec) EncodeInterface(val interface{}, stream *Stream) {
  300. stream.WriteRaw(string(val.(json.RawMessage)))
  301. }
  302. func (codec *jsonRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
  303. return len(*((*json.RawMessage)(ptr))) == 0
  304. }
  305. type jsoniterRawMessageCodec struct {
  306. }
  307. func (codec *jsoniterRawMessageCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  308. *((*RawMessage)(ptr)) = RawMessage(iter.SkipAndReturnBytes())
  309. }
  310. func (codec *jsoniterRawMessageCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
  311. stream.WriteRaw(string(*((*RawMessage)(ptr))))
  312. }
  313. func (codec *jsoniterRawMessageCodec) EncodeInterface(val interface{}, stream *Stream) {
  314. stream.WriteRaw(string(val.(RawMessage)))
  315. }
  316. func (codec *jsoniterRawMessageCodec) IsEmpty(ptr unsafe.Pointer) bool {
  317. return len(*((*RawMessage)(ptr))) == 0
  318. }
  319. type base64Codec struct {
  320. sliceDecoder ValDecoder
  321. }
  322. func (codec *base64Codec) Decode(ptr unsafe.Pointer, iter *Iterator) {
  323. if iter.ReadNil() {
  324. ptrSlice := (*sliceHeader)(ptr)
  325. ptrSlice.Len = 0
  326. ptrSlice.Cap = 0
  327. ptrSlice.Data = nil
  328. return
  329. }
  330. switch iter.WhatIsNext() {
  331. case String:
  332. encoding := base64.StdEncoding
  333. src := iter.SkipAndReturnBytes()
  334. src = src[1 : len(src)-1]
  335. decodedLen := encoding.DecodedLen(len(src))
  336. dst := make([]byte, decodedLen)
  337. len, err := encoding.Decode(dst, src)
  338. if err != nil {
  339. iter.ReportError("decode base64", err.Error())
  340. } else {
  341. dst = dst[:len]
  342. dstSlice := (*sliceHeader)(unsafe.Pointer(&dst))
  343. ptrSlice := (*sliceHeader)(ptr)
  344. ptrSlice.Data = dstSlice.Data
  345. ptrSlice.Cap = dstSlice.Cap
  346. ptrSlice.Len = dstSlice.Len
  347. }
  348. case Array:
  349. codec.sliceDecoder.Decode(ptr, iter)
  350. default:
  351. iter.ReportError("base64Codec", "invalid input")
  352. }
  353. }
  354. func (codec *base64Codec) Encode(ptr unsafe.Pointer, stream *Stream) {
  355. src := *((*[]byte)(ptr))
  356. if len(src) == 0 {
  357. stream.WriteNil()
  358. return
  359. }
  360. encoding := base64.StdEncoding
  361. stream.writeByte('"')
  362. toGrow := encoding.EncodedLen(len(src))
  363. stream.ensure(toGrow)
  364. encoding.Encode(stream.buf[stream.n:], src)
  365. stream.n += toGrow
  366. stream.writeByte('"')
  367. }
  368. func (codec *base64Codec) EncodeInterface(val interface{}, stream *Stream) {
  369. ptr := extractInterface(val).word
  370. src := *((*[]byte)(ptr))
  371. if len(src) == 0 {
  372. stream.WriteNil()
  373. return
  374. }
  375. encoding := base64.StdEncoding
  376. stream.writeByte('"')
  377. toGrow := encoding.EncodedLen(len(src))
  378. stream.ensure(toGrow)
  379. encoding.Encode(stream.buf[stream.n:], src)
  380. stream.n += toGrow
  381. stream.writeByte('"')
  382. }
  383. func (codec *base64Codec) IsEmpty(ptr unsafe.Pointer) bool {
  384. return len(*((*[]byte)(ptr))) == 0
  385. }
  386. type stringModeNumberDecoder struct {
  387. elemDecoder ValDecoder
  388. }
  389. func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  390. c := iter.nextToken()
  391. if c != '"' {
  392. iter.ReportError("stringModeNumberDecoder", `expect "`)
  393. return
  394. }
  395. decoder.elemDecoder.Decode(ptr, iter)
  396. if iter.Error != nil {
  397. return
  398. }
  399. c = iter.readByte()
  400. if c != '"' {
  401. iter.ReportError("stringModeNumberDecoder", `expect "`)
  402. return
  403. }
  404. }
  405. type stringModeStringDecoder struct {
  406. elemDecoder ValDecoder
  407. cfg *frozenConfig
  408. }
  409. func (decoder *stringModeStringDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  410. decoder.elemDecoder.Decode(ptr, iter)
  411. str := *((*string)(ptr))
  412. tempIter := decoder.cfg.BorrowIterator([]byte(str))
  413. defer decoder.cfg.ReturnIterator(tempIter)
  414. *((*string)(ptr)) = tempIter.ReadString()
  415. }
  416. type stringModeNumberEncoder struct {
  417. elemEncoder ValEncoder
  418. }
  419. func (encoder *stringModeNumberEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  420. stream.writeByte('"')
  421. encoder.elemEncoder.Encode(ptr, stream)
  422. stream.writeByte('"')
  423. }
  424. func (encoder *stringModeNumberEncoder) EncodeInterface(val interface{}, stream *Stream) {
  425. WriteToStream(val, stream, encoder)
  426. }
  427. func (encoder *stringModeNumberEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  428. return encoder.elemEncoder.IsEmpty(ptr)
  429. }
  430. type stringModeStringEncoder struct {
  431. elemEncoder ValEncoder
  432. cfg *frozenConfig
  433. }
  434. func (encoder *stringModeStringEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  435. tempStream := encoder.cfg.BorrowStream(nil)
  436. defer encoder.cfg.ReturnStream(tempStream)
  437. encoder.elemEncoder.Encode(ptr, tempStream)
  438. stream.WriteString(string(tempStream.Buffer()))
  439. }
  440. func (encoder *stringModeStringEncoder) EncodeInterface(val interface{}, stream *Stream) {
  441. WriteToStream(val, stream, encoder)
  442. }
  443. func (encoder *stringModeStringEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  444. return encoder.elemEncoder.IsEmpty(ptr)
  445. }
  446. type marshalerEncoder struct {
  447. templateInterface emptyInterface
  448. checkIsEmpty checkIsEmpty
  449. }
  450. func (encoder *marshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  451. templateInterface := encoder.templateInterface
  452. templateInterface.word = ptr
  453. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  454. marshaler := (*realInterface).(json.Marshaler)
  455. bytes, err := marshaler.MarshalJSON()
  456. if err != nil {
  457. stream.Error = err
  458. } else {
  459. stream.Write(bytes)
  460. }
  461. }
  462. func (encoder *marshalerEncoder) EncodeInterface(val interface{}, stream *Stream) {
  463. WriteToStream(val, stream, encoder)
  464. }
  465. func (encoder *marshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  466. return encoder.checkIsEmpty.IsEmpty(ptr)
  467. }
  468. type textMarshalerEncoder struct {
  469. templateInterface emptyInterface
  470. checkIsEmpty checkIsEmpty
  471. }
  472. func (encoder *textMarshalerEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
  473. templateInterface := encoder.templateInterface
  474. templateInterface.word = ptr
  475. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  476. marshaler := (*realInterface).(encoding.TextMarshaler)
  477. bytes, err := marshaler.MarshalText()
  478. if err != nil {
  479. stream.Error = err
  480. } else {
  481. stream.WriteString(string(bytes))
  482. }
  483. }
  484. func (encoder *textMarshalerEncoder) EncodeInterface(val interface{}, stream *Stream) {
  485. WriteToStream(val, stream, encoder)
  486. }
  487. func (encoder *textMarshalerEncoder) IsEmpty(ptr unsafe.Pointer) bool {
  488. return encoder.checkIsEmpty.IsEmpty(ptr)
  489. }
  490. type unmarshalerDecoder struct {
  491. templateInterface emptyInterface
  492. }
  493. func (decoder *unmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  494. templateInterface := decoder.templateInterface
  495. templateInterface.word = ptr
  496. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  497. unmarshaler := (*realInterface).(json.Unmarshaler)
  498. iter.nextToken()
  499. iter.unreadByte() // skip spaces
  500. bytes := iter.SkipAndReturnBytes()
  501. err := unmarshaler.UnmarshalJSON(bytes)
  502. if err != nil {
  503. iter.ReportError("unmarshalerDecoder", err.Error())
  504. }
  505. }
  506. type textUnmarshalerDecoder struct {
  507. templateInterface emptyInterface
  508. }
  509. func (decoder *textUnmarshalerDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  510. templateInterface := decoder.templateInterface
  511. templateInterface.word = ptr
  512. realInterface := (*interface{})(unsafe.Pointer(&templateInterface))
  513. unmarshaler := (*realInterface).(encoding.TextUnmarshaler)
  514. str := iter.ReadString()
  515. err := unmarshaler.UnmarshalText([]byte(str))
  516. if err != nil {
  517. iter.ReportError("textUnmarshalerDecoder", err.Error())
  518. }
  519. }