PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/frameworks/base/core/tests/coretests/src/android/util/JsonReaderTest.java

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
Java | 909 lines | 843 code | 51 blank | 15 comment | 0 complexity | 8752e647af832128b9cfbda26d3c29f6 MD5 | raw file
  1. /*
  2. * Copyright (C) 2010 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package android.util;
  17. import java.io.IOException;
  18. import java.io.StringReader;
  19. import java.util.Arrays;
  20. import junit.framework.TestCase;
  21. public final class JsonReaderTest extends TestCase {
  22. private static final int READER_BUFFER_SIZE = 1024;
  23. public void testReadArray() throws IOException {
  24. JsonReader reader = new JsonReader(new StringReader("[true, true]"));
  25. reader.beginArray();
  26. assertEquals(true, reader.nextBoolean());
  27. assertEquals(true, reader.nextBoolean());
  28. reader.endArray();
  29. assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  30. }
  31. public void testReadEmptyArray() throws IOException {
  32. JsonReader reader = new JsonReader(new StringReader("[]"));
  33. reader.beginArray();
  34. assertFalse(reader.hasNext());
  35. reader.endArray();
  36. assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  37. }
  38. public void testReadObject() throws IOException {
  39. JsonReader reader = new JsonReader(new StringReader(
  40. "{\"a\": \"android\", \"b\": \"banana\"}"));
  41. reader.beginObject();
  42. assertEquals("a", reader.nextName());
  43. assertEquals("android", reader.nextString());
  44. assertEquals("b", reader.nextName());
  45. assertEquals("banana", reader.nextString());
  46. reader.endObject();
  47. assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  48. }
  49. public void testReadEmptyObject() throws IOException {
  50. JsonReader reader = new JsonReader(new StringReader("{}"));
  51. reader.beginObject();
  52. assertFalse(reader.hasNext());
  53. reader.endObject();
  54. assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  55. }
  56. public void testSkipObject() throws IOException {
  57. JsonReader reader = new JsonReader(new StringReader(
  58. "{\"a\": { \"c\": [], \"d\": [true, true, {}] }, \"b\": \"banana\"}"));
  59. reader.beginObject();
  60. assertEquals("a", reader.nextName());
  61. reader.skipValue();
  62. assertEquals("b", reader.nextName());
  63. reader.skipValue();
  64. reader.endObject();
  65. assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  66. }
  67. public void testHelloWorld() throws IOException {
  68. String json = "{\n" +
  69. " \"hello\": true,\n" +
  70. " \"foo\": [\"world\"]\n" +
  71. "}";
  72. JsonReader reader = new JsonReader(new StringReader(json));
  73. reader.beginObject();
  74. assertEquals("hello", reader.nextName());
  75. assertEquals(true, reader.nextBoolean());
  76. assertEquals("foo", reader.nextName());
  77. reader.beginArray();
  78. assertEquals("world", reader.nextString());
  79. reader.endArray();
  80. reader.endObject();
  81. assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  82. }
  83. public void testNulls() {
  84. try {
  85. new JsonReader(null);
  86. fail();
  87. } catch (NullPointerException expected) {
  88. }
  89. }
  90. public void testEmptyString() throws IOException {
  91. try {
  92. new JsonReader(new StringReader("")).beginArray();
  93. } catch (IOException expected) {
  94. }
  95. try {
  96. new JsonReader(new StringReader("")).beginObject();
  97. } catch (IOException expected) {
  98. }
  99. }
  100. public void testNoTopLevelObject() throws IOException {
  101. try {
  102. new JsonReader(new StringReader("true")).nextBoolean();
  103. } catch (IOException expected) {
  104. }
  105. }
  106. public void testCharacterUnescaping() throws IOException {
  107. String json = "[\"a\","
  108. + "\"a\\\"\","
  109. + "\"\\\"\","
  110. + "\":\","
  111. + "\",\","
  112. + "\"\\b\","
  113. + "\"\\f\","
  114. + "\"\\n\","
  115. + "\"\\r\","
  116. + "\"\\t\","
  117. + "\" \","
  118. + "\"\\\\\","
  119. + "\"{\","
  120. + "\"}\","
  121. + "\"[\","
  122. + "\"]\","
  123. + "\"\\u0000\","
  124. + "\"\\u0019\","
  125. + "\"\\u20AC\""
  126. + "]";
  127. JsonReader reader = new JsonReader(new StringReader(json));
  128. reader.beginArray();
  129. assertEquals("a", reader.nextString());
  130. assertEquals("a\"", reader.nextString());
  131. assertEquals("\"", reader.nextString());
  132. assertEquals(":", reader.nextString());
  133. assertEquals(",", reader.nextString());
  134. assertEquals("\b", reader.nextString());
  135. assertEquals("\f", reader.nextString());
  136. assertEquals("\n", reader.nextString());
  137. assertEquals("\r", reader.nextString());
  138. assertEquals("\t", reader.nextString());
  139. assertEquals(" ", reader.nextString());
  140. assertEquals("\\", reader.nextString());
  141. assertEquals("{", reader.nextString());
  142. assertEquals("}", reader.nextString());
  143. assertEquals("[", reader.nextString());
  144. assertEquals("]", reader.nextString());
  145. assertEquals("\0", reader.nextString());
  146. assertEquals("\u0019", reader.nextString());
  147. assertEquals("\u20AC", reader.nextString());
  148. reader.endArray();
  149. assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  150. }
  151. public void testIntegersWithFractionalPartSpecified() throws IOException {
  152. JsonReader reader = new JsonReader(new StringReader("[1.0,1.0,1.0]"));
  153. reader.beginArray();
  154. assertEquals(1.0, reader.nextDouble());
  155. assertEquals(1, reader.nextInt());
  156. assertEquals(1L, reader.nextLong());
  157. }
  158. public void testDoubles() throws IOException {
  159. String json = "[-0.0,"
  160. + "1.0,"
  161. + "1.7976931348623157E308,"
  162. + "4.9E-324,"
  163. + "0.0,"
  164. + "-0.5,"
  165. + "2.2250738585072014E-308,"
  166. + "3.141592653589793,"
  167. + "2.718281828459045,"
  168. + "\"1.0\","
  169. + "\"011.0\","
  170. + "\"NaN\","
  171. + "\"Infinity\","
  172. + "\"-Infinity\""
  173. + "]";
  174. JsonReader reader = new JsonReader(new StringReader(json));
  175. reader.beginArray();
  176. assertEquals(-0.0, reader.nextDouble());
  177. assertEquals(1.0, reader.nextDouble());
  178. assertEquals(1.7976931348623157E308, reader.nextDouble());
  179. assertEquals(4.9E-324, reader.nextDouble());
  180. assertEquals(0.0, reader.nextDouble());
  181. assertEquals(-0.5, reader.nextDouble());
  182. assertEquals(2.2250738585072014E-308, reader.nextDouble());
  183. assertEquals(3.141592653589793, reader.nextDouble());
  184. assertEquals(2.718281828459045, reader.nextDouble());
  185. assertEquals(1,0, reader.nextDouble());
  186. assertEquals(11.0, reader.nextDouble());
  187. assertTrue(Double.isNaN(reader.nextDouble()));
  188. assertEquals(Double.POSITIVE_INFINITY, reader.nextDouble());
  189. assertEquals(Double.NEGATIVE_INFINITY, reader.nextDouble());
  190. reader.endArray();
  191. assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  192. }
  193. public void testLenientDoubles() throws IOException {
  194. String json = "["
  195. + "011.0,"
  196. + "NaN,"
  197. + "NAN,"
  198. + "Infinity,"
  199. + "INFINITY,"
  200. + "-Infinity"
  201. + "]";
  202. JsonReader reader = new JsonReader(new StringReader(json));
  203. reader.setLenient(true);
  204. reader.beginArray();
  205. assertEquals(11.0, reader.nextDouble());
  206. assertTrue(Double.isNaN(reader.nextDouble()));
  207. try {
  208. reader.nextDouble();
  209. fail();
  210. } catch (NumberFormatException expected) {
  211. }
  212. assertEquals("NAN", reader.nextString());
  213. assertEquals(Double.POSITIVE_INFINITY, reader.nextDouble());
  214. try {
  215. reader.nextDouble();
  216. fail();
  217. } catch (NumberFormatException expected) {
  218. }
  219. assertEquals("INFINITY", reader.nextString());
  220. assertEquals(Double.NEGATIVE_INFINITY, reader.nextDouble());
  221. reader.endArray();
  222. assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  223. }
  224. public void testBufferBoundary() throws IOException {
  225. char[] pad = new char[READER_BUFFER_SIZE - 8];
  226. Arrays.fill(pad, '5');
  227. String json = "[\"" + new String(pad) + "\",33333]";
  228. JsonReader reader = new JsonReader(new StringReader(json));
  229. reader.beginArray();
  230. assertEquals(JsonToken.STRING, reader.peek());
  231. assertEquals(new String(pad), reader.nextString());
  232. assertEquals(JsonToken.NUMBER, reader.peek());
  233. assertEquals(33333, reader.nextInt());
  234. }
  235. public void testTruncatedBufferBoundary() throws IOException {
  236. char[] pad = new char[READER_BUFFER_SIZE - 8];
  237. Arrays.fill(pad, '5');
  238. String json = "[\"" + new String(pad) + "\",33333";
  239. JsonReader reader = new JsonReader(new StringReader(json));
  240. reader.setLenient(true);
  241. reader.beginArray();
  242. assertEquals(JsonToken.STRING, reader.peek());
  243. assertEquals(new String(pad), reader.nextString());
  244. assertEquals(JsonToken.NUMBER, reader.peek());
  245. assertEquals(33333, reader.nextInt());
  246. try {
  247. reader.endArray();
  248. fail();
  249. } catch (IOException e) {
  250. }
  251. }
  252. public void testLongestSupportedNumericLiterals() throws IOException {
  253. testLongNumericLiterals(READER_BUFFER_SIZE - 1, JsonToken.NUMBER);
  254. }
  255. public void testLongerNumericLiterals() throws IOException {
  256. testLongNumericLiterals(READER_BUFFER_SIZE, JsonToken.STRING);
  257. }
  258. private void testLongNumericLiterals(int length, JsonToken expectedToken) throws IOException {
  259. char[] longNumber = new char[length];
  260. Arrays.fill(longNumber, '9');
  261. longNumber[0] = '1';
  262. longNumber[1] = '.';
  263. String json = "[" + new String(longNumber) + "]";
  264. JsonReader reader = new JsonReader(new StringReader(json));
  265. reader.setLenient(true);
  266. reader.beginArray();
  267. assertEquals(expectedToken, reader.peek());
  268. assertEquals(2.0d, reader.nextDouble());
  269. reader.endArray();
  270. }
  271. public void testLongs() throws IOException {
  272. String json = "[0,0,0,"
  273. + "1,1,1,"
  274. + "-1,-1,-1,"
  275. + "-9223372036854775808,"
  276. + "9223372036854775807,"
  277. + "5.0,"
  278. + "1.0e2,"
  279. + "\"011\","
  280. + "\"5.0\","
  281. + "\"1.0e2\""
  282. + "]";
  283. JsonReader reader = new JsonReader(new StringReader(json));
  284. reader.beginArray();
  285. assertEquals(0L, reader.nextLong());
  286. assertEquals(0, reader.nextInt());
  287. assertEquals(0.0, reader.nextDouble());
  288. assertEquals(1L, reader.nextLong());
  289. assertEquals(1, reader.nextInt());
  290. assertEquals(1.0, reader.nextDouble());
  291. assertEquals(-1L, reader.nextLong());
  292. assertEquals(-1, reader.nextInt());
  293. assertEquals(-1.0, reader.nextDouble());
  294. try {
  295. reader.nextInt();
  296. fail();
  297. } catch (NumberFormatException expected) {
  298. }
  299. assertEquals(Long.MIN_VALUE, reader.nextLong());
  300. try {
  301. reader.nextInt();
  302. fail();
  303. } catch (NumberFormatException expected) {
  304. }
  305. assertEquals(Long.MAX_VALUE, reader.nextLong());
  306. assertEquals(5, reader.nextLong());
  307. assertEquals(100, reader.nextLong());
  308. assertEquals(11, reader.nextLong());
  309. assertEquals(5, reader.nextLong());
  310. assertEquals(100, reader.nextLong());
  311. reader.endArray();
  312. assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  313. }
  314. /**
  315. * This test fails because there's no double for 9223372036854775806, and
  316. * our long parsing uses Double.parseDouble() for fractional values.
  317. */
  318. public void testHighPrecisionLong() throws IOException {
  319. String json = "[9223372036854775806.000]";
  320. JsonReader reader = new JsonReader(new StringReader(json));
  321. reader.beginArray();
  322. assertEquals(9223372036854775806L, reader.nextLong());
  323. reader.endArray();
  324. }
  325. public void testMatchingValidNumbers() throws IOException {
  326. String json = "[-1,99,-0,0,0e1,0e+1,0e-1,0E1,0E+1,0E-1,0.0,1.0,-1.0,1.0e0,1.0e+1,1.0e-1]";
  327. JsonReader reader = new JsonReader(new StringReader(json));
  328. reader.beginArray();
  329. for (int i = 0; i < 16; i++) {
  330. assertEquals(JsonToken.NUMBER, reader.peek());
  331. reader.nextDouble();
  332. }
  333. reader.endArray();
  334. }
  335. public void testRecognizingInvalidNumbers() throws IOException {
  336. String json = "[-00,00,001,+1,1f,0x,0xf,0x0,0f1,0ee1,1..0,1e0.1,1.-01,1.+1,1.0x,1.0+]";
  337. JsonReader reader = new JsonReader(new StringReader(json));
  338. reader.setLenient(true);
  339. reader.beginArray();
  340. for (int i = 0; i < 16; i++) {
  341. assertEquals(JsonToken.STRING, reader.peek());
  342. reader.nextString();
  343. }
  344. reader.endArray();
  345. }
  346. public void testNonFiniteDouble() throws IOException {
  347. String json = "[NaN]";
  348. JsonReader reader = new JsonReader(new StringReader(json));
  349. reader.beginArray();
  350. try {
  351. reader.nextDouble();
  352. fail();
  353. } catch (IOException expected) {
  354. }
  355. }
  356. public void testNumberWithHexPrefix() throws IOException {
  357. String json = "[0x11]";
  358. JsonReader reader = new JsonReader(new StringReader(json));
  359. reader.beginArray();
  360. try {
  361. reader.nextLong();
  362. fail();
  363. } catch (IOException expected) {
  364. }
  365. }
  366. public void testNumberWithOctalPrefix() throws IOException {
  367. String json = "[01]";
  368. JsonReader reader = new JsonReader(new StringReader(json));
  369. reader.beginArray();
  370. try {
  371. reader.nextInt();
  372. fail();
  373. } catch (IOException expected) {
  374. }
  375. }
  376. public void testBooleans() throws IOException {
  377. JsonReader reader = new JsonReader(new StringReader("[true,false]"));
  378. reader.beginArray();
  379. assertEquals(true, reader.nextBoolean());
  380. assertEquals(false, reader.nextBoolean());
  381. reader.endArray();
  382. assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  383. }
  384. public void testMixedCaseLiterals() throws IOException {
  385. JsonReader reader = new JsonReader(new StringReader("[True,TruE,False,FALSE,NULL,nulL]"));
  386. reader.beginArray();
  387. assertEquals(true, reader.nextBoolean());
  388. assertEquals(true, reader.nextBoolean());
  389. assertEquals(false, reader.nextBoolean());
  390. assertEquals(false, reader.nextBoolean());
  391. reader.nextNull();
  392. reader.nextNull();
  393. reader.endArray();
  394. assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  395. }
  396. public void testMissingValue() throws IOException {
  397. JsonReader reader = new JsonReader(new StringReader("{\"a\":}"));
  398. reader.beginObject();
  399. assertEquals("a", reader.nextName());
  400. try {
  401. reader.nextString();
  402. fail();
  403. } catch (IOException expected) {
  404. }
  405. }
  406. public void testPrematureEndOfInput() throws IOException {
  407. JsonReader reader = new JsonReader(new StringReader("{\"a\":true,"));
  408. reader.beginObject();
  409. assertEquals("a", reader.nextName());
  410. assertEquals(true, reader.nextBoolean());
  411. try {
  412. reader.nextName();
  413. fail();
  414. } catch (IOException expected) {
  415. }
  416. }
  417. public void testPrematurelyClosed() throws IOException {
  418. try {
  419. JsonReader reader = new JsonReader(new StringReader("{\"a\":[]}"));
  420. reader.beginObject();
  421. reader.close();
  422. reader.nextName();
  423. fail();
  424. } catch (IllegalStateException expected) {
  425. }
  426. try {
  427. JsonReader reader = new JsonReader(new StringReader("{\"a\":[]}"));
  428. reader.close();
  429. reader.beginObject();
  430. fail();
  431. } catch (IllegalStateException expected) {
  432. }
  433. try {
  434. JsonReader reader = new JsonReader(new StringReader("{\"a\":true}"));
  435. reader.beginObject();
  436. reader.nextName();
  437. reader.peek();
  438. reader.close();
  439. reader.nextBoolean();
  440. fail();
  441. } catch (IllegalStateException expected) {
  442. }
  443. }
  444. public void testNextFailuresDoNotAdvance() throws IOException {
  445. JsonReader reader = new JsonReader(new StringReader("{\"a\":true}"));
  446. reader.beginObject();
  447. try {
  448. reader.nextString();
  449. fail();
  450. } catch (IllegalStateException expected) {
  451. }
  452. assertEquals("a", reader.nextName());
  453. try {
  454. reader.nextName();
  455. fail();
  456. } catch (IllegalStateException expected) {
  457. }
  458. try {
  459. reader.beginArray();
  460. fail();
  461. } catch (IllegalStateException expected) {
  462. }
  463. try {
  464. reader.endArray();
  465. fail();
  466. } catch (IllegalStateException expected) {
  467. }
  468. try {
  469. reader.beginObject();
  470. fail();
  471. } catch (IllegalStateException expected) {
  472. }
  473. try {
  474. reader.endObject();
  475. fail();
  476. } catch (IllegalStateException expected) {
  477. }
  478. assertEquals(true, reader.nextBoolean());
  479. try {
  480. reader.nextString();
  481. fail();
  482. } catch (IllegalStateException expected) {
  483. }
  484. try {
  485. reader.nextName();
  486. fail();
  487. } catch (IllegalStateException expected) {
  488. }
  489. try {
  490. reader.beginArray();
  491. fail();
  492. } catch (IllegalStateException expected) {
  493. }
  494. try {
  495. reader.endArray();
  496. fail();
  497. } catch (IllegalStateException expected) {
  498. }
  499. reader.endObject();
  500. assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  501. reader.close();
  502. }
  503. public void testStringNullIsNotNull() throws IOException {
  504. JsonReader reader = new JsonReader(new StringReader("[\"null\"]"));
  505. reader.beginArray();
  506. try {
  507. reader.nextNull();
  508. fail();
  509. } catch (IllegalStateException expected) {
  510. }
  511. }
  512. public void testNullLiteralIsNotAString() throws IOException {
  513. JsonReader reader = new JsonReader(new StringReader("[null]"));
  514. reader.beginArray();
  515. try {
  516. reader.nextString();
  517. fail();
  518. } catch (IllegalStateException expected) {
  519. }
  520. }
  521. public void testStrictNameValueSeparator() throws IOException {
  522. JsonReader reader = new JsonReader(new StringReader("{\"a\"=true}"));
  523. reader.beginObject();
  524. assertEquals("a", reader.nextName());
  525. try {
  526. reader.nextBoolean();
  527. fail();
  528. } catch (IOException expected) {
  529. }
  530. reader = new JsonReader(new StringReader("{\"a\"=>true}"));
  531. reader.beginObject();
  532. assertEquals("a", reader.nextName());
  533. try {
  534. reader.nextBoolean();
  535. fail();
  536. } catch (IOException expected) {
  537. }
  538. }
  539. public void testLenientNameValueSeparator() throws IOException {
  540. JsonReader reader = new JsonReader(new StringReader("{\"a\"=true}"));
  541. reader.setLenient(true);
  542. reader.beginObject();
  543. assertEquals("a", reader.nextName());
  544. assertEquals(true, reader.nextBoolean());
  545. reader = new JsonReader(new StringReader("{\"a\"=>true}"));
  546. reader.setLenient(true);
  547. reader.beginObject();
  548. assertEquals("a", reader.nextName());
  549. assertEquals(true, reader.nextBoolean());
  550. }
  551. public void testStrictComments() throws IOException {
  552. JsonReader reader = new JsonReader(new StringReader("[// comment \n true]"));
  553. reader.beginArray();
  554. try {
  555. reader.nextBoolean();
  556. fail();
  557. } catch (IOException expected) {
  558. }
  559. reader = new JsonReader(new StringReader("[# comment \n true]"));
  560. reader.beginArray();
  561. try {
  562. reader.nextBoolean();
  563. fail();
  564. } catch (IOException expected) {
  565. }
  566. reader = new JsonReader(new StringReader("[/* comment */ true]"));
  567. reader.beginArray();
  568. try {
  569. reader.nextBoolean();
  570. fail();
  571. } catch (IOException expected) {
  572. }
  573. }
  574. public void testLenientComments() throws IOException {
  575. JsonReader reader = new JsonReader(new StringReader("[// comment \n true]"));
  576. reader.setLenient(true);
  577. reader.beginArray();
  578. assertEquals(true, reader.nextBoolean());
  579. reader = new JsonReader(new StringReader("[# comment \n true]"));
  580. reader.setLenient(true);
  581. reader.beginArray();
  582. assertEquals(true, reader.nextBoolean());
  583. reader = new JsonReader(new StringReader("[/* comment */ true]"));
  584. reader.setLenient(true);
  585. reader.beginArray();
  586. assertEquals(true, reader.nextBoolean());
  587. }
  588. public void testStrictUnquotedNames() throws IOException {
  589. JsonReader reader = new JsonReader(new StringReader("{a:true}"));
  590. reader.beginObject();
  591. try {
  592. reader.nextName();
  593. fail();
  594. } catch (IOException expected) {
  595. }
  596. }
  597. public void testLenientUnquotedNames() throws IOException {
  598. JsonReader reader = new JsonReader(new StringReader("{a:true}"));
  599. reader.setLenient(true);
  600. reader.beginObject();
  601. assertEquals("a", reader.nextName());
  602. }
  603. public void testStrictSingleQuotedNames() throws IOException {
  604. JsonReader reader = new JsonReader(new StringReader("{'a':true}"));
  605. reader.beginObject();
  606. try {
  607. reader.nextName();
  608. fail();
  609. } catch (IOException expected) {
  610. }
  611. }
  612. public void testLenientSingleQuotedNames() throws IOException {
  613. JsonReader reader = new JsonReader(new StringReader("{'a':true}"));
  614. reader.setLenient(true);
  615. reader.beginObject();
  616. assertEquals("a", reader.nextName());
  617. }
  618. public void testStrictUnquotedStrings() throws IOException {
  619. JsonReader reader = new JsonReader(new StringReader("[a]"));
  620. reader.beginArray();
  621. try {
  622. reader.nextString();
  623. fail();
  624. } catch (MalformedJsonException expected) {
  625. }
  626. }
  627. public void testLenientUnquotedStrings() throws IOException {
  628. JsonReader reader = new JsonReader(new StringReader("[a]"));
  629. reader.setLenient(true);
  630. reader.beginArray();
  631. assertEquals("a", reader.nextString());
  632. }
  633. public void testStrictSingleQuotedStrings() throws IOException {
  634. JsonReader reader = new JsonReader(new StringReader("['a']"));
  635. reader.beginArray();
  636. try {
  637. reader.nextString();
  638. fail();
  639. } catch (IOException expected) {
  640. }
  641. }
  642. public void testLenientSingleQuotedStrings() throws IOException {
  643. JsonReader reader = new JsonReader(new StringReader("['a']"));
  644. reader.setLenient(true);
  645. reader.beginArray();
  646. assertEquals("a", reader.nextString());
  647. }
  648. public void testStrictSemicolonDelimitedArray() throws IOException {
  649. JsonReader reader = new JsonReader(new StringReader("[true;true]"));
  650. reader.beginArray();
  651. try {
  652. reader.nextBoolean();
  653. reader.nextBoolean();
  654. fail();
  655. } catch (IOException expected) {
  656. }
  657. }
  658. public void testLenientSemicolonDelimitedArray() throws IOException {
  659. JsonReader reader = new JsonReader(new StringReader("[true;true]"));
  660. reader.setLenient(true);
  661. reader.beginArray();
  662. assertEquals(true, reader.nextBoolean());
  663. assertEquals(true, reader.nextBoolean());
  664. }
  665. public void testStrictSemicolonDelimitedNameValuePair() throws IOException {
  666. JsonReader reader = new JsonReader(new StringReader("{\"a\":true;\"b\":true}"));
  667. reader.beginObject();
  668. assertEquals("a", reader.nextName());
  669. try {
  670. reader.nextBoolean();
  671. reader.nextName();
  672. fail();
  673. } catch (IOException expected) {
  674. }
  675. }
  676. public void testLenientSemicolonDelimitedNameValuePair() throws IOException {
  677. JsonReader reader = new JsonReader(new StringReader("{\"a\":true;\"b\":true}"));
  678. reader.setLenient(true);
  679. reader.beginObject();
  680. assertEquals("a", reader.nextName());
  681. assertEquals(true, reader.nextBoolean());
  682. assertEquals("b", reader.nextName());
  683. }
  684. public void testStrictUnnecessaryArraySeparators() throws IOException {
  685. JsonReader reader = new JsonReader(new StringReader("[true,,true]"));
  686. reader.beginArray();
  687. assertEquals(true, reader.nextBoolean());
  688. try {
  689. reader.nextNull();
  690. fail();
  691. } catch (IOException expected) {
  692. }
  693. reader = new JsonReader(new StringReader("[,true]"));
  694. reader.beginArray();
  695. try {
  696. reader.nextNull();
  697. fail();
  698. } catch (IOException expected) {
  699. }
  700. reader = new JsonReader(new StringReader("[true,]"));
  701. reader.beginArray();
  702. assertEquals(true, reader.nextBoolean());
  703. try {
  704. reader.nextNull();
  705. fail();
  706. } catch (IOException expected) {
  707. }
  708. reader = new JsonReader(new StringReader("[,]"));
  709. reader.beginArray();
  710. try {
  711. reader.nextNull();
  712. fail();
  713. } catch (IOException expected) {
  714. }
  715. }
  716. public void testLenientUnnecessaryArraySeparators() throws IOException {
  717. JsonReader reader = new JsonReader(new StringReader("[true,,true]"));
  718. reader.setLenient(true);
  719. reader.beginArray();
  720. assertEquals(true, reader.nextBoolean());
  721. reader.nextNull();
  722. assertEquals(true, reader.nextBoolean());
  723. reader.endArray();
  724. reader = new JsonReader(new StringReader("[,true]"));
  725. reader.setLenient(true);
  726. reader.beginArray();
  727. reader.nextNull();
  728. assertEquals(true, reader.nextBoolean());
  729. reader.endArray();
  730. reader = new JsonReader(new StringReader("[true,]"));
  731. reader.setLenient(true);
  732. reader.beginArray();
  733. assertEquals(true, reader.nextBoolean());
  734. reader.nextNull();
  735. reader.endArray();
  736. reader = new JsonReader(new StringReader("[,]"));
  737. reader.setLenient(true);
  738. reader.beginArray();
  739. reader.nextNull();
  740. reader.nextNull();
  741. reader.endArray();
  742. }
  743. public void testStrictMultipleTopLevelValues() throws IOException {
  744. JsonReader reader = new JsonReader(new StringReader("[] []"));
  745. reader.beginArray();
  746. reader.endArray();
  747. try {
  748. reader.peek();
  749. fail();
  750. } catch (IOException expected) {
  751. }
  752. }
  753. public void testLenientMultipleTopLevelValues() throws IOException {
  754. JsonReader reader = new JsonReader(new StringReader("[] true {}"));
  755. reader.setLenient(true);
  756. reader.beginArray();
  757. reader.endArray();
  758. assertEquals(true, reader.nextBoolean());
  759. reader.beginObject();
  760. reader.endObject();
  761. assertEquals(JsonToken.END_DOCUMENT, reader.peek());
  762. }
  763. public void testStrictTopLevelValueType() {
  764. JsonReader reader = new JsonReader(new StringReader("true"));
  765. try {
  766. reader.nextBoolean();
  767. fail();
  768. } catch (IOException expected) {
  769. }
  770. }
  771. public void testLenientTopLevelValueType() throws IOException {
  772. JsonReader reader = new JsonReader(new StringReader("true"));
  773. reader.setLenient(true);
  774. assertEquals(true, reader.nextBoolean());
  775. }
  776. public void testStrictNonExecutePrefix() {
  777. JsonReader reader = new JsonReader(new StringReader(")]}'\n []"));
  778. try {
  779. reader.beginArray();
  780. fail();
  781. } catch (IOException expected) {
  782. }
  783. }
  784. public void testBomIgnoredAsFirstCharacterOfDocument() throws IOException {
  785. JsonReader reader = new JsonReader(new StringReader("\ufeff[]"));
  786. reader.beginArray();
  787. reader.endArray();
  788. }
  789. public void testBomForbiddenAsOtherCharacterInDocument() throws IOException {
  790. JsonReader reader = new JsonReader(new StringReader("[\ufeff]"));
  791. reader.beginArray();
  792. try {
  793. reader.endArray();
  794. fail();
  795. } catch (IOException expected) {
  796. }
  797. }
  798. public void testFailWithPosition() throws IOException {
  799. testFailWithPosition("Expected literal value at line 6 column 3",
  800. "[\n\n\n\n\n0,}]");
  801. }
  802. public void testFailWithPositionIsOffsetByBom() throws IOException {
  803. testFailWithPosition("Expected literal value at line 1 column 4",
  804. "\ufeff[0,}]");
  805. }
  806. public void testFailWithPositionGreaterThanBufferSize() throws IOException {
  807. String spaces = repeat(' ', 8192);
  808. testFailWithPosition("Expected literal value at line 6 column 3",
  809. "[\n\n" + spaces + "\n\n\n0,}]");
  810. }
  811. private void testFailWithPosition(String message, String json) throws IOException {
  812. JsonReader reader = new JsonReader(new StringReader(json));
  813. reader.beginArray();
  814. reader.nextInt();
  815. try {
  816. reader.peek();
  817. fail();
  818. } catch (IOException expected) {
  819. assertEquals(message, expected.getMessage());
  820. }
  821. }
  822. private String repeat(char c, int count) {
  823. char[] array = new char[count];
  824. Arrays.fill(array, c);
  825. return new String(array);
  826. }
  827. }