Jesse Wilson | 76d7e20 | 2010-08-03 17:55:09 -0700 | [diff] [blame] | 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 | |
| 17 | package android.util; |
| 18 | |
| 19 | import junit.framework.TestCase; |
| 20 | |
| 21 | import java.io.IOException; |
| 22 | import java.io.StringReader; |
| 23 | |
| 24 | public final class JsonReaderTest extends TestCase { |
| 25 | |
| 26 | public void testReadArray() throws IOException { |
| 27 | JsonReader reader = new JsonReader(new StringReader("[true, true]")); |
| 28 | reader.beginArray(); |
| 29 | assertEquals(true, reader.nextBoolean()); |
| 30 | assertEquals(true, reader.nextBoolean()); |
| 31 | reader.endArray(); |
| 32 | assertEquals(JsonToken.END_DOCUMENT, reader.peek()); |
| 33 | } |
| 34 | |
Jesse Wilson | 1ba4171 | 2010-08-06 16:08:59 -0700 | [diff] [blame] | 35 | public void testReadEmptyArray() throws IOException { |
| 36 | JsonReader reader = new JsonReader(new StringReader("[]")); |
| 37 | reader.beginArray(); |
| 38 | assertFalse(reader.hasNext()); |
| 39 | reader.endArray(); |
| 40 | assertEquals(JsonToken.END_DOCUMENT, reader.peek()); |
| 41 | } |
| 42 | |
Jesse Wilson | 76d7e20 | 2010-08-03 17:55:09 -0700 | [diff] [blame] | 43 | public void testReadObject() throws IOException { |
| 44 | JsonReader reader = new JsonReader(new StringReader( |
| 45 | "{\"a\": \"android\", \"b\": \"banana\"}")); |
| 46 | reader.beginObject(); |
| 47 | assertEquals("a", reader.nextName()); |
| 48 | assertEquals("android", reader.nextString()); |
| 49 | assertEquals("b", reader.nextName()); |
| 50 | assertEquals("banana", reader.nextString()); |
| 51 | reader.endObject(); |
| 52 | assertEquals(JsonToken.END_DOCUMENT, reader.peek()); |
| 53 | } |
| 54 | |
Jesse Wilson | 1ba4171 | 2010-08-06 16:08:59 -0700 | [diff] [blame] | 55 | public void testReadEmptyObject() throws IOException { |
| 56 | JsonReader reader = new JsonReader(new StringReader("{}")); |
| 57 | reader.beginObject(); |
| 58 | assertFalse(reader.hasNext()); |
| 59 | reader.endObject(); |
| 60 | assertEquals(JsonToken.END_DOCUMENT, reader.peek()); |
| 61 | } |
| 62 | |
Jesse Wilson | 76d7e20 | 2010-08-03 17:55:09 -0700 | [diff] [blame] | 63 | public void testSkipObject() throws IOException { |
| 64 | JsonReader reader = new JsonReader(new StringReader( |
| 65 | "{\"a\": { \"c\": [], \"d\": [true, true, {}] }, \"b\": \"banana\"}")); |
| 66 | reader.beginObject(); |
| 67 | assertEquals("a", reader.nextName()); |
| 68 | reader.skipValue(); |
| 69 | assertEquals("b", reader.nextName()); |
| 70 | reader.skipValue(); |
| 71 | reader.endObject(); |
| 72 | assertEquals(JsonToken.END_DOCUMENT, reader.peek()); |
| 73 | } |
| 74 | |
| 75 | public void testHelloWorld() throws IOException { |
| 76 | String json = "{\n" + |
| 77 | " \"hello\": true,\n" + |
| 78 | " \"foo\": [\"world\"]\n" + |
| 79 | "}"; |
| 80 | JsonReader reader = new JsonReader(new StringReader(json)); |
| 81 | reader.beginObject(); |
| 82 | assertEquals("hello", reader.nextName()); |
| 83 | assertEquals(true, reader.nextBoolean()); |
| 84 | assertEquals("foo", reader.nextName()); |
| 85 | reader.beginArray(); |
| 86 | assertEquals("world", reader.nextString()); |
| 87 | reader.endArray(); |
| 88 | reader.endObject(); |
| 89 | assertEquals(JsonToken.END_DOCUMENT, reader.peek()); |
| 90 | } |
| 91 | |
| 92 | public void testNulls() { |
| 93 | try { |
| 94 | new JsonReader(null); |
| 95 | fail(); |
| 96 | } catch (NullPointerException expected) { |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public void testEmptyString() throws IOException { |
| 101 | try { |
| 102 | new JsonReader(new StringReader("")).beginArray(); |
| 103 | } catch (IOException expected) { |
| 104 | } |
| 105 | try { |
| 106 | new JsonReader(new StringReader("")).beginObject(); |
| 107 | } catch (IOException expected) { |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public void testNoTopLevelObject() throws IOException { |
| 112 | try { |
| 113 | new JsonReader(new StringReader("true")).nextBoolean(); |
| 114 | } catch (IOException expected) { |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | public void testCharacterUnescaping() throws IOException { |
| 119 | String json = "[\"a\"," |
| 120 | + "\"a\\\"\"," |
| 121 | + "\"\\\"\"," |
| 122 | + "\":\"," |
| 123 | + "\",\"," |
| 124 | + "\"\\b\"," |
| 125 | + "\"\\f\"," |
| 126 | + "\"\\n\"," |
| 127 | + "\"\\r\"," |
| 128 | + "\"\\t\"," |
| 129 | + "\" \"," |
| 130 | + "\"\\\\\"," |
| 131 | + "\"{\"," |
| 132 | + "\"}\"," |
| 133 | + "\"[\"," |
| 134 | + "\"]\"," |
| 135 | + "\"\\u0000\"," |
| 136 | + "\"\\u0019\"," |
| 137 | + "\"\\u20AC\"" |
| 138 | + "]"; |
| 139 | JsonReader reader = new JsonReader(new StringReader(json)); |
| 140 | reader.beginArray(); |
| 141 | assertEquals("a", reader.nextString()); |
| 142 | assertEquals("a\"", reader.nextString()); |
| 143 | assertEquals("\"", reader.nextString()); |
| 144 | assertEquals(":", reader.nextString()); |
| 145 | assertEquals(",", reader.nextString()); |
| 146 | assertEquals("\b", reader.nextString()); |
| 147 | assertEquals("\f", reader.nextString()); |
| 148 | assertEquals("\n", reader.nextString()); |
| 149 | assertEquals("\r", reader.nextString()); |
| 150 | assertEquals("\t", reader.nextString()); |
| 151 | assertEquals(" ", reader.nextString()); |
| 152 | assertEquals("\\", reader.nextString()); |
| 153 | assertEquals("{", reader.nextString()); |
| 154 | assertEquals("}", reader.nextString()); |
| 155 | assertEquals("[", reader.nextString()); |
| 156 | assertEquals("]", reader.nextString()); |
| 157 | assertEquals("\0", reader.nextString()); |
| 158 | assertEquals("\u0019", reader.nextString()); |
| 159 | assertEquals("\u20AC", reader.nextString()); |
| 160 | reader.endArray(); |
| 161 | assertEquals(JsonToken.END_DOCUMENT, reader.peek()); |
| 162 | } |
| 163 | |
| 164 | public void testIntegersWithFractionalPartSpecified() throws IOException { |
| 165 | JsonReader reader = new JsonReader(new StringReader("[1.0,1.0,1.0]")); |
| 166 | reader.beginArray(); |
| 167 | assertEquals(1.0, reader.nextDouble()); |
| 168 | assertEquals(1, reader.nextInt()); |
| 169 | assertEquals(1L, reader.nextLong()); |
| 170 | } |
| 171 | |
| 172 | public void testDoubles() throws IOException { |
| 173 | String json = "[-0.0," |
| 174 | + "1.0," |
| 175 | + "1.7976931348623157E308," |
| 176 | + "4.9E-324," |
| 177 | + "0.0," |
| 178 | + "-0.5," |
| 179 | + "2.2250738585072014E-308," |
| 180 | + "3.141592653589793," |
| 181 | + "2.718281828459045]"; |
| 182 | JsonReader reader = new JsonReader(new StringReader(json)); |
| 183 | reader.beginArray(); |
| 184 | assertEquals(-0.0, reader.nextDouble()); |
| 185 | assertEquals(1.0, reader.nextDouble()); |
| 186 | assertEquals(1.7976931348623157E308, reader.nextDouble()); |
| 187 | assertEquals(4.9E-324, reader.nextDouble()); |
| 188 | assertEquals(0.0, reader.nextDouble()); |
| 189 | assertEquals(-0.5, reader.nextDouble()); |
| 190 | assertEquals(2.2250738585072014E-308, reader.nextDouble()); |
| 191 | assertEquals(3.141592653589793, reader.nextDouble()); |
| 192 | assertEquals(2.718281828459045, reader.nextDouble()); |
| 193 | reader.endArray(); |
| 194 | assertEquals(JsonToken.END_DOCUMENT, reader.peek()); |
| 195 | } |
| 196 | |
| 197 | public void testNonFiniteDoubles() throws IOException { |
| 198 | String json = "[NaN]"; |
| 199 | JsonReader reader = new JsonReader(new StringReader(json)); |
| 200 | reader.beginArray(); |
| 201 | try { |
| 202 | reader.nextDouble(); |
| 203 | fail(); |
| 204 | } catch (NumberFormatException expected) { |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | public void testLongs() throws IOException { |
| 209 | String json = "[0,0,0," |
| 210 | + "1,1,1," |
| 211 | + "-1,-1,-1," |
| 212 | + "-9223372036854775808," |
| 213 | + "9223372036854775807]"; |
| 214 | JsonReader reader = new JsonReader(new StringReader(json)); |
| 215 | reader.beginArray(); |
| 216 | assertEquals(0L, reader.nextLong()); |
| 217 | assertEquals(0, reader.nextInt()); |
| 218 | assertEquals(0.0, reader.nextDouble()); |
| 219 | assertEquals(1L, reader.nextLong()); |
| 220 | assertEquals(1, reader.nextInt()); |
| 221 | assertEquals(1.0, reader.nextDouble()); |
| 222 | assertEquals(-1L, reader.nextLong()); |
| 223 | assertEquals(-1, reader.nextInt()); |
| 224 | assertEquals(-1.0, reader.nextDouble()); |
| 225 | try { |
| 226 | reader.nextInt(); |
| 227 | fail(); |
| 228 | } catch (NumberFormatException expected) { |
| 229 | } |
| 230 | assertEquals(Long.MIN_VALUE, reader.nextLong()); |
| 231 | try { |
| 232 | reader.nextInt(); |
| 233 | fail(); |
| 234 | } catch (NumberFormatException expected) { |
| 235 | } |
| 236 | assertEquals(Long.MAX_VALUE, reader.nextLong()); |
| 237 | reader.endArray(); |
| 238 | assertEquals(JsonToken.END_DOCUMENT, reader.peek()); |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * This test fails because there's no double for 9223372036854775806, and |
| 243 | * our long parsing uses Double.parseDouble() for fractional values. |
| 244 | */ |
| 245 | public void testHighPrecisionLong() throws IOException { |
| 246 | String json = "[9223372036854775806.000]"; |
| 247 | JsonReader reader = new JsonReader(new StringReader(json)); |
| 248 | reader.beginArray(); |
| 249 | assertEquals(9223372036854775806L, reader.nextLong()); |
| 250 | reader.endArray(); |
| 251 | } |
| 252 | |
| 253 | public void testNumberWithOctalPrefix() throws IOException { |
| 254 | String json = "[01]"; |
| 255 | JsonReader reader = new JsonReader(new StringReader(json)); |
| 256 | reader.beginArray(); |
| 257 | try { |
| 258 | reader.nextInt(); |
| 259 | fail(); |
| 260 | } catch (NumberFormatException expected) { |
| 261 | } |
| 262 | try { |
| 263 | reader.nextLong(); |
| 264 | fail(); |
| 265 | } catch (NumberFormatException expected) { |
| 266 | } |
| 267 | try { |
| 268 | reader.nextDouble(); |
| 269 | fail(); |
| 270 | } catch (NumberFormatException expected) { |
| 271 | } |
| 272 | assertEquals("01", reader.nextString()); |
| 273 | reader.endArray(); |
| 274 | assertEquals(JsonToken.END_DOCUMENT, reader.peek()); |
| 275 | } |
| 276 | |
| 277 | public void testBooleans() throws IOException { |
| 278 | JsonReader reader = new JsonReader(new StringReader("[true,false]")); |
| 279 | reader.beginArray(); |
| 280 | assertEquals(true, reader.nextBoolean()); |
| 281 | assertEquals(false, reader.nextBoolean()); |
| 282 | reader.endArray(); |
| 283 | assertEquals(JsonToken.END_DOCUMENT, reader.peek()); |
| 284 | } |
| 285 | |
| 286 | public void testMixedCaseLiterals() throws IOException { |
| 287 | JsonReader reader = new JsonReader(new StringReader("[True,TruE,False,FALSE,NULL,nulL]")); |
| 288 | reader.beginArray(); |
| 289 | assertEquals(true, reader.nextBoolean()); |
| 290 | assertEquals(true, reader.nextBoolean()); |
| 291 | assertEquals(false, reader.nextBoolean()); |
| 292 | assertEquals(false, reader.nextBoolean()); |
| 293 | reader.nextNull(); |
| 294 | reader.nextNull(); |
| 295 | reader.endArray(); |
| 296 | assertEquals(JsonToken.END_DOCUMENT, reader.peek()); |
| 297 | } |
| 298 | |
| 299 | public void testMissingValue() throws IOException { |
| 300 | JsonReader reader = new JsonReader(new StringReader("{\"a\":}")); |
| 301 | reader.beginObject(); |
| 302 | assertEquals("a", reader.nextName()); |
| 303 | try { |
| 304 | reader.nextString(); |
| 305 | fail(); |
| 306 | } catch (IOException expected) { |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | public void testPrematureEndOfInput() throws IOException { |
| 311 | JsonReader reader = new JsonReader(new StringReader("{\"a\":true,")); |
| 312 | reader.beginObject(); |
| 313 | assertEquals("a", reader.nextName()); |
| 314 | assertEquals(true, reader.nextBoolean()); |
| 315 | try { |
| 316 | reader.nextName(); |
| 317 | fail(); |
| 318 | } catch (IOException expected) { |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | public void testPrematurelyClosed() throws IOException { |
| 323 | try { |
| 324 | JsonReader reader = new JsonReader(new StringReader("{\"a\":[]}")); |
| 325 | reader.beginObject(); |
| 326 | reader.close(); |
| 327 | reader.nextName(); |
| 328 | fail(); |
| 329 | } catch (IllegalStateException expected) { |
| 330 | } |
| 331 | |
| 332 | try { |
| 333 | JsonReader reader = new JsonReader(new StringReader("{\"a\":[]}")); |
| 334 | reader.close(); |
| 335 | reader.beginObject(); |
| 336 | fail(); |
| 337 | } catch (IllegalStateException expected) { |
| 338 | } |
| 339 | |
| 340 | try { |
| 341 | JsonReader reader = new JsonReader(new StringReader("{\"a\":true}")); |
| 342 | reader.beginObject(); |
| 343 | reader.nextName(); |
| 344 | reader.peek(); |
| 345 | reader.close(); |
| 346 | reader.nextBoolean(); |
| 347 | fail(); |
| 348 | } catch (IllegalStateException expected) { |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | public void testNextFailuresDoNotAdvance() throws IOException { |
| 353 | JsonReader reader = new JsonReader(new StringReader("{\"a\":true}")); |
| 354 | reader.beginObject(); |
| 355 | try { |
| 356 | reader.nextString(); |
| 357 | fail(); |
| 358 | } catch (IllegalStateException expected) { |
| 359 | } |
| 360 | assertEquals("a", reader.nextName()); |
| 361 | try { |
| 362 | reader.nextName(); |
| 363 | fail(); |
| 364 | } catch (IllegalStateException expected) { |
| 365 | } |
| 366 | try { |
| 367 | reader.beginArray(); |
| 368 | fail(); |
| 369 | } catch (IllegalStateException expected) { |
| 370 | } |
| 371 | try { |
| 372 | reader.endArray(); |
| 373 | fail(); |
| 374 | } catch (IllegalStateException expected) { |
| 375 | } |
| 376 | try { |
| 377 | reader.beginObject(); |
| 378 | fail(); |
| 379 | } catch (IllegalStateException expected) { |
| 380 | } |
| 381 | try { |
| 382 | reader.endObject(); |
| 383 | fail(); |
| 384 | } catch (IllegalStateException expected) { |
| 385 | } |
| 386 | assertEquals(true, reader.nextBoolean()); |
| 387 | try { |
| 388 | reader.nextString(); |
| 389 | fail(); |
| 390 | } catch (IllegalStateException expected) { |
| 391 | } |
| 392 | try { |
| 393 | reader.nextName(); |
| 394 | fail(); |
| 395 | } catch (IllegalStateException expected) { |
| 396 | } |
| 397 | try { |
| 398 | reader.beginArray(); |
| 399 | fail(); |
| 400 | } catch (IllegalStateException expected) { |
| 401 | } |
| 402 | try { |
| 403 | reader.endArray(); |
| 404 | fail(); |
| 405 | } catch (IllegalStateException expected) { |
| 406 | } |
| 407 | reader.endObject(); |
| 408 | assertEquals(JsonToken.END_DOCUMENT, reader.peek()); |
| 409 | reader.close(); |
| 410 | } |
| 411 | |
| 412 | public void testStringNullIsNotNull() throws IOException { |
| 413 | JsonReader reader = new JsonReader(new StringReader("[\"null\"]")); |
| 414 | reader.beginArray(); |
| 415 | try { |
| 416 | reader.nextNull(); |
| 417 | fail(); |
| 418 | } catch (IllegalStateException expected) { |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | public void testNullLiteralIsNotAString() throws IOException { |
| 423 | JsonReader reader = new JsonReader(new StringReader("[null]")); |
| 424 | reader.beginArray(); |
| 425 | try { |
| 426 | reader.nextString(); |
| 427 | fail(); |
| 428 | } catch (IllegalStateException expected) { |
| 429 | } |
| 430 | } |
Jesse Wilson | 1ba4171 | 2010-08-06 16:08:59 -0700 | [diff] [blame] | 431 | |
| 432 | public void testStrictNameValueSeparator() throws IOException { |
| 433 | JsonReader reader = new JsonReader(new StringReader("{\"a\"=true}")); |
| 434 | reader.beginObject(); |
| 435 | assertEquals("a", reader.nextName()); |
| 436 | try { |
| 437 | reader.nextBoolean(); |
| 438 | fail(); |
| 439 | } catch (IOException expected) { |
| 440 | } |
| 441 | |
| 442 | reader = new JsonReader(new StringReader("{\"a\"=>true}")); |
| 443 | reader.beginObject(); |
| 444 | assertEquals("a", reader.nextName()); |
| 445 | try { |
| 446 | reader.nextBoolean(); |
| 447 | fail(); |
| 448 | } catch (IOException expected) { |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | public void testLenientNameValueSeparator() throws IOException { |
| 453 | JsonReader reader = new JsonReader(new StringReader("{\"a\"=true}")); |
| 454 | reader.setLenient(true); |
| 455 | reader.beginObject(); |
| 456 | assertEquals("a", reader.nextName()); |
| 457 | assertEquals(true, reader.nextBoolean()); |
| 458 | |
| 459 | reader = new JsonReader(new StringReader("{\"a\"=>true}")); |
| 460 | reader.setLenient(true); |
| 461 | reader.beginObject(); |
| 462 | assertEquals("a", reader.nextName()); |
| 463 | assertEquals(true, reader.nextBoolean()); |
| 464 | } |
| 465 | |
| 466 | public void testStrictComments() throws IOException { |
| 467 | JsonReader reader = new JsonReader(new StringReader("[// comment \n true]")); |
| 468 | reader.beginArray(); |
| 469 | try { |
| 470 | reader.nextBoolean(); |
| 471 | fail(); |
| 472 | } catch (IOException expected) { |
| 473 | } |
| 474 | |
| 475 | reader = new JsonReader(new StringReader("[# comment \n true]")); |
| 476 | reader.beginArray(); |
| 477 | try { |
| 478 | reader.nextBoolean(); |
| 479 | fail(); |
| 480 | } catch (IOException expected) { |
| 481 | } |
| 482 | |
| 483 | reader = new JsonReader(new StringReader("[/* comment */ true]")); |
| 484 | reader.beginArray(); |
| 485 | try { |
| 486 | reader.nextBoolean(); |
| 487 | fail(); |
| 488 | } catch (IOException expected) { |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | public void testLenientComments() throws IOException { |
| 493 | JsonReader reader = new JsonReader(new StringReader("[// comment \n true]")); |
| 494 | reader.setLenient(true); |
| 495 | reader.beginArray(); |
| 496 | assertEquals(true, reader.nextBoolean()); |
| 497 | |
| 498 | reader = new JsonReader(new StringReader("[# comment \n true]")); |
| 499 | reader.setLenient(true); |
| 500 | reader.beginArray(); |
| 501 | assertEquals(true, reader.nextBoolean()); |
| 502 | |
| 503 | reader = new JsonReader(new StringReader("[/* comment */ true]")); |
| 504 | reader.setLenient(true); |
| 505 | reader.beginArray(); |
| 506 | assertEquals(true, reader.nextBoolean()); |
| 507 | } |
| 508 | |
| 509 | public void testStrictUnquotedNames() throws IOException { |
| 510 | JsonReader reader = new JsonReader(new StringReader("{a:true}")); |
| 511 | reader.beginObject(); |
| 512 | try { |
| 513 | reader.nextName(); |
| 514 | fail(); |
| 515 | } catch (IOException expected) { |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | public void testLenientUnquotedNames() throws IOException { |
| 520 | JsonReader reader = new JsonReader(new StringReader("{a:true}")); |
| 521 | reader.setLenient(true); |
| 522 | reader.beginObject(); |
| 523 | assertEquals("a", reader.nextName()); |
| 524 | } |
| 525 | |
| 526 | public void testStrictSingleQuotedNames() throws IOException { |
| 527 | JsonReader reader = new JsonReader(new StringReader("{'a':true}")); |
| 528 | reader.beginObject(); |
| 529 | try { |
| 530 | reader.nextName(); |
| 531 | fail(); |
| 532 | } catch (IOException expected) { |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | public void testLenientSingleQuotedNames() throws IOException { |
| 537 | JsonReader reader = new JsonReader(new StringReader("{'a':true}")); |
| 538 | reader.setLenient(true); |
| 539 | reader.beginObject(); |
| 540 | assertEquals("a", reader.nextName()); |
| 541 | } |
| 542 | |
| 543 | public void testStrictUnquotedStrings() throws IOException { |
| 544 | JsonReader reader = new JsonReader(new StringReader("[a]")); |
| 545 | reader.beginArray(); |
| 546 | try { |
| 547 | reader.nextString(); |
| 548 | fail(); |
| 549 | } catch (IOException expected) { |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | public void testLenientUnquotedStrings() throws IOException { |
| 554 | JsonReader reader = new JsonReader(new StringReader("[a]")); |
| 555 | reader.setLenient(true); |
| 556 | reader.beginArray(); |
| 557 | assertEquals("a", reader.nextString()); |
| 558 | } |
| 559 | |
| 560 | public void testStrictSingleQuotedStrings() throws IOException { |
| 561 | JsonReader reader = new JsonReader(new StringReader("['a']")); |
| 562 | reader.beginArray(); |
| 563 | try { |
| 564 | reader.nextString(); |
| 565 | fail(); |
| 566 | } catch (IOException expected) { |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | public void testLenientSingleQuotedStrings() throws IOException { |
| 571 | JsonReader reader = new JsonReader(new StringReader("['a']")); |
| 572 | reader.setLenient(true); |
| 573 | reader.beginArray(); |
| 574 | assertEquals("a", reader.nextString()); |
| 575 | } |
| 576 | |
| 577 | public void testStrictSemicolonDelimitedArray() throws IOException { |
| 578 | JsonReader reader = new JsonReader(new StringReader("[true;true]")); |
| 579 | reader.beginArray(); |
| 580 | try { |
| 581 | reader.nextBoolean(); |
| 582 | reader.nextBoolean(); |
| 583 | fail(); |
| 584 | } catch (IOException expected) { |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | public void testLenientSemicolonDelimitedArray() throws IOException { |
| 589 | JsonReader reader = new JsonReader(new StringReader("[true;true]")); |
| 590 | reader.setLenient(true); |
| 591 | reader.beginArray(); |
| 592 | assertEquals(true, reader.nextBoolean()); |
| 593 | assertEquals(true, reader.nextBoolean()); |
| 594 | } |
| 595 | |
| 596 | public void testStrictSemicolonDelimitedNameValuePair() throws IOException { |
| 597 | JsonReader reader = new JsonReader(new StringReader("{\"a\":true;\"b\":true}")); |
| 598 | reader.beginObject(); |
| 599 | assertEquals("a", reader.nextName()); |
| 600 | try { |
| 601 | reader.nextBoolean(); |
| 602 | reader.nextName(); |
| 603 | fail(); |
| 604 | } catch (IOException expected) { |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | public void testLenientSemicolonDelimitedNameValuePair() throws IOException { |
| 609 | JsonReader reader = new JsonReader(new StringReader("{\"a\":true;\"b\":true}")); |
| 610 | reader.setLenient(true); |
| 611 | reader.beginObject(); |
| 612 | assertEquals("a", reader.nextName()); |
| 613 | assertEquals(true, reader.nextBoolean()); |
| 614 | assertEquals("b", reader.nextName()); |
| 615 | } |
| 616 | |
| 617 | public void testStrictUnnecessaryArraySeparators() throws IOException { |
| 618 | JsonReader reader = new JsonReader(new StringReader("[true,,true]")); |
| 619 | reader.beginArray(); |
| 620 | assertEquals(true, reader.nextBoolean()); |
| 621 | try { |
| 622 | reader.nextNull(); |
| 623 | fail(); |
| 624 | } catch (IOException expected) { |
| 625 | } |
| 626 | |
| 627 | reader = new JsonReader(new StringReader("[,true]")); |
| 628 | reader.beginArray(); |
| 629 | try { |
| 630 | reader.nextNull(); |
| 631 | fail(); |
| 632 | } catch (IOException expected) { |
| 633 | } |
| 634 | |
| 635 | reader = new JsonReader(new StringReader("[true,]")); |
| 636 | reader.beginArray(); |
| 637 | assertEquals(true, reader.nextBoolean()); |
| 638 | try { |
| 639 | reader.nextNull(); |
| 640 | fail(); |
| 641 | } catch (IOException expected) { |
| 642 | } |
| 643 | |
| 644 | reader = new JsonReader(new StringReader("[,]")); |
| 645 | reader.beginArray(); |
| 646 | try { |
| 647 | reader.nextNull(); |
| 648 | fail(); |
| 649 | } catch (IOException expected) { |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | public void testLenientUnnecessaryArraySeparators() throws IOException { |
| 654 | JsonReader reader = new JsonReader(new StringReader("[true,,true]")); |
| 655 | reader.setLenient(true); |
| 656 | reader.beginArray(); |
| 657 | assertEquals(true, reader.nextBoolean()); |
| 658 | reader.nextNull(); |
| 659 | assertEquals(true, reader.nextBoolean()); |
| 660 | reader.endArray(); |
| 661 | |
| 662 | reader = new JsonReader(new StringReader("[,true]")); |
| 663 | reader.setLenient(true); |
| 664 | reader.beginArray(); |
| 665 | reader.nextNull(); |
| 666 | assertEquals(true, reader.nextBoolean()); |
| 667 | reader.endArray(); |
| 668 | |
| 669 | reader = new JsonReader(new StringReader("[true,]")); |
| 670 | reader.setLenient(true); |
| 671 | reader.beginArray(); |
| 672 | assertEquals(true, reader.nextBoolean()); |
| 673 | reader.nextNull(); |
| 674 | reader.endArray(); |
| 675 | |
| 676 | reader = new JsonReader(new StringReader("[,]")); |
| 677 | reader.setLenient(true); |
| 678 | reader.beginArray(); |
| 679 | reader.nextNull(); |
| 680 | reader.nextNull(); |
| 681 | reader.endArray(); |
| 682 | } |
Jesse Wilson | 76d7e20 | 2010-08-03 17:55:09 -0700 | [diff] [blame] | 683 | } |