decode_test.go 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. // Copyright 2019 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package json_test
  5. import (
  6. "strings"
  7. "testing"
  8. "unicode/utf8"
  9. "google.golang.org/protobuf/internal/encoding/json"
  10. )
  11. type R struct {
  12. // T is expected Type returned from calling Decoder.Read.
  13. T json.Type
  14. // E is expected error substring from calling Decoder.Read if set.
  15. E string
  16. // V is expected value from calling
  17. // Value.{Bool()|Float()|Int()|Uint()|String()} depending on type.
  18. V interface{}
  19. // VE is expected error substring from calling
  20. // Value.{Bool()|Float()|Int()|Uint()|String()} depending on type if set.
  21. VE string
  22. }
  23. func TestDecoder(t *testing.T) {
  24. const space = " \n\r\t"
  25. tests := []struct {
  26. input string
  27. // want is a list of expected values returned from calling
  28. // Decoder.Read. An item makes the test code invoke
  29. // Decoder.Read and compare against R.T and R.E. For Bool,
  30. // Number and String tokens, it invokes the corresponding getter method
  31. // and compares the returned value against R.V or R.VE if it returned an
  32. // error.
  33. want []R
  34. }{
  35. {
  36. input: ``,
  37. want: []R{{T: json.EOF}},
  38. },
  39. {
  40. input: space,
  41. want: []R{{T: json.EOF}},
  42. },
  43. {
  44. // Calling Read after EOF will keep returning EOF for
  45. // succeeding Read calls.
  46. input: space,
  47. want: []R{
  48. {T: json.EOF},
  49. {T: json.EOF},
  50. {T: json.EOF},
  51. },
  52. },
  53. // JSON literals.
  54. {
  55. input: space + `null` + space,
  56. want: []R{
  57. {T: json.Null},
  58. {T: json.EOF},
  59. },
  60. },
  61. {
  62. input: space + `true` + space,
  63. want: []R{
  64. {T: json.Bool, V: true},
  65. {T: json.EOF},
  66. },
  67. },
  68. {
  69. input: space + `false` + space,
  70. want: []R{
  71. {T: json.Bool, V: false},
  72. {T: json.EOF},
  73. },
  74. },
  75. {
  76. // Error returned will produce the same error again.
  77. input: space + `foo` + space,
  78. want: []R{
  79. {E: `invalid value foo`},
  80. {E: `invalid value foo`},
  81. },
  82. },
  83. // JSON strings.
  84. {
  85. input: space + `""` + space,
  86. want: []R{
  87. {T: json.String, V: ""},
  88. {T: json.EOF},
  89. },
  90. },
  91. {
  92. input: space + `"hello"` + space,
  93. want: []R{
  94. {T: json.String, V: "hello"},
  95. {T: json.EOF},
  96. },
  97. },
  98. {
  99. input: `"hello`,
  100. want: []R{{E: `unexpected EOF`}},
  101. },
  102. {
  103. input: "\"\x00\"",
  104. want: []R{{E: `invalid character '\x00' in string`}},
  105. },
  106. {
  107. input: "\"\u0031\u0032\"",
  108. want: []R{
  109. {T: json.String, V: "12"},
  110. {T: json.EOF},
  111. },
  112. },
  113. {
  114. // Invalid UTF-8 error is returned in ReadString instead of Read.
  115. input: "\"\xff\"",
  116. want: []R{
  117. {T: json.String, E: `invalid UTF-8 detected`, V: string("\xff")},
  118. {T: json.EOF},
  119. },
  120. },
  121. {
  122. input: `"` + string(utf8.RuneError) + `"`,
  123. want: []R{
  124. {T: json.String, V: string(utf8.RuneError)},
  125. {T: json.EOF},
  126. },
  127. },
  128. {
  129. input: `"\uFFFD"`,
  130. want: []R{
  131. {T: json.String, V: string(utf8.RuneError)},
  132. {T: json.EOF},
  133. },
  134. },
  135. {
  136. input: `"\x"`,
  137. want: []R{{E: `invalid escape code "\\x" in string`}},
  138. },
  139. {
  140. input: `"\uXXXX"`,
  141. want: []R{{E: `invalid escape code "\\uXXXX" in string`}},
  142. },
  143. {
  144. input: `"\uDEAD"`, // unmatched surrogate pair
  145. want: []R{{E: `unexpected EOF`}},
  146. },
  147. {
  148. input: `"\uDEAD\uBEEF"`, // invalid surrogate half
  149. want: []R{{E: `invalid escape code "\\uBEEF" in string`}},
  150. },
  151. {
  152. input: `"\uD800\udead"`, // valid surrogate pair
  153. want: []R{
  154. {T: json.String, V: `𐊭`},
  155. {T: json.EOF},
  156. },
  157. },
  158. {
  159. input: `"\u0000\"\\\/\b\f\n\r\t"`,
  160. want: []R{
  161. {T: json.String, V: "\u0000\"\\/\b\f\n\r\t"},
  162. {T: json.EOF},
  163. },
  164. },
  165. // Invalid JSON numbers.
  166. {
  167. input: `-`,
  168. want: []R{{E: `invalid number -`}},
  169. },
  170. {
  171. input: `+0`,
  172. want: []R{{E: `invalid value +0`}},
  173. },
  174. {
  175. input: `-+`,
  176. want: []R{{E: `invalid number -+`}},
  177. },
  178. {
  179. input: `0.`,
  180. want: []R{{E: `invalid number 0.`}},
  181. },
  182. {
  183. input: `.1`,
  184. want: []R{{E: `invalid value .1`}},
  185. },
  186. {
  187. input: `1.0.1`,
  188. want: []R{{E: `invalid number 1.0.1`}},
  189. },
  190. {
  191. input: `1..1`,
  192. want: []R{{E: `invalid number 1..1`}},
  193. },
  194. {
  195. input: `-1-2`,
  196. want: []R{{E: `invalid number -1-2`}},
  197. },
  198. {
  199. input: `01`,
  200. want: []R{{E: `invalid number 01`}},
  201. },
  202. {
  203. input: `1e`,
  204. want: []R{{E: `invalid number 1e`}},
  205. },
  206. {
  207. input: `1e1.2`,
  208. want: []R{{E: `invalid number 1e1.2`}},
  209. },
  210. {
  211. input: `1Ee`,
  212. want: []R{{E: `invalid number 1Ee`}},
  213. },
  214. {
  215. input: `1.e1`,
  216. want: []R{{E: `invalid number 1.e1`}},
  217. },
  218. {
  219. input: `1.e+`,
  220. want: []R{{E: `invalid number 1.e+`}},
  221. },
  222. {
  223. input: `1e+-2`,
  224. want: []R{{E: `invalid number 1e+-2`}},
  225. },
  226. {
  227. input: `1e--2`,
  228. want: []R{{E: `invalid number 1e--2`}},
  229. },
  230. {
  231. input: `1.0true`,
  232. want: []R{{E: `invalid number 1.0true`}},
  233. },
  234. // JSON numbers as floating point.
  235. {
  236. input: space + `0.0` + space,
  237. want: []R{
  238. {T: json.Number, V: float32(0)},
  239. {T: json.EOF},
  240. },
  241. },
  242. {
  243. input: space + `0` + space,
  244. want: []R{
  245. {T: json.Number, V: float32(0)},
  246. {T: json.EOF},
  247. },
  248. },
  249. {
  250. input: space + `-0` + space,
  251. want: []R{
  252. {T: json.Number, V: float32(0)},
  253. {T: json.EOF},
  254. },
  255. },
  256. {
  257. input: `-1.02`,
  258. want: []R{
  259. {T: json.Number, V: float32(-1.02)},
  260. {T: json.EOF},
  261. },
  262. },
  263. {
  264. input: `1.020000`,
  265. want: []R{
  266. {T: json.Number, V: float32(1.02)},
  267. {T: json.EOF},
  268. },
  269. },
  270. {
  271. input: `-1.0e0`,
  272. want: []R{
  273. {T: json.Number, V: float32(-1)},
  274. {T: json.EOF},
  275. },
  276. },
  277. {
  278. input: `1.0e-000`,
  279. want: []R{
  280. {T: json.Number, V: float32(1)},
  281. {T: json.EOF},
  282. },
  283. },
  284. {
  285. input: `1e+00`,
  286. want: []R{
  287. {T: json.Number, V: float32(1)},
  288. {T: json.EOF},
  289. },
  290. },
  291. {
  292. input: `1.02e3`,
  293. want: []R{
  294. {T: json.Number, V: float32(1.02e3)},
  295. {T: json.EOF},
  296. },
  297. },
  298. {
  299. input: `-1.02E03`,
  300. want: []R{
  301. {T: json.Number, V: float32(-1.02e3)},
  302. {T: json.EOF},
  303. },
  304. },
  305. {
  306. input: `1.0200e+3`,
  307. want: []R{
  308. {T: json.Number, V: float32(1.02e3)},
  309. {T: json.EOF},
  310. },
  311. },
  312. {
  313. input: `-1.0200E+03`,
  314. want: []R{
  315. {T: json.Number, V: float32(-1.02e3)},
  316. {T: json.EOF},
  317. },
  318. },
  319. {
  320. input: `1.0200e-3`,
  321. want: []R{
  322. {T: json.Number, V: float32(1.02e-3)},
  323. {T: json.EOF},
  324. },
  325. },
  326. {
  327. input: `-1.0200E-03`,
  328. want: []R{
  329. {T: json.Number, V: float32(-1.02e-3)},
  330. {T: json.EOF},
  331. },
  332. },
  333. {
  334. // Exceeds max float32 limit, but should be ok for float64.
  335. input: `3.4e39`,
  336. want: []R{
  337. {T: json.Number, V: float64(3.4e39)},
  338. {T: json.EOF},
  339. },
  340. },
  341. {
  342. // Exceeds max float32 limit.
  343. input: `3.4e39`,
  344. want: []R{
  345. {T: json.Number, V: float32(0), VE: `value out of range`},
  346. {T: json.EOF},
  347. },
  348. },
  349. {
  350. // Less than negative max float32 limit.
  351. input: `-3.4e39`,
  352. want: []R{
  353. {T: json.Number, V: float32(0), VE: `value out of range`},
  354. {T: json.EOF},
  355. },
  356. },
  357. {
  358. // Exceeds max float64 limit.
  359. input: `1.79e+309`,
  360. want: []R{
  361. {T: json.Number, V: float64(0), VE: `value out of range`},
  362. {T: json.EOF},
  363. },
  364. },
  365. {
  366. // Less than negative max float64 limit.
  367. input: `-1.79e+309`,
  368. want: []R{
  369. {T: json.Number, V: float64(0), VE: `value out of range`},
  370. {T: json.EOF},
  371. },
  372. },
  373. // JSON numbers as signed integers.
  374. {
  375. input: space + `0` + space,
  376. want: []R{
  377. {T: json.Number, V: int32(0)},
  378. {T: json.EOF},
  379. },
  380. },
  381. {
  382. input: space + `-0` + space,
  383. want: []R{
  384. {T: json.Number, V: int32(0)},
  385. {T: json.EOF},
  386. },
  387. },
  388. {
  389. // Fractional part equals 0 is ok.
  390. input: `1.00000`,
  391. want: []R{
  392. {T: json.Number, V: int32(1)},
  393. {T: json.EOF},
  394. },
  395. },
  396. {
  397. // Fractional part not equals 0 returns error.
  398. input: `1.0000000001`,
  399. want: []R{
  400. {T: json.Number, V: int32(0), VE: `cannot convert 1.0000000001 to integer`},
  401. {T: json.EOF},
  402. },
  403. },
  404. {
  405. input: `0e0`,
  406. want: []R{
  407. {T: json.Number, V: int32(0)},
  408. {T: json.EOF},
  409. },
  410. },
  411. {
  412. input: `0.0E0`,
  413. want: []R{
  414. {T: json.Number, V: int32(0)},
  415. {T: json.EOF},
  416. },
  417. },
  418. {
  419. input: `0.0E10`,
  420. want: []R{
  421. {T: json.Number, V: int32(0)},
  422. {T: json.EOF},
  423. },
  424. },
  425. {
  426. input: `-1`,
  427. want: []R{
  428. {T: json.Number, V: int32(-1)},
  429. {T: json.EOF},
  430. },
  431. },
  432. {
  433. input: `1.0e+0`,
  434. want: []R{
  435. {T: json.Number, V: int32(1)},
  436. {T: json.EOF},
  437. },
  438. },
  439. {
  440. input: `-1E-0`,
  441. want: []R{
  442. {T: json.Number, V: int32(-1)},
  443. {T: json.EOF},
  444. },
  445. },
  446. {
  447. input: `1E1`,
  448. want: []R{
  449. {T: json.Number, V: int32(10)},
  450. {T: json.EOF},
  451. },
  452. },
  453. {
  454. input: `-100.00e-02`,
  455. want: []R{
  456. {T: json.Number, V: int32(-1)},
  457. {T: json.EOF},
  458. },
  459. },
  460. {
  461. input: `0.1200E+02`,
  462. want: []R{
  463. {T: json.Number, V: int64(12)},
  464. {T: json.EOF},
  465. },
  466. },
  467. {
  468. input: `0.012e2`,
  469. want: []R{
  470. {T: json.Number, V: int32(0), VE: `cannot convert 0.012e2 to integer`},
  471. {T: json.EOF},
  472. },
  473. },
  474. {
  475. input: `12e-2`,
  476. want: []R{
  477. {T: json.Number, V: int32(0), VE: `cannot convert 12e-2 to integer`},
  478. {T: json.EOF},
  479. },
  480. },
  481. {
  482. // Exceeds math.MaxInt32.
  483. input: `2147483648`,
  484. want: []R{
  485. {T: json.Number, V: int32(0), VE: `value out of range`},
  486. {T: json.EOF},
  487. },
  488. },
  489. {
  490. // Exceeds math.MinInt32.
  491. input: `-2147483649`,
  492. want: []R{
  493. {T: json.Number, V: int32(0), VE: `value out of range`},
  494. {T: json.EOF},
  495. },
  496. },
  497. {
  498. // Exceeds math.MaxInt32, but ok for int64.
  499. input: `2147483648`,
  500. want: []R{
  501. {T: json.Number, V: int64(2147483648)},
  502. {T: json.EOF},
  503. },
  504. },
  505. {
  506. // Exceeds math.MinInt32, but ok for int64.
  507. input: `-2147483649`,
  508. want: []R{
  509. {T: json.Number, V: int64(-2147483649)},
  510. {T: json.EOF},
  511. },
  512. },
  513. {
  514. // Exceeds math.MaxInt64.
  515. input: `9223372036854775808`,
  516. want: []R{
  517. {T: json.Number, V: int64(0), VE: `value out of range`},
  518. {T: json.EOF},
  519. },
  520. },
  521. {
  522. // Exceeds math.MinInt64.
  523. input: `-9223372036854775809`,
  524. want: []R{
  525. {T: json.Number, V: int64(0), VE: `value out of range`},
  526. {T: json.EOF},
  527. },
  528. },
  529. // JSON numbers as unsigned integers.
  530. {
  531. input: space + `0` + space,
  532. want: []R{
  533. {T: json.Number, V: uint32(0)},
  534. {T: json.EOF},
  535. },
  536. },
  537. {
  538. input: space + `-0` + space,
  539. want: []R{
  540. {T: json.Number, V: uint32(0)},
  541. {T: json.EOF},
  542. },
  543. },
  544. {
  545. input: `-1`,
  546. want: []R{
  547. {T: json.Number, V: uint32(0), VE: `invalid syntax`},
  548. {T: json.EOF},
  549. },
  550. },
  551. {
  552. // Exceeds math.MaxUint32.
  553. input: `4294967296`,
  554. want: []R{
  555. {T: json.Number, V: uint32(0), VE: `value out of range`},
  556. {T: json.EOF},
  557. },
  558. },
  559. {
  560. // Exceeds math.MaxUint64.
  561. input: `18446744073709551616`,
  562. want: []R{
  563. {T: json.Number, V: uint64(0), VE: `value out of range`},
  564. {T: json.EOF},
  565. },
  566. },
  567. // JSON sequence of values.
  568. {
  569. input: `true null`,
  570. want: []R{
  571. {T: json.Bool, V: true},
  572. {E: `unexpected value null`},
  573. },
  574. },
  575. {
  576. input: "null false",
  577. want: []R{
  578. {T: json.Null},
  579. {E: `unexpected value false`},
  580. },
  581. },
  582. {
  583. input: `true,false`,
  584. want: []R{
  585. {T: json.Bool, V: true},
  586. {E: `unexpected character ,`},
  587. },
  588. },
  589. {
  590. input: `47"hello"`,
  591. want: []R{
  592. {T: json.Number, V: int32(47)},
  593. {E: `unexpected value "hello"`},
  594. },
  595. },
  596. {
  597. input: `47 "hello"`,
  598. want: []R{
  599. {T: json.Number, V: int32(47)},
  600. {E: `unexpected value "hello"`},
  601. },
  602. },
  603. {
  604. input: `true 42`,
  605. want: []R{
  606. {T: json.Bool, V: true},
  607. {E: `unexpected value 42`},
  608. },
  609. },
  610. // JSON arrays.
  611. {
  612. input: space + `[]` + space,
  613. want: []R{
  614. {T: json.StartArray},
  615. {T: json.EndArray},
  616. {T: json.EOF},
  617. },
  618. },
  619. {
  620. input: space + `[` + space + `]` + space,
  621. want: []R{
  622. {T: json.StartArray},
  623. {T: json.EndArray},
  624. {T: json.EOF},
  625. },
  626. },
  627. {
  628. input: space + `[` + space,
  629. want: []R{
  630. {T: json.StartArray},
  631. {E: `unexpected EOF`},
  632. },
  633. },
  634. {
  635. input: space + `]` + space,
  636. want: []R{{E: `unexpected character ]`}},
  637. },
  638. {
  639. input: `[null,true,false, 1e1, "hello" ]`,
  640. want: []R{
  641. {T: json.StartArray},
  642. {T: json.Null},
  643. {T: json.Bool, V: true},
  644. {T: json.Bool, V: false},
  645. {T: json.Number, V: int32(10)},
  646. {T: json.String, V: "hello"},
  647. {T: json.EndArray},
  648. {T: json.EOF},
  649. },
  650. },
  651. {
  652. input: `[` + space + `true` + space + `,` + space + `"hello"` + space + `]`,
  653. want: []R{
  654. {T: json.StartArray},
  655. {T: json.Bool, V: true},
  656. {T: json.String, V: "hello"},
  657. {T: json.EndArray},
  658. {T: json.EOF},
  659. },
  660. },
  661. {
  662. input: `[` + space + `true` + space + `,` + space + `]`,
  663. want: []R{
  664. {T: json.StartArray},
  665. {T: json.Bool, V: true},
  666. {E: `unexpected character ]`},
  667. },
  668. },
  669. {
  670. input: `[` + space + `false` + space + `]`,
  671. want: []R{
  672. {T: json.StartArray},
  673. {T: json.Bool, V: false},
  674. {T: json.EndArray},
  675. {T: json.EOF},
  676. },
  677. },
  678. {
  679. input: `[` + space + `1` + space + `0` + space + `]`,
  680. want: []R{
  681. {T: json.StartArray},
  682. {T: json.Number, V: int64(1)},
  683. {E: `unexpected value 0`},
  684. },
  685. },
  686. {
  687. input: `[null`,
  688. want: []R{
  689. {T: json.StartArray},
  690. {T: json.Null},
  691. {E: `unexpected EOF`},
  692. },
  693. },
  694. {
  695. input: `[foo]`,
  696. want: []R{
  697. {T: json.StartArray},
  698. {E: `invalid value foo`},
  699. },
  700. },
  701. {
  702. input: `[{}, "hello", [true, false], null]`,
  703. want: []R{
  704. {T: json.StartArray},
  705. {T: json.StartObject},
  706. {T: json.EndObject},
  707. {T: json.String, V: "hello"},
  708. {T: json.StartArray},
  709. {T: json.Bool, V: true},
  710. {T: json.Bool, V: false},
  711. {T: json.EndArray},
  712. {T: json.Null},
  713. {T: json.EndArray},
  714. {T: json.EOF},
  715. },
  716. },
  717. {
  718. input: `[{ ]`,
  719. want: []R{
  720. {T: json.StartArray},
  721. {T: json.StartObject},
  722. {E: `unexpected character ]`},
  723. },
  724. },
  725. {
  726. input: `[[ ]`,
  727. want: []R{
  728. {T: json.StartArray},
  729. {T: json.StartArray},
  730. {T: json.EndArray},
  731. {E: `unexpected EOF`},
  732. },
  733. },
  734. {
  735. input: `[,]`,
  736. want: []R{
  737. {T: json.StartArray},
  738. {E: `unexpected character ,`},
  739. },
  740. },
  741. {
  742. input: `[true "hello"]`,
  743. want: []R{
  744. {T: json.StartArray},
  745. {T: json.Bool, V: true},
  746. {E: `unexpected value "hello"`},
  747. },
  748. },
  749. {
  750. input: `[] null`,
  751. want: []R{
  752. {T: json.StartArray},
  753. {T: json.EndArray},
  754. {E: `unexpected value null`},
  755. },
  756. },
  757. {
  758. input: `true []`,
  759. want: []R{
  760. {T: json.Bool, V: true},
  761. {E: `unexpected character [`},
  762. },
  763. },
  764. // JSON objects.
  765. {
  766. input: space + `{}` + space,
  767. want: []R{
  768. {T: json.StartObject},
  769. {T: json.EndObject},
  770. {T: json.EOF},
  771. },
  772. },
  773. {
  774. input: space + `{` + space + `}` + space,
  775. want: []R{
  776. {T: json.StartObject},
  777. {T: json.EndObject},
  778. {T: json.EOF},
  779. },
  780. },
  781. {
  782. input: space + `{` + space,
  783. want: []R{
  784. {T: json.StartObject},
  785. {E: `unexpected EOF`},
  786. },
  787. },
  788. {
  789. input: space + `}` + space,
  790. want: []R{{E: `unexpected character }`}},
  791. },
  792. {
  793. input: `{` + space + `null` + space + `}`,
  794. want: []R{
  795. {T: json.StartObject},
  796. {E: `unexpected value null`},
  797. },
  798. },
  799. {
  800. input: `{[]}`,
  801. want: []R{
  802. {T: json.StartObject},
  803. {E: `unexpected character [`},
  804. },
  805. },
  806. {
  807. input: `{,}`,
  808. want: []R{
  809. {T: json.StartObject},
  810. {E: `unexpected character ,`},
  811. },
  812. },
  813. {
  814. input: `{"345678"}`,
  815. want: []R{
  816. {T: json.StartObject},
  817. {E: `unexpected character }, missing ":" after object name`},
  818. },
  819. },
  820. {
  821. input: `{` + space + `"hello"` + space + `:` + space + `"world"` + space + `}`,
  822. want: []R{
  823. {T: json.StartObject},
  824. {T: json.Name, V: "hello"},
  825. {T: json.String, V: "world"},
  826. {T: json.EndObject},
  827. {T: json.EOF},
  828. },
  829. },
  830. {
  831. input: `{"hello" "world"}`,
  832. want: []R{
  833. {T: json.StartObject},
  834. {E: `unexpected character ", missing ":" after object name`},
  835. },
  836. },
  837. {
  838. input: `{"hello":`,
  839. want: []R{
  840. {T: json.StartObject},
  841. {T: json.Name, V: "hello"},
  842. {E: `unexpected EOF`},
  843. },
  844. },
  845. {
  846. input: `{"hello":"world"`,
  847. want: []R{
  848. {T: json.StartObject},
  849. {T: json.Name, V: "hello"},
  850. {T: json.String, V: "world"},
  851. {E: `unexpected EOF`},
  852. },
  853. },
  854. {
  855. input: `{"hello":"world",`,
  856. want: []R{
  857. {T: json.StartObject},
  858. {T: json.Name, V: "hello"},
  859. {T: json.String, V: "world"},
  860. {E: `unexpected EOF`},
  861. },
  862. },
  863. {
  864. input: `{"34":"89",}`,
  865. want: []R{
  866. {T: json.StartObject},
  867. {T: json.Name, V: "34"},
  868. {T: json.String, V: "89"},
  869. {E: `syntax error (line 1:12): unexpected character }`},
  870. },
  871. },
  872. {
  873. input: `{
  874. "number": 123e2,
  875. "bool" : false,
  876. "object": {"string": "world"},
  877. "null" : null,
  878. "array" : [1.01, "hello", true],
  879. "string": "hello"
  880. }`,
  881. want: []R{
  882. {T: json.StartObject},
  883. {T: json.Name, V: "number"},
  884. {T: json.Number, V: int32(12300)},
  885. {T: json.Name, V: "bool"},
  886. {T: json.Bool, V: false},
  887. {T: json.Name, V: "object"},
  888. {T: json.StartObject},
  889. {T: json.Name, V: "string"},
  890. {T: json.String, V: "world"},
  891. {T: json.EndObject},
  892. {T: json.Name, V: "null"},
  893. {T: json.Null},
  894. {T: json.Name, V: "array"},
  895. {T: json.StartArray},
  896. {T: json.Number, V: float32(1.01)},
  897. {T: json.String, V: "hello"},
  898. {T: json.Bool, V: true},
  899. {T: json.EndArray},
  900. {T: json.Name, V: "string"},
  901. {T: json.String, V: "hello"},
  902. {T: json.EndObject},
  903. {T: json.EOF},
  904. },
  905. },
  906. {
  907. input: `[
  908. {"object": {"number": 47}},
  909. ["list"],
  910. null
  911. ]`,
  912. want: []R{
  913. {T: json.StartArray},
  914. {T: json.StartObject},
  915. {T: json.Name, V: "object"},
  916. {T: json.StartObject},
  917. {T: json.Name, V: "number"},
  918. {T: json.Number, V: uint32(47)},
  919. {T: json.EndObject},
  920. {T: json.EndObject},
  921. {T: json.StartArray},
  922. {T: json.String, V: "list"},
  923. {T: json.EndArray},
  924. {T: json.Null},
  925. {T: json.EndArray},
  926. {T: json.EOF},
  927. },
  928. },
  929. // Tests for line and column info.
  930. {
  931. input: `12345678 x`,
  932. want: []R{
  933. {T: json.Number, V: int64(12345678)},
  934. {E: `syntax error (line 1:10): invalid value x`},
  935. },
  936. },
  937. {
  938. input: "\ntrue\n x",
  939. want: []R{
  940. {T: json.Bool, V: true},
  941. {E: `syntax error (line 3:4): invalid value x`},
  942. },
  943. },
  944. {
  945. input: `"💩"x`,
  946. want: []R{
  947. {T: json.String, V: "💩"},
  948. {E: `syntax error (line 1:4): invalid value x`},
  949. },
  950. },
  951. {
  952. input: "\n\n[\"🔥🔥🔥\"x",
  953. want: []R{
  954. {T: json.StartArray},
  955. {T: json.String, V: "🔥🔥🔥"},
  956. {E: `syntax error (line 3:7): invalid value x`},
  957. },
  958. },
  959. {
  960. // Multi-rune emojis.
  961. input: `["👍🏻👍🏿"x`,
  962. want: []R{
  963. {T: json.StartArray},
  964. {T: json.String, V: "👍🏻👍🏿"},
  965. {E: `syntax error (line 1:8): invalid value x`},
  966. },
  967. },
  968. {
  969. input: `{
  970. "45678":-1
  971. }`,
  972. want: []R{
  973. {T: json.StartObject},
  974. {T: json.Name, V: "45678"},
  975. {T: json.Number, V: uint64(1), VE: "error (line 2:11)"},
  976. },
  977. },
  978. }
  979. for _, tc := range tests {
  980. tc := tc
  981. t.Run("", func(t *testing.T) {
  982. dec := json.NewDecoder([]byte(tc.input))
  983. for i, want := range tc.want {
  984. typ := dec.Peek()
  985. if typ != want.T {
  986. t.Errorf("input: %v\nPeek() got %v want %v", tc.input, typ, want.T)
  987. }
  988. value, err := dec.Read()
  989. if err != nil {
  990. if want.E == "" {
  991. t.Errorf("input: %v\nRead() got unexpected error: %v", tc.input, err)
  992. } else if !strings.Contains(err.Error(), want.E) {
  993. t.Errorf("input: %v\nRead() got %q, want %q", tc.input, err, want.E)
  994. }
  995. } else {
  996. if want.E != "" {
  997. t.Errorf("input: %v\nRead() got nil error, want %q", tc.input, want.E)
  998. }
  999. }
  1000. token := value.Type()
  1001. if token != want.T {
  1002. t.Errorf("input: %v\nRead() got %v, want %v", tc.input, token, want.T)
  1003. break
  1004. }
  1005. checkValue(t, value, i, want)
  1006. }
  1007. })
  1008. }
  1009. }
  1010. func checkValue(t *testing.T, value json.Value, wantIdx int, want R) {
  1011. var got interface{}
  1012. var err error
  1013. switch value.Type() {
  1014. case json.Bool:
  1015. got, err = value.Bool()
  1016. case json.String:
  1017. got = value.String()
  1018. case json.Name:
  1019. got, err = value.Name()
  1020. case json.Number:
  1021. switch want.V.(type) {
  1022. case float32:
  1023. got, err = value.Float(32)
  1024. got = float32(got.(float64))
  1025. case float64:
  1026. got, err = value.Float(64)
  1027. case int32:
  1028. got, err = value.Int(32)
  1029. got = int32(got.(int64))
  1030. case int64:
  1031. got, err = value.Int(64)
  1032. case uint32:
  1033. got, err = value.Uint(32)
  1034. got = uint32(got.(uint64))
  1035. case uint64:
  1036. got, err = value.Uint(64)
  1037. }
  1038. default:
  1039. return
  1040. }
  1041. if err != nil {
  1042. if want.VE == "" {
  1043. t.Errorf("want%d: %v got unexpected error: %v", wantIdx, value, err)
  1044. } else if !strings.Contains(err.Error(), want.VE) {
  1045. t.Errorf("want#%d: %v got %q, want %q", wantIdx, value, err, want.VE)
  1046. }
  1047. return
  1048. } else {
  1049. if want.VE != "" {
  1050. t.Errorf("want#%d: %v got nil error, want %q", wantIdx, value, want.VE)
  1051. return
  1052. }
  1053. }
  1054. if got != want.V {
  1055. t.Errorf("want#%d: %v got %v, want %v", wantIdx, value, got, want.V)
  1056. }
  1057. }
  1058. func TestClone(t *testing.T) {
  1059. input := `{"outer":{"str":"hello", "number": 123}}`
  1060. dec := json.NewDecoder([]byte(input))
  1061. // Clone at the start should produce the same reads as the original.
  1062. clone := dec.Clone()
  1063. compareDecoders(t, dec, clone)
  1064. // Advance to inner object, clone and compare again.
  1065. dec.Read() // Read StartObject.
  1066. dec.Read() // Read Name.
  1067. clone = dec.Clone()
  1068. compareDecoders(t, dec, clone)
  1069. }
  1070. func compareDecoders(t *testing.T, d1 *json.Decoder, d2 *json.Decoder) {
  1071. for {
  1072. v1, err1 := d1.Read()
  1073. v2, err2 := d2.Read()
  1074. if v1.Type() != v2.Type() {
  1075. t.Errorf("cloned decoder: got Type %v, want %v", v2.Type(), v1.Type())
  1076. }
  1077. if v1.Raw() != v2.Raw() {
  1078. t.Errorf("cloned decoder: got Raw %v, want %v", v2.Raw(), v1.Raw())
  1079. }
  1080. if err1 != err2 {
  1081. t.Errorf("cloned decoder: got error %v, want %v", err2, err1)
  1082. }
  1083. if v1.Type() == json.EOF {
  1084. break
  1085. }
  1086. }
  1087. }