feature_reflect_native.go 16 KB

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