reflect_struct_decoder.go 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "io"
  5. "strings"
  6. "unsafe"
  7. "github.com/modern-go/reflect2"
  8. )
  9. func decoderOfStruct(ctx *ctx, typ reflect2.Type) ValDecoder {
  10. bindings := map[string]*Binding{}
  11. structDescriptor := describeStruct(ctx, typ)
  12. for _, binding := range structDescriptor.Fields {
  13. for _, fromName := range binding.FromNames {
  14. old := bindings[fromName]
  15. if old == nil {
  16. bindings[fromName] = binding
  17. continue
  18. }
  19. ignoreOld, ignoreNew := resolveConflictBinding(ctx.frozenConfig, old, binding)
  20. if ignoreOld {
  21. delete(bindings, fromName)
  22. }
  23. if !ignoreNew {
  24. bindings[fromName] = binding
  25. }
  26. }
  27. }
  28. fields := map[string]*structFieldDecoder{}
  29. for k, binding := range bindings {
  30. fields[k] = binding.Decoder.(*structFieldDecoder)
  31. }
  32. if !ctx.caseSensitive() {
  33. for k, binding := range bindings {
  34. if _, found := fields[strings.ToLower(k)]; !found {
  35. fields[strings.ToLower(k)] = binding.Decoder.(*structFieldDecoder)
  36. }
  37. }
  38. }
  39. return createStructDecoder(ctx, typ, fields)
  40. }
  41. func createStructDecoder(ctx *ctx, typ reflect2.Type, fields map[string]*structFieldDecoder) ValDecoder {
  42. if ctx.disallowUnknownFields {
  43. return &generalStructDecoder{typ: typ, fields: fields, disallowUnknownFields: true}
  44. }
  45. knownHash := map[int64]struct{}{
  46. 0: {},
  47. }
  48. switch len(fields) {
  49. case 0:
  50. return &skipObjectDecoder{typ}
  51. case 1:
  52. for fieldName, fieldDecoder := range fields {
  53. fieldHash := calcHash(fieldName, ctx.caseSensitive())
  54. _, known := knownHash[fieldHash]
  55. if known {
  56. return &generalStructDecoder{typ, fields, false}
  57. }
  58. knownHash[fieldHash] = struct{}{}
  59. return &oneFieldStructDecoder{typ, fieldHash, fieldDecoder}
  60. }
  61. case 2:
  62. var fieldHash1 int64
  63. var fieldHash2 int64
  64. var fieldDecoder1 *structFieldDecoder
  65. var fieldDecoder2 *structFieldDecoder
  66. for fieldName, fieldDecoder := range fields {
  67. fieldHash := calcHash(fieldName, ctx.caseSensitive())
  68. _, known := knownHash[fieldHash]
  69. if known {
  70. return &generalStructDecoder{typ, fields, false}
  71. }
  72. knownHash[fieldHash] = struct{}{}
  73. if fieldHash1 == 0 {
  74. fieldHash1 = fieldHash
  75. fieldDecoder1 = fieldDecoder
  76. } else {
  77. fieldHash2 = fieldHash
  78. fieldDecoder2 = fieldDecoder
  79. }
  80. }
  81. return &twoFieldsStructDecoder{typ, fieldHash1, fieldDecoder1, fieldHash2, fieldDecoder2}
  82. case 3:
  83. var fieldName1 int64
  84. var fieldName2 int64
  85. var fieldName3 int64
  86. var fieldDecoder1 *structFieldDecoder
  87. var fieldDecoder2 *structFieldDecoder
  88. var fieldDecoder3 *structFieldDecoder
  89. for fieldName, fieldDecoder := range fields {
  90. fieldHash := calcHash(fieldName, ctx.caseSensitive())
  91. _, known := knownHash[fieldHash]
  92. if known {
  93. return &generalStructDecoder{typ, fields, false}
  94. }
  95. knownHash[fieldHash] = struct{}{}
  96. if fieldName1 == 0 {
  97. fieldName1 = fieldHash
  98. fieldDecoder1 = fieldDecoder
  99. } else if fieldName2 == 0 {
  100. fieldName2 = fieldHash
  101. fieldDecoder2 = fieldDecoder
  102. } else {
  103. fieldName3 = fieldHash
  104. fieldDecoder3 = fieldDecoder
  105. }
  106. }
  107. return &threeFieldsStructDecoder{typ,
  108. fieldName1, fieldDecoder1,
  109. fieldName2, fieldDecoder2,
  110. fieldName3, fieldDecoder3}
  111. case 4:
  112. var fieldName1 int64
  113. var fieldName2 int64
  114. var fieldName3 int64
  115. var fieldName4 int64
  116. var fieldDecoder1 *structFieldDecoder
  117. var fieldDecoder2 *structFieldDecoder
  118. var fieldDecoder3 *structFieldDecoder
  119. var fieldDecoder4 *structFieldDecoder
  120. for fieldName, fieldDecoder := range fields {
  121. fieldHash := calcHash(fieldName, ctx.caseSensitive())
  122. _, known := knownHash[fieldHash]
  123. if known {
  124. return &generalStructDecoder{typ, fields, false}
  125. }
  126. knownHash[fieldHash] = struct{}{}
  127. if fieldName1 == 0 {
  128. fieldName1 = fieldHash
  129. fieldDecoder1 = fieldDecoder
  130. } else if fieldName2 == 0 {
  131. fieldName2 = fieldHash
  132. fieldDecoder2 = fieldDecoder
  133. } else if fieldName3 == 0 {
  134. fieldName3 = fieldHash
  135. fieldDecoder3 = fieldDecoder
  136. } else {
  137. fieldName4 = fieldHash
  138. fieldDecoder4 = fieldDecoder
  139. }
  140. }
  141. return &fourFieldsStructDecoder{typ,
  142. fieldName1, fieldDecoder1,
  143. fieldName2, fieldDecoder2,
  144. fieldName3, fieldDecoder3,
  145. fieldName4, fieldDecoder4}
  146. case 5:
  147. var fieldName1 int64
  148. var fieldName2 int64
  149. var fieldName3 int64
  150. var fieldName4 int64
  151. var fieldName5 int64
  152. var fieldDecoder1 *structFieldDecoder
  153. var fieldDecoder2 *structFieldDecoder
  154. var fieldDecoder3 *structFieldDecoder
  155. var fieldDecoder4 *structFieldDecoder
  156. var fieldDecoder5 *structFieldDecoder
  157. for fieldName, fieldDecoder := range fields {
  158. fieldHash := calcHash(fieldName, ctx.caseSensitive())
  159. _, known := knownHash[fieldHash]
  160. if known {
  161. return &generalStructDecoder{typ, fields, false}
  162. }
  163. knownHash[fieldHash] = struct{}{}
  164. if fieldName1 == 0 {
  165. fieldName1 = fieldHash
  166. fieldDecoder1 = fieldDecoder
  167. } else if fieldName2 == 0 {
  168. fieldName2 = fieldHash
  169. fieldDecoder2 = fieldDecoder
  170. } else if fieldName3 == 0 {
  171. fieldName3 = fieldHash
  172. fieldDecoder3 = fieldDecoder
  173. } else if fieldName4 == 0 {
  174. fieldName4 = fieldHash
  175. fieldDecoder4 = fieldDecoder
  176. } else {
  177. fieldName5 = fieldHash
  178. fieldDecoder5 = fieldDecoder
  179. }
  180. }
  181. return &fiveFieldsStructDecoder{typ,
  182. fieldName1, fieldDecoder1,
  183. fieldName2, fieldDecoder2,
  184. fieldName3, fieldDecoder3,
  185. fieldName4, fieldDecoder4,
  186. fieldName5, fieldDecoder5}
  187. case 6:
  188. var fieldName1 int64
  189. var fieldName2 int64
  190. var fieldName3 int64
  191. var fieldName4 int64
  192. var fieldName5 int64
  193. var fieldName6 int64
  194. var fieldDecoder1 *structFieldDecoder
  195. var fieldDecoder2 *structFieldDecoder
  196. var fieldDecoder3 *structFieldDecoder
  197. var fieldDecoder4 *structFieldDecoder
  198. var fieldDecoder5 *structFieldDecoder
  199. var fieldDecoder6 *structFieldDecoder
  200. for fieldName, fieldDecoder := range fields {
  201. fieldHash := calcHash(fieldName, ctx.caseSensitive())
  202. _, known := knownHash[fieldHash]
  203. if known {
  204. return &generalStructDecoder{typ, fields, false}
  205. }
  206. knownHash[fieldHash] = struct{}{}
  207. if fieldName1 == 0 {
  208. fieldName1 = fieldHash
  209. fieldDecoder1 = fieldDecoder
  210. } else if fieldName2 == 0 {
  211. fieldName2 = fieldHash
  212. fieldDecoder2 = fieldDecoder
  213. } else if fieldName3 == 0 {
  214. fieldName3 = fieldHash
  215. fieldDecoder3 = fieldDecoder
  216. } else if fieldName4 == 0 {
  217. fieldName4 = fieldHash
  218. fieldDecoder4 = fieldDecoder
  219. } else if fieldName5 == 0 {
  220. fieldName5 = fieldHash
  221. fieldDecoder5 = fieldDecoder
  222. } else {
  223. fieldName6 = fieldHash
  224. fieldDecoder6 = fieldDecoder
  225. }
  226. }
  227. return &sixFieldsStructDecoder{typ,
  228. fieldName1, fieldDecoder1,
  229. fieldName2, fieldDecoder2,
  230. fieldName3, fieldDecoder3,
  231. fieldName4, fieldDecoder4,
  232. fieldName5, fieldDecoder5,
  233. fieldName6, fieldDecoder6}
  234. case 7:
  235. var fieldName1 int64
  236. var fieldName2 int64
  237. var fieldName3 int64
  238. var fieldName4 int64
  239. var fieldName5 int64
  240. var fieldName6 int64
  241. var fieldName7 int64
  242. var fieldDecoder1 *structFieldDecoder
  243. var fieldDecoder2 *structFieldDecoder
  244. var fieldDecoder3 *structFieldDecoder
  245. var fieldDecoder4 *structFieldDecoder
  246. var fieldDecoder5 *structFieldDecoder
  247. var fieldDecoder6 *structFieldDecoder
  248. var fieldDecoder7 *structFieldDecoder
  249. for fieldName, fieldDecoder := range fields {
  250. fieldHash := calcHash(fieldName, ctx.caseSensitive())
  251. _, known := knownHash[fieldHash]
  252. if known {
  253. return &generalStructDecoder{typ, fields, false}
  254. }
  255. knownHash[fieldHash] = struct{}{}
  256. if fieldName1 == 0 {
  257. fieldName1 = fieldHash
  258. fieldDecoder1 = fieldDecoder
  259. } else if fieldName2 == 0 {
  260. fieldName2 = fieldHash
  261. fieldDecoder2 = fieldDecoder
  262. } else if fieldName3 == 0 {
  263. fieldName3 = fieldHash
  264. fieldDecoder3 = fieldDecoder
  265. } else if fieldName4 == 0 {
  266. fieldName4 = fieldHash
  267. fieldDecoder4 = fieldDecoder
  268. } else if fieldName5 == 0 {
  269. fieldName5 = fieldHash
  270. fieldDecoder5 = fieldDecoder
  271. } else if fieldName6 == 0 {
  272. fieldName6 = fieldHash
  273. fieldDecoder6 = fieldDecoder
  274. } else {
  275. fieldName7 = fieldHash
  276. fieldDecoder7 = fieldDecoder
  277. }
  278. }
  279. return &sevenFieldsStructDecoder{typ,
  280. fieldName1, fieldDecoder1,
  281. fieldName2, fieldDecoder2,
  282. fieldName3, fieldDecoder3,
  283. fieldName4, fieldDecoder4,
  284. fieldName5, fieldDecoder5,
  285. fieldName6, fieldDecoder6,
  286. fieldName7, fieldDecoder7}
  287. case 8:
  288. var fieldName1 int64
  289. var fieldName2 int64
  290. var fieldName3 int64
  291. var fieldName4 int64
  292. var fieldName5 int64
  293. var fieldName6 int64
  294. var fieldName7 int64
  295. var fieldName8 int64
  296. var fieldDecoder1 *structFieldDecoder
  297. var fieldDecoder2 *structFieldDecoder
  298. var fieldDecoder3 *structFieldDecoder
  299. var fieldDecoder4 *structFieldDecoder
  300. var fieldDecoder5 *structFieldDecoder
  301. var fieldDecoder6 *structFieldDecoder
  302. var fieldDecoder7 *structFieldDecoder
  303. var fieldDecoder8 *structFieldDecoder
  304. for fieldName, fieldDecoder := range fields {
  305. fieldHash := calcHash(fieldName, ctx.caseSensitive())
  306. _, known := knownHash[fieldHash]
  307. if known {
  308. return &generalStructDecoder{typ, fields, false}
  309. }
  310. knownHash[fieldHash] = struct{}{}
  311. if fieldName1 == 0 {
  312. fieldName1 = fieldHash
  313. fieldDecoder1 = fieldDecoder
  314. } else if fieldName2 == 0 {
  315. fieldName2 = fieldHash
  316. fieldDecoder2 = fieldDecoder
  317. } else if fieldName3 == 0 {
  318. fieldName3 = fieldHash
  319. fieldDecoder3 = fieldDecoder
  320. } else if fieldName4 == 0 {
  321. fieldName4 = fieldHash
  322. fieldDecoder4 = fieldDecoder
  323. } else if fieldName5 == 0 {
  324. fieldName5 = fieldHash
  325. fieldDecoder5 = fieldDecoder
  326. } else if fieldName6 == 0 {
  327. fieldName6 = fieldHash
  328. fieldDecoder6 = fieldDecoder
  329. } else if fieldName7 == 0 {
  330. fieldName7 = fieldHash
  331. fieldDecoder7 = fieldDecoder
  332. } else {
  333. fieldName8 = fieldHash
  334. fieldDecoder8 = fieldDecoder
  335. }
  336. }
  337. return &eightFieldsStructDecoder{typ,
  338. fieldName1, fieldDecoder1,
  339. fieldName2, fieldDecoder2,
  340. fieldName3, fieldDecoder3,
  341. fieldName4, fieldDecoder4,
  342. fieldName5, fieldDecoder5,
  343. fieldName6, fieldDecoder6,
  344. fieldName7, fieldDecoder7,
  345. fieldName8, fieldDecoder8}
  346. case 9:
  347. var fieldName1 int64
  348. var fieldName2 int64
  349. var fieldName3 int64
  350. var fieldName4 int64
  351. var fieldName5 int64
  352. var fieldName6 int64
  353. var fieldName7 int64
  354. var fieldName8 int64
  355. var fieldName9 int64
  356. var fieldDecoder1 *structFieldDecoder
  357. var fieldDecoder2 *structFieldDecoder
  358. var fieldDecoder3 *structFieldDecoder
  359. var fieldDecoder4 *structFieldDecoder
  360. var fieldDecoder5 *structFieldDecoder
  361. var fieldDecoder6 *structFieldDecoder
  362. var fieldDecoder7 *structFieldDecoder
  363. var fieldDecoder8 *structFieldDecoder
  364. var fieldDecoder9 *structFieldDecoder
  365. for fieldName, fieldDecoder := range fields {
  366. fieldHash := calcHash(fieldName, ctx.caseSensitive())
  367. _, known := knownHash[fieldHash]
  368. if known {
  369. return &generalStructDecoder{typ, fields, false}
  370. }
  371. knownHash[fieldHash] = struct{}{}
  372. if fieldName1 == 0 {
  373. fieldName1 = fieldHash
  374. fieldDecoder1 = fieldDecoder
  375. } else if fieldName2 == 0 {
  376. fieldName2 = fieldHash
  377. fieldDecoder2 = fieldDecoder
  378. } else if fieldName3 == 0 {
  379. fieldName3 = fieldHash
  380. fieldDecoder3 = fieldDecoder
  381. } else if fieldName4 == 0 {
  382. fieldName4 = fieldHash
  383. fieldDecoder4 = fieldDecoder
  384. } else if fieldName5 == 0 {
  385. fieldName5 = fieldHash
  386. fieldDecoder5 = fieldDecoder
  387. } else if fieldName6 == 0 {
  388. fieldName6 = fieldHash
  389. fieldDecoder6 = fieldDecoder
  390. } else if fieldName7 == 0 {
  391. fieldName7 = fieldHash
  392. fieldDecoder7 = fieldDecoder
  393. } else if fieldName8 == 0 {
  394. fieldName8 = fieldHash
  395. fieldDecoder8 = fieldDecoder
  396. } else {
  397. fieldName9 = fieldHash
  398. fieldDecoder9 = fieldDecoder
  399. }
  400. }
  401. return &nineFieldsStructDecoder{typ,
  402. fieldName1, fieldDecoder1,
  403. fieldName2, fieldDecoder2,
  404. fieldName3, fieldDecoder3,
  405. fieldName4, fieldDecoder4,
  406. fieldName5, fieldDecoder5,
  407. fieldName6, fieldDecoder6,
  408. fieldName7, fieldDecoder7,
  409. fieldName8, fieldDecoder8,
  410. fieldName9, fieldDecoder9}
  411. case 10:
  412. var fieldName1 int64
  413. var fieldName2 int64
  414. var fieldName3 int64
  415. var fieldName4 int64
  416. var fieldName5 int64
  417. var fieldName6 int64
  418. var fieldName7 int64
  419. var fieldName8 int64
  420. var fieldName9 int64
  421. var fieldName10 int64
  422. var fieldDecoder1 *structFieldDecoder
  423. var fieldDecoder2 *structFieldDecoder
  424. var fieldDecoder3 *structFieldDecoder
  425. var fieldDecoder4 *structFieldDecoder
  426. var fieldDecoder5 *structFieldDecoder
  427. var fieldDecoder6 *structFieldDecoder
  428. var fieldDecoder7 *structFieldDecoder
  429. var fieldDecoder8 *structFieldDecoder
  430. var fieldDecoder9 *structFieldDecoder
  431. var fieldDecoder10 *structFieldDecoder
  432. for fieldName, fieldDecoder := range fields {
  433. fieldHash := calcHash(fieldName, ctx.caseSensitive())
  434. _, known := knownHash[fieldHash]
  435. if known {
  436. return &generalStructDecoder{typ, fields, false}
  437. }
  438. knownHash[fieldHash] = struct{}{}
  439. if fieldName1 == 0 {
  440. fieldName1 = fieldHash
  441. fieldDecoder1 = fieldDecoder
  442. } else if fieldName2 == 0 {
  443. fieldName2 = fieldHash
  444. fieldDecoder2 = fieldDecoder
  445. } else if fieldName3 == 0 {
  446. fieldName3 = fieldHash
  447. fieldDecoder3 = fieldDecoder
  448. } else if fieldName4 == 0 {
  449. fieldName4 = fieldHash
  450. fieldDecoder4 = fieldDecoder
  451. } else if fieldName5 == 0 {
  452. fieldName5 = fieldHash
  453. fieldDecoder5 = fieldDecoder
  454. } else if fieldName6 == 0 {
  455. fieldName6 = fieldHash
  456. fieldDecoder6 = fieldDecoder
  457. } else if fieldName7 == 0 {
  458. fieldName7 = fieldHash
  459. fieldDecoder7 = fieldDecoder
  460. } else if fieldName8 == 0 {
  461. fieldName8 = fieldHash
  462. fieldDecoder8 = fieldDecoder
  463. } else if fieldName9 == 0 {
  464. fieldName9 = fieldHash
  465. fieldDecoder9 = fieldDecoder
  466. } else {
  467. fieldName10 = fieldHash
  468. fieldDecoder10 = fieldDecoder
  469. }
  470. }
  471. return &tenFieldsStructDecoder{typ,
  472. fieldName1, fieldDecoder1,
  473. fieldName2, fieldDecoder2,
  474. fieldName3, fieldDecoder3,
  475. fieldName4, fieldDecoder4,
  476. fieldName5, fieldDecoder5,
  477. fieldName6, fieldDecoder6,
  478. fieldName7, fieldDecoder7,
  479. fieldName8, fieldDecoder8,
  480. fieldName9, fieldDecoder9,
  481. fieldName10, fieldDecoder10}
  482. }
  483. return &generalStructDecoder{typ, fields, false}
  484. }
  485. type generalStructDecoder struct {
  486. typ reflect2.Type
  487. fields map[string]*structFieldDecoder
  488. disallowUnknownFields bool
  489. }
  490. func (decoder *generalStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  491. if !iter.readObjectStart() {
  492. return
  493. }
  494. if !iter.incrementDepth() {
  495. return
  496. }
  497. var c byte
  498. for c = ','; c == ','; c = iter.nextToken() {
  499. decoder.decodeOneField(ptr, iter)
  500. }
  501. if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
  502. iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
  503. }
  504. if c != '}' {
  505. iter.ReportError("struct Decode", `expect }, but found `+string([]byte{c}))
  506. }
  507. iter.decrementDepth()
  508. }
  509. func (decoder *generalStructDecoder) decodeOneField(ptr unsafe.Pointer, iter *Iterator) {
  510. var field string
  511. var fieldDecoder *structFieldDecoder
  512. if iter.cfg.objectFieldMustBeSimpleString {
  513. fieldBytes := iter.ReadStringAsSlice()
  514. field = *(*string)(unsafe.Pointer(&fieldBytes))
  515. fieldDecoder = decoder.fields[field]
  516. if fieldDecoder == nil && !iter.cfg.caseSensitive {
  517. fieldDecoder = decoder.fields[strings.ToLower(field)]
  518. }
  519. } else {
  520. field = iter.ReadString()
  521. fieldDecoder = decoder.fields[field]
  522. if fieldDecoder == nil && !iter.cfg.caseSensitive {
  523. fieldDecoder = decoder.fields[strings.ToLower(field)]
  524. }
  525. }
  526. if fieldDecoder == nil {
  527. if decoder.disallowUnknownFields {
  528. msg := "found unknown field: " + field
  529. iter.ReportError("ReadObject", msg)
  530. }
  531. c := iter.nextToken()
  532. if c != ':' {
  533. iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
  534. }
  535. iter.Skip()
  536. return
  537. }
  538. c := iter.nextToken()
  539. if c != ':' {
  540. iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
  541. }
  542. fieldDecoder.Decode(ptr, iter)
  543. }
  544. type skipObjectDecoder struct {
  545. typ reflect2.Type
  546. }
  547. func (decoder *skipObjectDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  548. valueType := iter.WhatIsNext()
  549. if valueType != ObjectValue && valueType != NilValue {
  550. iter.ReportError("skipObjectDecoder", "expect object or null")
  551. return
  552. }
  553. iter.Skip()
  554. }
  555. type oneFieldStructDecoder struct {
  556. typ reflect2.Type
  557. fieldHash int64
  558. fieldDecoder *structFieldDecoder
  559. }
  560. func (decoder *oneFieldStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  561. if !iter.readObjectStart() {
  562. return
  563. }
  564. if !iter.incrementDepth() {
  565. return
  566. }
  567. for {
  568. if iter.readFieldHash() == decoder.fieldHash {
  569. decoder.fieldDecoder.Decode(ptr, iter)
  570. } else {
  571. iter.Skip()
  572. }
  573. if iter.isObjectEnd() {
  574. break
  575. }
  576. }
  577. if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
  578. iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
  579. }
  580. iter.decrementDepth()
  581. }
  582. type twoFieldsStructDecoder struct {
  583. typ reflect2.Type
  584. fieldHash1 int64
  585. fieldDecoder1 *structFieldDecoder
  586. fieldHash2 int64
  587. fieldDecoder2 *structFieldDecoder
  588. }
  589. func (decoder *twoFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  590. if !iter.readObjectStart() {
  591. return
  592. }
  593. if !iter.incrementDepth() {
  594. return
  595. }
  596. for {
  597. switch iter.readFieldHash() {
  598. case decoder.fieldHash1:
  599. decoder.fieldDecoder1.Decode(ptr, iter)
  600. case decoder.fieldHash2:
  601. decoder.fieldDecoder2.Decode(ptr, iter)
  602. default:
  603. iter.Skip()
  604. }
  605. if iter.isObjectEnd() {
  606. break
  607. }
  608. }
  609. if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
  610. iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
  611. }
  612. iter.decrementDepth()
  613. }
  614. type threeFieldsStructDecoder struct {
  615. typ reflect2.Type
  616. fieldHash1 int64
  617. fieldDecoder1 *structFieldDecoder
  618. fieldHash2 int64
  619. fieldDecoder2 *structFieldDecoder
  620. fieldHash3 int64
  621. fieldDecoder3 *structFieldDecoder
  622. }
  623. func (decoder *threeFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  624. if !iter.readObjectStart() {
  625. return
  626. }
  627. if !iter.incrementDepth() {
  628. return
  629. }
  630. for {
  631. switch iter.readFieldHash() {
  632. case decoder.fieldHash1:
  633. decoder.fieldDecoder1.Decode(ptr, iter)
  634. case decoder.fieldHash2:
  635. decoder.fieldDecoder2.Decode(ptr, iter)
  636. case decoder.fieldHash3:
  637. decoder.fieldDecoder3.Decode(ptr, iter)
  638. default:
  639. iter.Skip()
  640. }
  641. if iter.isObjectEnd() {
  642. break
  643. }
  644. }
  645. if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
  646. iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
  647. }
  648. iter.decrementDepth()
  649. }
  650. type fourFieldsStructDecoder struct {
  651. typ reflect2.Type
  652. fieldHash1 int64
  653. fieldDecoder1 *structFieldDecoder
  654. fieldHash2 int64
  655. fieldDecoder2 *structFieldDecoder
  656. fieldHash3 int64
  657. fieldDecoder3 *structFieldDecoder
  658. fieldHash4 int64
  659. fieldDecoder4 *structFieldDecoder
  660. }
  661. func (decoder *fourFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  662. if !iter.readObjectStart() {
  663. return
  664. }
  665. if !iter.incrementDepth() {
  666. return
  667. }
  668. for {
  669. switch iter.readFieldHash() {
  670. case decoder.fieldHash1:
  671. decoder.fieldDecoder1.Decode(ptr, iter)
  672. case decoder.fieldHash2:
  673. decoder.fieldDecoder2.Decode(ptr, iter)
  674. case decoder.fieldHash3:
  675. decoder.fieldDecoder3.Decode(ptr, iter)
  676. case decoder.fieldHash4:
  677. decoder.fieldDecoder4.Decode(ptr, iter)
  678. default:
  679. iter.Skip()
  680. }
  681. if iter.isObjectEnd() {
  682. break
  683. }
  684. }
  685. if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
  686. iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
  687. }
  688. iter.decrementDepth()
  689. }
  690. type fiveFieldsStructDecoder struct {
  691. typ reflect2.Type
  692. fieldHash1 int64
  693. fieldDecoder1 *structFieldDecoder
  694. fieldHash2 int64
  695. fieldDecoder2 *structFieldDecoder
  696. fieldHash3 int64
  697. fieldDecoder3 *structFieldDecoder
  698. fieldHash4 int64
  699. fieldDecoder4 *structFieldDecoder
  700. fieldHash5 int64
  701. fieldDecoder5 *structFieldDecoder
  702. }
  703. func (decoder *fiveFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  704. if !iter.readObjectStart() {
  705. return
  706. }
  707. if !iter.incrementDepth() {
  708. return
  709. }
  710. for {
  711. switch iter.readFieldHash() {
  712. case decoder.fieldHash1:
  713. decoder.fieldDecoder1.Decode(ptr, iter)
  714. case decoder.fieldHash2:
  715. decoder.fieldDecoder2.Decode(ptr, iter)
  716. case decoder.fieldHash3:
  717. decoder.fieldDecoder3.Decode(ptr, iter)
  718. case decoder.fieldHash4:
  719. decoder.fieldDecoder4.Decode(ptr, iter)
  720. case decoder.fieldHash5:
  721. decoder.fieldDecoder5.Decode(ptr, iter)
  722. default:
  723. iter.Skip()
  724. }
  725. if iter.isObjectEnd() {
  726. break
  727. }
  728. }
  729. if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
  730. iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
  731. }
  732. iter.decrementDepth()
  733. }
  734. type sixFieldsStructDecoder struct {
  735. typ reflect2.Type
  736. fieldHash1 int64
  737. fieldDecoder1 *structFieldDecoder
  738. fieldHash2 int64
  739. fieldDecoder2 *structFieldDecoder
  740. fieldHash3 int64
  741. fieldDecoder3 *structFieldDecoder
  742. fieldHash4 int64
  743. fieldDecoder4 *structFieldDecoder
  744. fieldHash5 int64
  745. fieldDecoder5 *structFieldDecoder
  746. fieldHash6 int64
  747. fieldDecoder6 *structFieldDecoder
  748. }
  749. func (decoder *sixFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  750. if !iter.readObjectStart() {
  751. return
  752. }
  753. if !iter.incrementDepth() {
  754. return
  755. }
  756. for {
  757. switch iter.readFieldHash() {
  758. case decoder.fieldHash1:
  759. decoder.fieldDecoder1.Decode(ptr, iter)
  760. case decoder.fieldHash2:
  761. decoder.fieldDecoder2.Decode(ptr, iter)
  762. case decoder.fieldHash3:
  763. decoder.fieldDecoder3.Decode(ptr, iter)
  764. case decoder.fieldHash4:
  765. decoder.fieldDecoder4.Decode(ptr, iter)
  766. case decoder.fieldHash5:
  767. decoder.fieldDecoder5.Decode(ptr, iter)
  768. case decoder.fieldHash6:
  769. decoder.fieldDecoder6.Decode(ptr, iter)
  770. default:
  771. iter.Skip()
  772. }
  773. if iter.isObjectEnd() {
  774. break
  775. }
  776. }
  777. if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
  778. iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
  779. }
  780. iter.decrementDepth()
  781. }
  782. type sevenFieldsStructDecoder struct {
  783. typ reflect2.Type
  784. fieldHash1 int64
  785. fieldDecoder1 *structFieldDecoder
  786. fieldHash2 int64
  787. fieldDecoder2 *structFieldDecoder
  788. fieldHash3 int64
  789. fieldDecoder3 *structFieldDecoder
  790. fieldHash4 int64
  791. fieldDecoder4 *structFieldDecoder
  792. fieldHash5 int64
  793. fieldDecoder5 *structFieldDecoder
  794. fieldHash6 int64
  795. fieldDecoder6 *structFieldDecoder
  796. fieldHash7 int64
  797. fieldDecoder7 *structFieldDecoder
  798. }
  799. func (decoder *sevenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  800. if !iter.readObjectStart() {
  801. return
  802. }
  803. if !iter.incrementDepth() {
  804. return
  805. }
  806. for {
  807. switch iter.readFieldHash() {
  808. case decoder.fieldHash1:
  809. decoder.fieldDecoder1.Decode(ptr, iter)
  810. case decoder.fieldHash2:
  811. decoder.fieldDecoder2.Decode(ptr, iter)
  812. case decoder.fieldHash3:
  813. decoder.fieldDecoder3.Decode(ptr, iter)
  814. case decoder.fieldHash4:
  815. decoder.fieldDecoder4.Decode(ptr, iter)
  816. case decoder.fieldHash5:
  817. decoder.fieldDecoder5.Decode(ptr, iter)
  818. case decoder.fieldHash6:
  819. decoder.fieldDecoder6.Decode(ptr, iter)
  820. case decoder.fieldHash7:
  821. decoder.fieldDecoder7.Decode(ptr, iter)
  822. default:
  823. iter.Skip()
  824. }
  825. if iter.isObjectEnd() {
  826. break
  827. }
  828. }
  829. if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
  830. iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
  831. }
  832. iter.decrementDepth()
  833. }
  834. type eightFieldsStructDecoder struct {
  835. typ reflect2.Type
  836. fieldHash1 int64
  837. fieldDecoder1 *structFieldDecoder
  838. fieldHash2 int64
  839. fieldDecoder2 *structFieldDecoder
  840. fieldHash3 int64
  841. fieldDecoder3 *structFieldDecoder
  842. fieldHash4 int64
  843. fieldDecoder4 *structFieldDecoder
  844. fieldHash5 int64
  845. fieldDecoder5 *structFieldDecoder
  846. fieldHash6 int64
  847. fieldDecoder6 *structFieldDecoder
  848. fieldHash7 int64
  849. fieldDecoder7 *structFieldDecoder
  850. fieldHash8 int64
  851. fieldDecoder8 *structFieldDecoder
  852. }
  853. func (decoder *eightFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  854. if !iter.readObjectStart() {
  855. return
  856. }
  857. if !iter.incrementDepth() {
  858. return
  859. }
  860. for {
  861. switch iter.readFieldHash() {
  862. case decoder.fieldHash1:
  863. decoder.fieldDecoder1.Decode(ptr, iter)
  864. case decoder.fieldHash2:
  865. decoder.fieldDecoder2.Decode(ptr, iter)
  866. case decoder.fieldHash3:
  867. decoder.fieldDecoder3.Decode(ptr, iter)
  868. case decoder.fieldHash4:
  869. decoder.fieldDecoder4.Decode(ptr, iter)
  870. case decoder.fieldHash5:
  871. decoder.fieldDecoder5.Decode(ptr, iter)
  872. case decoder.fieldHash6:
  873. decoder.fieldDecoder6.Decode(ptr, iter)
  874. case decoder.fieldHash7:
  875. decoder.fieldDecoder7.Decode(ptr, iter)
  876. case decoder.fieldHash8:
  877. decoder.fieldDecoder8.Decode(ptr, iter)
  878. default:
  879. iter.Skip()
  880. }
  881. if iter.isObjectEnd() {
  882. break
  883. }
  884. }
  885. if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
  886. iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
  887. }
  888. iter.decrementDepth()
  889. }
  890. type nineFieldsStructDecoder struct {
  891. typ reflect2.Type
  892. fieldHash1 int64
  893. fieldDecoder1 *structFieldDecoder
  894. fieldHash2 int64
  895. fieldDecoder2 *structFieldDecoder
  896. fieldHash3 int64
  897. fieldDecoder3 *structFieldDecoder
  898. fieldHash4 int64
  899. fieldDecoder4 *structFieldDecoder
  900. fieldHash5 int64
  901. fieldDecoder5 *structFieldDecoder
  902. fieldHash6 int64
  903. fieldDecoder6 *structFieldDecoder
  904. fieldHash7 int64
  905. fieldDecoder7 *structFieldDecoder
  906. fieldHash8 int64
  907. fieldDecoder8 *structFieldDecoder
  908. fieldHash9 int64
  909. fieldDecoder9 *structFieldDecoder
  910. }
  911. func (decoder *nineFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  912. if !iter.readObjectStart() {
  913. return
  914. }
  915. if !iter.incrementDepth() {
  916. return
  917. }
  918. for {
  919. switch iter.readFieldHash() {
  920. case decoder.fieldHash1:
  921. decoder.fieldDecoder1.Decode(ptr, iter)
  922. case decoder.fieldHash2:
  923. decoder.fieldDecoder2.Decode(ptr, iter)
  924. case decoder.fieldHash3:
  925. decoder.fieldDecoder3.Decode(ptr, iter)
  926. case decoder.fieldHash4:
  927. decoder.fieldDecoder4.Decode(ptr, iter)
  928. case decoder.fieldHash5:
  929. decoder.fieldDecoder5.Decode(ptr, iter)
  930. case decoder.fieldHash6:
  931. decoder.fieldDecoder6.Decode(ptr, iter)
  932. case decoder.fieldHash7:
  933. decoder.fieldDecoder7.Decode(ptr, iter)
  934. case decoder.fieldHash8:
  935. decoder.fieldDecoder8.Decode(ptr, iter)
  936. case decoder.fieldHash9:
  937. decoder.fieldDecoder9.Decode(ptr, iter)
  938. default:
  939. iter.Skip()
  940. }
  941. if iter.isObjectEnd() {
  942. break
  943. }
  944. }
  945. if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
  946. iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
  947. }
  948. iter.decrementDepth()
  949. }
  950. type tenFieldsStructDecoder struct {
  951. typ reflect2.Type
  952. fieldHash1 int64
  953. fieldDecoder1 *structFieldDecoder
  954. fieldHash2 int64
  955. fieldDecoder2 *structFieldDecoder
  956. fieldHash3 int64
  957. fieldDecoder3 *structFieldDecoder
  958. fieldHash4 int64
  959. fieldDecoder4 *structFieldDecoder
  960. fieldHash5 int64
  961. fieldDecoder5 *structFieldDecoder
  962. fieldHash6 int64
  963. fieldDecoder6 *structFieldDecoder
  964. fieldHash7 int64
  965. fieldDecoder7 *structFieldDecoder
  966. fieldHash8 int64
  967. fieldDecoder8 *structFieldDecoder
  968. fieldHash9 int64
  969. fieldDecoder9 *structFieldDecoder
  970. fieldHash10 int64
  971. fieldDecoder10 *structFieldDecoder
  972. }
  973. func (decoder *tenFieldsStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  974. if !iter.readObjectStart() {
  975. return
  976. }
  977. if !iter.incrementDepth() {
  978. return
  979. }
  980. for {
  981. switch iter.readFieldHash() {
  982. case decoder.fieldHash1:
  983. decoder.fieldDecoder1.Decode(ptr, iter)
  984. case decoder.fieldHash2:
  985. decoder.fieldDecoder2.Decode(ptr, iter)
  986. case decoder.fieldHash3:
  987. decoder.fieldDecoder3.Decode(ptr, iter)
  988. case decoder.fieldHash4:
  989. decoder.fieldDecoder4.Decode(ptr, iter)
  990. case decoder.fieldHash5:
  991. decoder.fieldDecoder5.Decode(ptr, iter)
  992. case decoder.fieldHash6:
  993. decoder.fieldDecoder6.Decode(ptr, iter)
  994. case decoder.fieldHash7:
  995. decoder.fieldDecoder7.Decode(ptr, iter)
  996. case decoder.fieldHash8:
  997. decoder.fieldDecoder8.Decode(ptr, iter)
  998. case decoder.fieldHash9:
  999. decoder.fieldDecoder9.Decode(ptr, iter)
  1000. case decoder.fieldHash10:
  1001. decoder.fieldDecoder10.Decode(ptr, iter)
  1002. default:
  1003. iter.Skip()
  1004. }
  1005. if iter.isObjectEnd() {
  1006. break
  1007. }
  1008. }
  1009. if iter.Error != nil && iter.Error != io.EOF && len(decoder.typ.Type1().Name()) != 0 {
  1010. iter.Error = fmt.Errorf("%v.%s", decoder.typ, iter.Error.Error())
  1011. }
  1012. iter.decrementDepth()
  1013. }
  1014. type structFieldDecoder struct {
  1015. field reflect2.StructField
  1016. fieldDecoder ValDecoder
  1017. }
  1018. func (decoder *structFieldDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  1019. fieldPtr := decoder.field.UnsafeGet(ptr)
  1020. decoder.fieldDecoder.Decode(fieldPtr, iter)
  1021. if iter.Error != nil && iter.Error != io.EOF {
  1022. iter.Error = fmt.Errorf("%s: %s", decoder.field.Name(), iter.Error.Error())
  1023. }
  1024. }
  1025. type stringModeStringDecoder struct {
  1026. elemDecoder ValDecoder
  1027. cfg *frozenConfig
  1028. }
  1029. func (decoder *stringModeStringDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  1030. decoder.elemDecoder.Decode(ptr, iter)
  1031. str := *((*string)(ptr))
  1032. tempIter := decoder.cfg.BorrowIterator([]byte(str))
  1033. defer decoder.cfg.ReturnIterator(tempIter)
  1034. *((*string)(ptr)) = tempIter.ReadString()
  1035. }
  1036. type stringModeNumberDecoder struct {
  1037. elemDecoder ValDecoder
  1038. }
  1039. func (decoder *stringModeNumberDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
  1040. if iter.WhatIsNext() == NilValue {
  1041. decoder.elemDecoder.Decode(ptr, iter)
  1042. return
  1043. }
  1044. c := iter.nextToken()
  1045. if c != '"' {
  1046. iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
  1047. return
  1048. }
  1049. decoder.elemDecoder.Decode(ptr, iter)
  1050. if iter.Error != nil {
  1051. return
  1052. }
  1053. c = iter.readByte()
  1054. if c != '"' {
  1055. iter.ReportError("stringModeNumberDecoder", `expect ", but found `+string([]byte{c}))
  1056. return
  1057. }
  1058. }