feature_reflect_native.go 16 KB

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