reflect_struct_decoder.go 28 KB

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