jsonunmarshaler_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. package mapping
  2. import (
  3. "bytes"
  4. "reflect"
  5. "strings"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestUnmarshalBytes(t *testing.T) {
  10. var c struct {
  11. Name string
  12. }
  13. content := []byte(`{"Name": "liao"}`)
  14. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  15. assert.Equal(t, "liao", c.Name)
  16. }
  17. func TestUnmarshalBytesOptional(t *testing.T) {
  18. var c struct {
  19. Name string
  20. Age int `json:",optional"`
  21. }
  22. content := []byte(`{"Name": "liao"}`)
  23. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  24. assert.Equal(t, "liao", c.Name)
  25. }
  26. func TestUnmarshalBytesOptionalDefault(t *testing.T) {
  27. var c struct {
  28. Name string
  29. Age int `json:",optional,default=1"`
  30. }
  31. content := []byte(`{"Name": "liao"}`)
  32. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  33. assert.Equal(t, "liao", c.Name)
  34. assert.Equal(t, 1, c.Age)
  35. }
  36. func TestUnmarshalBytesDefaultOptional(t *testing.T) {
  37. var c struct {
  38. Name string
  39. Age int `json:",default=1,optional"`
  40. }
  41. content := []byte(`{"Name": "liao"}`)
  42. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  43. assert.Equal(t, "liao", c.Name)
  44. assert.Equal(t, 1, c.Age)
  45. }
  46. func TestUnmarshalBytesDefault(t *testing.T) {
  47. var c struct {
  48. Name string `json:",default=liao"`
  49. }
  50. content := []byte(`{}`)
  51. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  52. assert.Equal(t, "liao", c.Name)
  53. }
  54. func TestUnmarshalBytesBool(t *testing.T) {
  55. var c struct {
  56. Great bool
  57. }
  58. content := []byte(`{"Great": true}`)
  59. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  60. assert.True(t, c.Great)
  61. }
  62. func TestUnmarshalBytesInt(t *testing.T) {
  63. var c struct {
  64. Age int
  65. }
  66. content := []byte(`{"Age": 1}`)
  67. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  68. assert.Equal(t, 1, c.Age)
  69. }
  70. func TestUnmarshalBytesUint(t *testing.T) {
  71. var c struct {
  72. Age uint
  73. }
  74. content := []byte(`{"Age": 1}`)
  75. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  76. assert.Equal(t, uint(1), c.Age)
  77. }
  78. func TestUnmarshalBytesFloat(t *testing.T) {
  79. var c struct {
  80. Age float32
  81. }
  82. content := []byte(`{"Age": 1.5}`)
  83. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  84. assert.Equal(t, float32(1.5), c.Age)
  85. }
  86. func TestUnmarshalBytesMustInOptional(t *testing.T) {
  87. var c struct {
  88. Inner struct {
  89. There string
  90. Must string
  91. Optional string `json:",optional"`
  92. } `json:",optional"`
  93. }
  94. content := []byte(`{}`)
  95. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  96. }
  97. func TestUnmarshalBytesMustInOptionalMissedPart(t *testing.T) {
  98. var c struct {
  99. Inner struct {
  100. There string
  101. Must string
  102. Optional string `json:",optional"`
  103. } `json:",optional"`
  104. }
  105. content := []byte(`{"Inner": {"There": "sure"}}`)
  106. assert.NotNil(t, UnmarshalJsonBytes(content, &c))
  107. }
  108. func TestUnmarshalBytesMustInOptionalOnlyOptionalFilled(t *testing.T) {
  109. var c struct {
  110. Inner struct {
  111. There string
  112. Must string
  113. Optional string `json:",optional"`
  114. } `json:",optional"`
  115. }
  116. content := []byte(`{"Inner": {"Optional": "sure"}}`)
  117. assert.NotNil(t, UnmarshalJsonBytes(content, &c))
  118. }
  119. func TestUnmarshalBytesNil(t *testing.T) {
  120. var c struct {
  121. Int int64 `json:"int,optional"`
  122. }
  123. content := []byte(`{"int":null}`)
  124. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  125. assert.Equal(t, int64(0), c.Int)
  126. }
  127. func TestUnmarshalBytesNilSlice(t *testing.T) {
  128. var c struct {
  129. Ints []int64 `json:"ints"`
  130. }
  131. content := []byte(`{"ints":[null]}`)
  132. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  133. assert.Equal(t, 0, len(c.Ints))
  134. }
  135. func TestUnmarshalBytesPartial(t *testing.T) {
  136. var c struct {
  137. Name string
  138. Age float32
  139. }
  140. content := []byte(`{"Age": 1.5}`)
  141. assert.NotNil(t, UnmarshalJsonBytes(content, &c))
  142. }
  143. func TestUnmarshalBytesStruct(t *testing.T) {
  144. var c struct {
  145. Inner struct {
  146. Name string
  147. }
  148. }
  149. content := []byte(`{"Inner": {"Name": "liao"}}`)
  150. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  151. assert.Equal(t, "liao", c.Inner.Name)
  152. }
  153. func TestUnmarshalBytesStructOptional(t *testing.T) {
  154. var c struct {
  155. Inner struct {
  156. Name string
  157. Age int `json:",optional"`
  158. }
  159. }
  160. content := []byte(`{"Inner": {"Name": "liao"}}`)
  161. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  162. assert.Equal(t, "liao", c.Inner.Name)
  163. }
  164. func TestUnmarshalBytesStructPtr(t *testing.T) {
  165. var c struct {
  166. Inner *struct {
  167. Name string
  168. }
  169. }
  170. content := []byte(`{"Inner": {"Name": "liao"}}`)
  171. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  172. assert.Equal(t, "liao", c.Inner.Name)
  173. }
  174. func TestUnmarshalBytesStructPtrOptional(t *testing.T) {
  175. var c struct {
  176. Inner *struct {
  177. Name string
  178. Age int `json:",optional"`
  179. }
  180. }
  181. content := []byte(`{"Inner": {"Name": "liao"}}`)
  182. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  183. }
  184. func TestUnmarshalBytesStructPtrDefault(t *testing.T) {
  185. var c struct {
  186. Inner *struct {
  187. Name string
  188. Age int `json:",default=4"`
  189. }
  190. }
  191. content := []byte(`{"Inner": {"Name": "liao"}}`)
  192. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  193. assert.Equal(t, "liao", c.Inner.Name)
  194. assert.Equal(t, 4, c.Inner.Age)
  195. }
  196. func TestUnmarshalBytesSliceString(t *testing.T) {
  197. var c struct {
  198. Names []string
  199. }
  200. content := []byte(`{"Names": ["liao", "chaoxin"]}`)
  201. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  202. want := []string{"liao", "chaoxin"}
  203. if !reflect.DeepEqual(c.Names, want) {
  204. t.Fatalf("want %q, got %q", c.Names, want)
  205. }
  206. }
  207. func TestUnmarshalBytesSliceStringOptional(t *testing.T) {
  208. var c struct {
  209. Names []string
  210. Age []int `json:",optional"`
  211. }
  212. content := []byte(`{"Names": ["liao", "chaoxin"]}`)
  213. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  214. want := []string{"liao", "chaoxin"}
  215. if !reflect.DeepEqual(c.Names, want) {
  216. t.Fatalf("want %q, got %q", c.Names, want)
  217. }
  218. }
  219. func TestUnmarshalBytesSliceStruct(t *testing.T) {
  220. var c struct {
  221. People []struct {
  222. Name string
  223. Age int
  224. }
  225. }
  226. content := []byte(`{"People": [{"Name": "liao", "Age": 1}, {"Name": "chaoxin", "Age": 2}]}`)
  227. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  228. want := []struct {
  229. Name string
  230. Age int
  231. }{
  232. {"liao", 1},
  233. {"chaoxin", 2},
  234. }
  235. if !reflect.DeepEqual(c.People, want) {
  236. t.Fatalf("want %q, got %q", c.People, want)
  237. }
  238. }
  239. func TestUnmarshalBytesSliceStructOptional(t *testing.T) {
  240. var c struct {
  241. People []struct {
  242. Name string
  243. Age int
  244. Emails []string `json:",optional"`
  245. }
  246. }
  247. content := []byte(`{"People": [{"Name": "liao", "Age": 1}, {"Name": "chaoxin", "Age": 2}]}`)
  248. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  249. want := []struct {
  250. Name string
  251. Age int
  252. Emails []string `json:",optional"`
  253. }{
  254. {"liao", 1, nil},
  255. {"chaoxin", 2, nil},
  256. }
  257. if !reflect.DeepEqual(c.People, want) {
  258. t.Fatalf("want %q, got %q", c.People, want)
  259. }
  260. }
  261. func TestUnmarshalBytesSliceStructPtr(t *testing.T) {
  262. var c struct {
  263. People []*struct {
  264. Name string
  265. Age int
  266. }
  267. }
  268. content := []byte(`{"People": [{"Name": "liao", "Age": 1}, {"Name": "chaoxin", "Age": 2}]}`)
  269. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  270. want := []*struct {
  271. Name string
  272. Age int
  273. }{
  274. {"liao", 1},
  275. {"chaoxin", 2},
  276. }
  277. if !reflect.DeepEqual(c.People, want) {
  278. t.Fatalf("want %v, got %v", c.People, want)
  279. }
  280. }
  281. func TestUnmarshalBytesSliceStructPtrOptional(t *testing.T) {
  282. var c struct {
  283. People []*struct {
  284. Name string
  285. Age int
  286. Emails []string `json:",optional"`
  287. }
  288. }
  289. content := []byte(`{"People": [{"Name": "liao", "Age": 1}, {"Name": "chaoxin", "Age": 2}]}`)
  290. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  291. want := []*struct {
  292. Name string
  293. Age int
  294. Emails []string `json:",optional"`
  295. }{
  296. {"liao", 1, nil},
  297. {"chaoxin", 2, nil},
  298. }
  299. if !reflect.DeepEqual(c.People, want) {
  300. t.Fatalf("want %v, got %v", c.People, want)
  301. }
  302. }
  303. func TestUnmarshalBytesSliceStructPtrPartial(t *testing.T) {
  304. var c struct {
  305. People []*struct {
  306. Name string
  307. Age int
  308. Email string
  309. }
  310. }
  311. content := []byte(`{"People": [{"Name": "liao", "Age": 1}, {"Name": "chaoxin", "Age": 2}]}`)
  312. assert.NotNil(t, UnmarshalJsonBytes(content, &c))
  313. }
  314. func TestUnmarshalBytesSliceStructPtrDefault(t *testing.T) {
  315. var c struct {
  316. People []*struct {
  317. Name string
  318. Age int
  319. Email string `json:",default=chaoxin@liao.com"`
  320. }
  321. }
  322. content := []byte(`{"People": [{"Name": "liao", "Age": 1}, {"Name": "chaoxin", "Age": 2}]}`)
  323. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  324. want := []*struct {
  325. Name string
  326. Age int
  327. Email string
  328. }{
  329. {"liao", 1, "chaoxin@liao.com"},
  330. {"chaoxin", 2, "chaoxin@liao.com"},
  331. }
  332. for i := range c.People {
  333. actual := c.People[i]
  334. expect := want[i]
  335. assert.Equal(t, expect.Age, actual.Age)
  336. assert.Equal(t, expect.Email, actual.Email)
  337. assert.Equal(t, expect.Name, actual.Name)
  338. }
  339. }
  340. func TestUnmarshalBytesSliceStringPartial(t *testing.T) {
  341. var c struct {
  342. Names []string
  343. Age int
  344. }
  345. content := []byte(`{"Age": 1}`)
  346. assert.NotNil(t, UnmarshalJsonBytes(content, &c))
  347. }
  348. func TestUnmarshalBytesSliceStructPartial(t *testing.T) {
  349. var c struct {
  350. Group string
  351. People []struct {
  352. Name string
  353. Age int
  354. }
  355. }
  356. content := []byte(`{"Group": "chaoxin"}`)
  357. assert.NotNil(t, UnmarshalJsonBytes(content, &c))
  358. }
  359. func TestUnmarshalBytesInnerAnonymousPartial(t *testing.T) {
  360. type (
  361. Deep struct {
  362. A string
  363. B string `json:",optional"`
  364. }
  365. Inner struct {
  366. Deep
  367. InnerV string `json:",optional"`
  368. }
  369. )
  370. var c struct {
  371. Value Inner `json:",optional"`
  372. }
  373. content := []byte(`{"Value": {"InnerV": "chaoxin"}}`)
  374. assert.NotNil(t, UnmarshalJsonBytes(content, &c))
  375. }
  376. func TestUnmarshalBytesStructPartial(t *testing.T) {
  377. var c struct {
  378. Group string
  379. Person struct {
  380. Name string
  381. Age int
  382. }
  383. }
  384. content := []byte(`{"Group": "chaoxin"}`)
  385. assert.NotNil(t, UnmarshalJsonBytes(content, &c))
  386. }
  387. func TestUnmarshalBytesEmptyMap(t *testing.T) {
  388. var c struct {
  389. Persons map[string]int `json:",optional"`
  390. }
  391. content := []byte(`{"Persons": {}}`)
  392. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  393. assert.Empty(t, c.Persons)
  394. }
  395. func TestUnmarshalBytesMap(t *testing.T) {
  396. var c struct {
  397. Persons map[string]int
  398. }
  399. content := []byte(`{"Persons": {"first": 1, "second": 2}}`)
  400. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  401. assert.Equal(t, 2, len(c.Persons))
  402. assert.Equal(t, 1, c.Persons["first"])
  403. assert.Equal(t, 2, c.Persons["second"])
  404. }
  405. func TestUnmarshalBytesMapStruct(t *testing.T) {
  406. var c struct {
  407. Persons map[string]struct {
  408. ID int
  409. Name string `json:"name,optional"`
  410. }
  411. }
  412. content := []byte(`{"Persons": {"first": {"ID": 1, "name": "kevin"}}}`)
  413. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  414. assert.Equal(t, 1, len(c.Persons))
  415. assert.Equal(t, 1, c.Persons["first"].ID)
  416. assert.Equal(t, "kevin", c.Persons["first"].Name)
  417. }
  418. func TestUnmarshalBytesMapStructPtr(t *testing.T) {
  419. var c struct {
  420. Persons map[string]*struct {
  421. ID int
  422. Name string `json:"name,optional"`
  423. }
  424. }
  425. content := []byte(`{"Persons": {"first": {"ID": 1, "name": "kevin"}}}`)
  426. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  427. assert.Equal(t, 1, len(c.Persons))
  428. assert.Equal(t, 1, c.Persons["first"].ID)
  429. assert.Equal(t, "kevin", c.Persons["first"].Name)
  430. }
  431. func TestUnmarshalBytesMapStructMissingPartial(t *testing.T) {
  432. var c struct {
  433. Persons map[string]*struct {
  434. ID int
  435. Name string
  436. }
  437. }
  438. content := []byte(`{"Persons": {"first": {"ID": 1}}}`)
  439. assert.NotNil(t, UnmarshalJsonBytes(content, &c))
  440. }
  441. func TestUnmarshalBytesMapStructOptional(t *testing.T) {
  442. var c struct {
  443. Persons map[string]*struct {
  444. ID int
  445. Name string `json:"name,optional"`
  446. }
  447. }
  448. content := []byte(`{"Persons": {"first": {"ID": 1}}}`)
  449. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  450. assert.Equal(t, 1, len(c.Persons))
  451. assert.Equal(t, 1, c.Persons["first"].ID)
  452. }
  453. func TestUnmarshalBytesMapEmptyStructSlice(t *testing.T) {
  454. var c struct {
  455. Persons map[string][]struct {
  456. ID int
  457. Name string `json:"name,optional"`
  458. }
  459. }
  460. content := []byte(`{"Persons": {"first": []}}`)
  461. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  462. assert.Equal(t, 1, len(c.Persons))
  463. assert.Empty(t, c.Persons["first"])
  464. }
  465. func TestUnmarshalBytesMapStructSlice(t *testing.T) {
  466. var c struct {
  467. Persons map[string][]struct {
  468. ID int
  469. Name string `json:"name,optional"`
  470. }
  471. }
  472. content := []byte(`{"Persons": {"first": [{"ID": 1, "name": "kevin"}]}}`)
  473. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  474. assert.Equal(t, 1, len(c.Persons))
  475. assert.Equal(t, 1, c.Persons["first"][0].ID)
  476. assert.Equal(t, "kevin", c.Persons["first"][0].Name)
  477. }
  478. func TestUnmarshalBytesMapEmptyStructPtrSlice(t *testing.T) {
  479. var c struct {
  480. Persons map[string][]*struct {
  481. ID int
  482. Name string `json:"name,optional"`
  483. }
  484. }
  485. content := []byte(`{"Persons": {"first": []}}`)
  486. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  487. assert.Equal(t, 1, len(c.Persons))
  488. assert.Empty(t, c.Persons["first"])
  489. }
  490. func TestUnmarshalBytesMapStructPtrSlice(t *testing.T) {
  491. var c struct {
  492. Persons map[string][]*struct {
  493. ID int
  494. Name string `json:"name,optional"`
  495. }
  496. }
  497. content := []byte(`{"Persons": {"first": [{"ID": 1, "name": "kevin"}]}}`)
  498. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  499. assert.Equal(t, 1, len(c.Persons))
  500. assert.Equal(t, 1, c.Persons["first"][0].ID)
  501. assert.Equal(t, "kevin", c.Persons["first"][0].Name)
  502. }
  503. func TestUnmarshalBytesMapStructPtrSliceMissingPartial(t *testing.T) {
  504. var c struct {
  505. Persons map[string][]*struct {
  506. ID int
  507. Name string
  508. }
  509. }
  510. content := []byte(`{"Persons": {"first": [{"ID": 1}]}}`)
  511. assert.NotNil(t, UnmarshalJsonBytes(content, &c))
  512. }
  513. func TestUnmarshalBytesMapStructPtrSliceOptional(t *testing.T) {
  514. var c struct {
  515. Persons map[string][]*struct {
  516. ID int
  517. Name string `json:"name,optional"`
  518. }
  519. }
  520. content := []byte(`{"Persons": {"first": [{"ID": 1}]}}`)
  521. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  522. assert.Equal(t, 1, len(c.Persons))
  523. assert.Equal(t, 1, c.Persons["first"][0].ID)
  524. }
  525. func TestUnmarshalStructOptional(t *testing.T) {
  526. var c struct {
  527. Name string
  528. Etcd struct {
  529. Hosts []string
  530. Key string
  531. } `json:",optional"`
  532. }
  533. content := []byte(`{"Name": "kevin"}`)
  534. err := UnmarshalJsonBytes(content, &c)
  535. assert.Nil(t, err)
  536. assert.Equal(t, "kevin", c.Name)
  537. }
  538. func TestUnmarshalStructLowerCase(t *testing.T) {
  539. var c struct {
  540. Name string
  541. Etcd struct {
  542. Key string
  543. } `json:"etcd"`
  544. }
  545. content := []byte(`{"Name": "kevin", "etcd": {"Key": "the key"}}`)
  546. err := UnmarshalJsonBytes(content, &c)
  547. assert.Nil(t, err)
  548. assert.Equal(t, "kevin", c.Name)
  549. assert.Equal(t, "the key", c.Etcd.Key)
  550. }
  551. func TestUnmarshalWithStructAllOptionalWithEmpty(t *testing.T) {
  552. var c struct {
  553. Inner struct {
  554. Optional string `json:",optional"`
  555. }
  556. Else string
  557. }
  558. content := []byte(`{"Else": "sure", "Inner": {}}`)
  559. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  560. }
  561. func TestUnmarshalWithStructAllOptionalPtr(t *testing.T) {
  562. var c struct {
  563. Inner *struct {
  564. Optional string `json:",optional"`
  565. }
  566. Else string
  567. }
  568. content := []byte(`{"Else": "sure", "Inner": {}}`)
  569. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  570. }
  571. func TestUnmarshalWithStructOptional(t *testing.T) {
  572. type Inner struct {
  573. Must string
  574. }
  575. var c struct {
  576. In Inner `json:",optional"`
  577. Else string
  578. }
  579. content := []byte(`{"Else": "sure"}`)
  580. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  581. assert.Equal(t, "sure", c.Else)
  582. assert.Equal(t, "", c.In.Must)
  583. }
  584. func TestUnmarshalWithStructPtrOptional(t *testing.T) {
  585. type Inner struct {
  586. Must string
  587. }
  588. var c struct {
  589. In *Inner `json:",optional"`
  590. Else string
  591. }
  592. content := []byte(`{"Else": "sure"}`)
  593. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  594. assert.Equal(t, "sure", c.Else)
  595. assert.Nil(t, c.In)
  596. }
  597. func TestUnmarshalWithStructAllOptionalAnonymous(t *testing.T) {
  598. type Inner struct {
  599. Optional string `json:",optional"`
  600. }
  601. var c struct {
  602. Inner
  603. Else string
  604. }
  605. content := []byte(`{"Else": "sure"}`)
  606. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  607. }
  608. func TestUnmarshalWithStructAllOptionalAnonymousPtr(t *testing.T) {
  609. type Inner struct {
  610. Optional string `json:",optional"`
  611. }
  612. var c struct {
  613. *Inner
  614. Else string
  615. }
  616. content := []byte(`{"Else": "sure"}`)
  617. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  618. }
  619. func TestUnmarshalWithStructAllOptionalProvoidedAnonymous(t *testing.T) {
  620. type Inner struct {
  621. Optional string `json:",optional"`
  622. }
  623. var c struct {
  624. Inner
  625. Else string
  626. }
  627. content := []byte(`{"Else": "sure", "Optional": "optional"}`)
  628. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  629. assert.Equal(t, "sure", c.Else)
  630. assert.Equal(t, "optional", c.Optional)
  631. }
  632. func TestUnmarshalWithStructAllOptionalProvoidedAnonymousPtr(t *testing.T) {
  633. type Inner struct {
  634. Optional string `json:",optional"`
  635. }
  636. var c struct {
  637. *Inner
  638. Else string
  639. }
  640. content := []byte(`{"Else": "sure", "Optional": "optional"}`)
  641. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  642. assert.Equal(t, "sure", c.Else)
  643. assert.Equal(t, "optional", c.Optional)
  644. }
  645. func TestUnmarshalWithStructAnonymous(t *testing.T) {
  646. type Inner struct {
  647. Must string
  648. }
  649. var c struct {
  650. Inner
  651. Else string
  652. }
  653. content := []byte(`{"Else": "sure", "Must": "must"}`)
  654. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  655. assert.Equal(t, "sure", c.Else)
  656. assert.Equal(t, "must", c.Must)
  657. }
  658. func TestUnmarshalWithStructAnonymousPtr(t *testing.T) {
  659. type Inner struct {
  660. Must string
  661. }
  662. var c struct {
  663. *Inner
  664. Else string
  665. }
  666. content := []byte(`{"Else": "sure", "Must": "must"}`)
  667. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  668. assert.Equal(t, "sure", c.Else)
  669. assert.Equal(t, "must", c.Must)
  670. }
  671. func TestUnmarshalWithStructAnonymousOptional(t *testing.T) {
  672. type Inner struct {
  673. Must string
  674. }
  675. var c struct {
  676. Inner `json:",optional"`
  677. Else string
  678. }
  679. content := []byte(`{"Else": "sure"}`)
  680. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  681. assert.Equal(t, "sure", c.Else)
  682. assert.Equal(t, "", c.Must)
  683. }
  684. func TestUnmarshalWithStructPtrAnonymousOptional(t *testing.T) {
  685. type Inner struct {
  686. Must string
  687. }
  688. var c struct {
  689. *Inner `json:",optional"`
  690. Else string
  691. }
  692. content := []byte(`{"Else": "sure"}`)
  693. assert.Nil(t, UnmarshalJsonBytes(content, &c))
  694. assert.Equal(t, "sure", c.Else)
  695. assert.Nil(t, c.Inner)
  696. }
  697. func TestUnmarshalWithZeroValues(t *testing.T) {
  698. type inner struct {
  699. False bool `json:"no"`
  700. Int int `json:"int"`
  701. String string `json:"string"`
  702. }
  703. content := []byte(`{"no": false, "int": 0, "string": ""}`)
  704. reader := bytes.NewReader(content)
  705. var in inner
  706. ast := assert.New(t)
  707. ast.Nil(UnmarshalJsonReader(reader, &in))
  708. ast.False(in.False)
  709. ast.Equal(0, in.Int)
  710. ast.Equal("", in.String)
  711. }
  712. func TestUnmarshalBytesError(t *testing.T) {
  713. payload := `[{"abcd": "cdef"}]`
  714. var v struct {
  715. Any string
  716. }
  717. err := UnmarshalJsonBytes([]byte(payload), &v)
  718. assert.NotNil(t, err)
  719. assert.True(t, strings.Contains(err.Error(), payload))
  720. }
  721. func TestUnmarshalReaderError(t *testing.T) {
  722. payload := `[{"abcd": "cdef"}]`
  723. reader := strings.NewReader(payload)
  724. var v struct {
  725. Any string
  726. }
  727. err := UnmarshalJsonReader(reader, &v)
  728. assert.NotNil(t, err)
  729. assert.True(t, strings.Contains(err.Error(), payload))
  730. }