feature_reflect_native.go 16 KB

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