feature_reflect_object.go 32 KB

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