Home | History | Annotate | Download | only in cts
      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.cts;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertTrue;
     22 import static org.junit.Assert.fail;
     23 
     24 import android.support.test.filters.SmallTest;
     25 import android.support.test.runner.AndroidJUnit4;
     26 import android.util.JsonReader;
     27 import android.util.JsonToken;
     28 import android.util.MalformedJsonException;
     29 
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 
     33 import java.io.IOException;
     34 import java.io.StringReader;
     35 import java.util.Arrays;
     36 
     37 @SmallTest
     38 @RunWith(AndroidJUnit4.class)
     39 public final class JsonReaderTest {
     40 
     41     private static final int READER_BUFFER_SIZE = 1024;
     42 
     43     @Test
     44     public void testReadArray() throws IOException {
     45         JsonReader reader = new JsonReader(new StringReader("[true, true]"));
     46         reader.beginArray();
     47         assertEquals(true, reader.nextBoolean());
     48         assertEquals(true, reader.nextBoolean());
     49         reader.endArray();
     50         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
     51     }
     52 
     53     @Test
     54     public void testReadEmptyArray() throws IOException {
     55         JsonReader reader = new JsonReader(new StringReader("[]"));
     56         reader.beginArray();
     57         assertFalse(reader.hasNext());
     58         reader.endArray();
     59         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
     60     }
     61 
     62     @Test
     63     public void testReadObject() throws IOException {
     64         JsonReader reader = new JsonReader(new StringReader(
     65                 "{\"a\": \"android\", \"b\": \"banana\"}"));
     66         reader.beginObject();
     67         assertEquals("a", reader.nextName());
     68         assertEquals("android", reader.nextString());
     69         assertEquals("b", reader.nextName());
     70         assertEquals("banana", reader.nextString());
     71         reader.endObject();
     72         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
     73     }
     74 
     75     @Test
     76     public void testReadEmptyObject() throws IOException {
     77         JsonReader reader = new JsonReader(new StringReader("{}"));
     78         reader.beginObject();
     79         assertFalse(reader.hasNext());
     80         reader.endObject();
     81         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
     82     }
     83 
     84     @Test
     85     public void testSkipObject() throws IOException {
     86         JsonReader reader = new JsonReader(new StringReader(
     87                 "{\"a\": { \"c\": [], \"d\": [true, true, {}] }, \"b\": \"banana\"}"));
     88         reader.beginObject();
     89         assertEquals("a", reader.nextName());
     90         reader.skipValue();
     91         assertEquals("b", reader.nextName());
     92         reader.skipValue();
     93         reader.endObject();
     94         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
     95     }
     96 
     97     @Test(expected=IllegalStateException.class)
     98     public void testSkipBeforeEndOfObject() throws IOException {
     99         JsonReader reader = new JsonReader(new StringReader("{}"));
    100         reader.beginObject();
    101         // Should not be possible to skip without elements
    102         reader.skipValue();
    103     }
    104 
    105     @Test(expected=IllegalStateException.class)
    106     public void testSkipBeforeEndOfArray() throws IOException {
    107         JsonReader reader = new JsonReader(new StringReader("[]"));
    108         reader.beginArray();
    109         // Should not be possible to skip without elements
    110         reader.skipValue();
    111     }
    112 
    113     @Test
    114     public void testSkipAfterEndOfDocument() throws IOException {
    115         JsonReader reader = new JsonReader(new StringReader("{}"));
    116         reader.beginObject();
    117         reader.endObject();
    118         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
    119         try {
    120             reader.skipValue();
    121             fail("Should not be possible to skip without elements.");
    122         } catch (IllegalStateException expected) {
    123         }
    124     }
    125 
    126     @Test
    127     public void testHelloWorld() throws IOException {
    128         String json = "{\n" +
    129                 "   \"hello\": true,\n" +
    130                 "   \"foo\": [\"world\"]\n" +
    131                 "}";
    132         JsonReader reader = new JsonReader(new StringReader(json));
    133         reader.beginObject();
    134         assertEquals("hello", reader.nextName());
    135         assertEquals(true, reader.nextBoolean());
    136         assertEquals("foo", reader.nextName());
    137         reader.beginArray();
    138         assertEquals("world", reader.nextString());
    139         reader.endArray();
    140         reader.endObject();
    141         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
    142     }
    143 
    144     @Test(expected=NullPointerException.class)
    145     public void testNulls() {
    146         new JsonReader(null);
    147     }
    148 
    149     @Test(expected=IOException.class)
    150     public void testEmptyString1() throws IOException {
    151         new JsonReader(new StringReader("")).beginArray();
    152     }
    153 
    154     @Test(expected=IOException.class)
    155     public void testEmptyString2() throws IOException {
    156         new JsonReader(new StringReader("")).beginObject();
    157     }
    158 
    159     @Test(expected=IOException.class)
    160     public void testNoTopLevelObject() throws IOException {
    161         new JsonReader(new StringReader("true")).nextBoolean();
    162     }
    163 
    164     @Test
    165     public void testCharacterUnescaping() throws IOException {
    166         String json = "[\"a\","
    167                 + "\"a\\\"\","
    168                 + "\"\\\"\","
    169                 + "\":\","
    170                 + "\",\","
    171                 + "\"\\b\","
    172                 + "\"\\f\","
    173                 + "\"\\n\","
    174                 + "\"\\r\","
    175                 + "\"\\t\","
    176                 + "\" \","
    177                 + "\"\\\\\","
    178                 + "\"{\","
    179                 + "\"}\","
    180                 + "\"[\","
    181                 + "\"]\","
    182                 + "\"\\u0000\","
    183                 + "\"\\u0019\","
    184                 + "\"\\u20AC\""
    185                 + "]";
    186         JsonReader reader = new JsonReader(new StringReader(json));
    187         reader.beginArray();
    188         assertEquals("a", reader.nextString());
    189         assertEquals("a\"", reader.nextString());
    190         assertEquals("\"", reader.nextString());
    191         assertEquals(":", reader.nextString());
    192         assertEquals(",", reader.nextString());
    193         assertEquals("\b", reader.nextString());
    194         assertEquals("\f", reader.nextString());
    195         assertEquals("\n", reader.nextString());
    196         assertEquals("\r", reader.nextString());
    197         assertEquals("\t", reader.nextString());
    198         assertEquals(" ", reader.nextString());
    199         assertEquals("\\", reader.nextString());
    200         assertEquals("{", reader.nextString());
    201         assertEquals("}", reader.nextString());
    202         assertEquals("[", reader.nextString());
    203         assertEquals("]", reader.nextString());
    204         assertEquals("\0", reader.nextString());
    205         assertEquals("\u0019", reader.nextString());
    206         assertEquals("\u20AC", reader.nextString());
    207         reader.endArray();
    208         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
    209     }
    210 
    211     @Test
    212     public void testIntegersWithFractionalPartSpecified() throws IOException {
    213         JsonReader reader = new JsonReader(new StringReader("[1.0,1.0,1.0]"));
    214         reader.beginArray();
    215         assertEquals(1.0, reader.nextDouble(), 0.0f);
    216         assertEquals(1, reader.nextInt());
    217         assertEquals(1L, reader.nextLong());
    218     }
    219 
    220     @Test
    221     public void testDoubles() throws IOException {
    222         String json = "[-0.0,"
    223                 + "1.0,"
    224                 + "1.7976931348623157E308,"
    225                 + "4.9E-324,"
    226                 + "0.0,"
    227                 + "-0.5,"
    228                 + "2.2250738585072014E-308,"
    229                 + "3.141592653589793,"
    230                 + "2.718281828459045,"
    231                 + "\"1.0\","
    232                 + "\"011.0\","
    233                 + "\"NaN\","
    234                 + "\"Infinity\","
    235                 + "\"-Infinity\""
    236                 + "]";
    237         JsonReader reader = new JsonReader(new StringReader(json));
    238         reader.beginArray();
    239         assertEquals(-0.0, reader.nextDouble(), 0.0f);
    240         assertEquals(1.0, reader.nextDouble(), 0.0f);
    241         assertEquals(1.7976931348623157E308, reader.nextDouble(), 0.0f);
    242         assertEquals(4.9E-324, reader.nextDouble(), 0.0f);
    243         assertEquals(0.0, reader.nextDouble(), 0.0f);
    244         assertEquals(-0.5, reader.nextDouble(), 0.0f);
    245         assertEquals(2.2250738585072014E-308, reader.nextDouble(), 0.0f);
    246         assertEquals(3.141592653589793, reader.nextDouble(), 0.0f);
    247         assertEquals(2.718281828459045, reader.nextDouble(), 0.0f);
    248         assertEquals(1.0, reader.nextDouble(), 0.0f);
    249         assertEquals(11.0, reader.nextDouble(), 0.0f);
    250         assertTrue(Double.isNaN(reader.nextDouble()));
    251         assertEquals(Double.POSITIVE_INFINITY, reader.nextDouble(), 0.0f);
    252         assertEquals(Double.NEGATIVE_INFINITY, reader.nextDouble(), 0.0f);
    253         reader.endArray();
    254         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
    255     }
    256 
    257     @Test
    258     public void testLenientDoubles() throws IOException {
    259         String json = "["
    260                 + "011.0,"
    261                 + "NaN,"
    262                 + "NAN,"
    263                 + "Infinity,"
    264                 + "INFINITY,"
    265                 + "-Infinity"
    266                 + "]";
    267         JsonReader reader = new JsonReader(new StringReader(json));
    268         reader.setLenient(true);
    269         reader.beginArray();
    270         assertEquals(11.0, reader.nextDouble(), 0.0f);
    271         assertTrue(Double.isNaN(reader.nextDouble()));
    272         try {
    273             reader.nextDouble();
    274             fail();
    275         } catch (NumberFormatException expected) {
    276         }
    277         assertEquals("NAN", reader.nextString());
    278         assertEquals(Double.POSITIVE_INFINITY, reader.nextDouble(), 0.0f);
    279         try {
    280             reader.nextDouble();
    281             fail();
    282         } catch (NumberFormatException expected) {
    283         }
    284         assertEquals("INFINITY", reader.nextString());
    285         assertEquals(Double.NEGATIVE_INFINITY, reader.nextDouble(), 0.0f);
    286         reader.endArray();
    287         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
    288     }
    289 
    290     @Test
    291     public void testBufferBoundary() throws IOException {
    292         char[] pad = new char[READER_BUFFER_SIZE - 8];
    293         Arrays.fill(pad, '5');
    294         String json = "[\"" + new String(pad) + "\",33333]";
    295         JsonReader reader = new JsonReader(new StringReader(json));
    296         reader.beginArray();
    297         assertEquals(JsonToken.STRING, reader.peek());
    298         assertEquals(new String(pad), reader.nextString());
    299         assertEquals(JsonToken.NUMBER, reader.peek());
    300         assertEquals(33333, reader.nextInt());
    301     }
    302 
    303     @Test
    304     public void testTruncatedBufferBoundary() throws IOException {
    305         char[] pad = new char[READER_BUFFER_SIZE - 8];
    306         Arrays.fill(pad, '5');
    307         String json = "[\"" + new String(pad) + "\",33333";
    308         JsonReader reader = new JsonReader(new StringReader(json));
    309         reader.setLenient(true);
    310         reader.beginArray();
    311         assertEquals(JsonToken.STRING, reader.peek());
    312         assertEquals(new String(pad), reader.nextString());
    313         assertEquals(JsonToken.NUMBER, reader.peek());
    314         assertEquals(33333, reader.nextInt());
    315         try {
    316             reader.endArray();
    317             fail();
    318         } catch (IOException e) {
    319         }
    320     }
    321 
    322     @Test
    323     public void testLongestSupportedNumericLiterals() throws IOException {
    324         verifyLongNumericLiterals(READER_BUFFER_SIZE - 1, JsonToken.NUMBER);
    325     }
    326 
    327     @Test
    328     public void testLongerNumericLiterals() throws IOException {
    329         verifyLongNumericLiterals(READER_BUFFER_SIZE, JsonToken.STRING);
    330     }
    331 
    332     private void verifyLongNumericLiterals(int length, JsonToken expectedToken) throws IOException {
    333         char[] longNumber = new char[length];
    334         Arrays.fill(longNumber, '9');
    335         longNumber[0] = '1';
    336         longNumber[1] = '.';
    337 
    338         String json = "[" + new String(longNumber) + "]";
    339         JsonReader reader = new JsonReader(new StringReader(json));
    340         reader.setLenient(true);
    341         reader.beginArray();
    342         assertEquals(expectedToken, reader.peek());
    343         assertEquals(2.0d, reader.nextDouble(), 0.0f);
    344         reader.endArray();
    345     }
    346 
    347     @Test
    348     public void testLongs() throws IOException {
    349         String json = "[0,0,0,"
    350                 + "1,1,1,"
    351                 + "-1,-1,-1,"
    352                 + "-9223372036854775808,"
    353                 + "9223372036854775807,"
    354                 + "5.0,"
    355                 + "1.0e2,"
    356                 + "\"011\","
    357                 + "\"5.0\","
    358                 + "\"1.0e2\""
    359                 + "]";
    360         JsonReader reader = new JsonReader(new StringReader(json));
    361         reader.beginArray();
    362         assertEquals(0L, reader.nextLong());
    363         assertEquals(0, reader.nextInt());
    364         assertEquals(0.0, reader.nextDouble(), 0.0f);
    365         assertEquals(1L, reader.nextLong());
    366         assertEquals(1, reader.nextInt());
    367         assertEquals(1.0, reader.nextDouble(), 0.0f);
    368         assertEquals(-1L, reader.nextLong());
    369         assertEquals(-1, reader.nextInt());
    370         assertEquals(-1.0, reader.nextDouble(), 0.0f);
    371         try {
    372             reader.nextInt();
    373             fail();
    374         } catch (NumberFormatException expected) {
    375         }
    376         assertEquals(Long.MIN_VALUE, reader.nextLong());
    377         try {
    378             reader.nextInt();
    379             fail();
    380         } catch (NumberFormatException expected) {
    381         }
    382         assertEquals(Long.MAX_VALUE, reader.nextLong());
    383         assertEquals(5, reader.nextLong());
    384         assertEquals(100, reader.nextLong());
    385         assertEquals(11, reader.nextLong());
    386         assertEquals(5, reader.nextLong());
    387         assertEquals(100, reader.nextLong());
    388         reader.endArray();
    389         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
    390     }
    391 
    392     @Test
    393     public void testHighPrecisionDouble_losesPrecision() throws IOException {
    394         // The presence of a fractional part forces us to use Double.parseDouble
    395         // instead of Long.parseLong (even though the fractional part is 0).
    396         //
    397         // A 52 bit mantissa isn't sufficient to precisely represent any of these
    398         // values, so we will lose some precision, thereby storing it as
    399         // ~(9.223372036854776E18). This value is then implicitly converted into
    400         // a long and is required by the JLS to be clamped to Long.MAX_VALUE since
    401         // it's larger than the largest long.
    402         String json = "["
    403                 + "9223372036854775806.000,"  // Long.MAX_VALUE - 1
    404                 + "9223372036854775807.000,"  // Long.MAX_VALUE
    405                 + "9223372036854775808.000"   // Long.MAX_VALUE + 1
    406                 + "]";
    407         JsonReader reader = new JsonReader(new StringReader(json));
    408         reader.beginArray();
    409         assertEquals(Long.MAX_VALUE, reader.nextLong());
    410         assertEquals(Long.MAX_VALUE, reader.nextLong());
    411         assertEquals(Long.MAX_VALUE, reader.nextLong());
    412         reader.endArray();
    413     }
    414 
    415     @Test
    416     public void testMatchingValidNumbers() throws IOException {
    417         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]";
    418         JsonReader reader = new JsonReader(new StringReader(json));
    419         reader.beginArray();
    420         for (int i = 0; i < 16; i++) {
    421             assertEquals(JsonToken.NUMBER, reader.peek());
    422             reader.nextDouble();
    423         }
    424         reader.endArray();
    425     }
    426 
    427     @Test
    428     public void testRecognizingInvalidNumbers() throws IOException {
    429         String json = "[-00,00,001,+1,1f,0x,0xf,0x0,0f1,0ee1,1..0,1e0.1,1.-01,1.+1,1.0x,1.0+]";
    430         JsonReader reader = new JsonReader(new StringReader(json));
    431         reader.setLenient(true);
    432         reader.beginArray();
    433         for (int i = 0; i < 16; i++) {
    434             assertEquals(JsonToken.STRING, reader.peek());
    435             reader.nextString();
    436         }
    437         reader.endArray();
    438     }
    439 
    440     @Test
    441     public void testNonFiniteDouble() throws IOException {
    442         String json = "[NaN]";
    443         JsonReader reader = new JsonReader(new StringReader(json));
    444         reader.beginArray();
    445         try {
    446             reader.nextDouble();
    447             fail();
    448         } catch (IOException expected) {
    449         }
    450     }
    451 
    452     @Test
    453     public void testNumberWithHexPrefix() throws IOException {
    454         String json = "[0x11]";
    455         JsonReader reader = new JsonReader(new StringReader(json));
    456         reader.beginArray();
    457         try {
    458             reader.nextLong();
    459             fail();
    460         } catch (IOException expected) {
    461         }
    462     }
    463 
    464     @Test
    465     public void testNumberWithOctalPrefix() throws IOException {
    466         String json = "[01]";
    467         JsonReader reader = new JsonReader(new StringReader(json));
    468         reader.beginArray();
    469         try {
    470             reader.nextInt();
    471             fail();
    472         } catch (IOException expected) {
    473         }
    474     }
    475 
    476     @Test
    477     public void testBooleans() throws IOException {
    478         JsonReader reader = new JsonReader(new StringReader("[true,false]"));
    479         reader.beginArray();
    480         assertEquals(true, reader.nextBoolean());
    481         assertEquals(false, reader.nextBoolean());
    482         reader.endArray();
    483         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
    484     }
    485 
    486     @Test
    487     public void testMixedCaseLiterals() throws IOException {
    488         JsonReader reader = new JsonReader(new StringReader("[True,TruE,False,FALSE,NULL,nulL]"));
    489         reader.beginArray();
    490         assertEquals(true, reader.nextBoolean());
    491         assertEquals(true, reader.nextBoolean());
    492         assertEquals(false, reader.nextBoolean());
    493         assertEquals(false, reader.nextBoolean());
    494         reader.nextNull();
    495         reader.nextNull();
    496         reader.endArray();
    497         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
    498     }
    499 
    500     @Test
    501     public void testMissingValue() throws IOException {
    502         JsonReader reader = new JsonReader(new StringReader("{\"a\":}"));
    503         reader.beginObject();
    504         assertEquals("a", reader.nextName());
    505         try {
    506             reader.nextString();
    507             fail();
    508         } catch (IOException expected) {
    509         }
    510     }
    511 
    512     @Test
    513     public void testPrematureEndOfInput() throws IOException {
    514         JsonReader reader = new JsonReader(new StringReader("{\"a\":true,"));
    515         reader.beginObject();
    516         assertEquals("a", reader.nextName());
    517         assertEquals(true, reader.nextBoolean());
    518         try {
    519             reader.nextName();
    520             fail();
    521         } catch (IOException expected) {
    522         }
    523     }
    524 
    525     @Test
    526     public void testPrematurelyClosed() throws IOException {
    527         try {
    528             JsonReader reader = new JsonReader(new StringReader("{\"a\":[]}"));
    529             reader.beginObject();
    530             reader.close();
    531             reader.nextName();
    532             fail();
    533         } catch (IllegalStateException expected) {
    534         }
    535 
    536         try {
    537             JsonReader reader = new JsonReader(new StringReader("{\"a\":[]}"));
    538             reader.close();
    539             reader.beginObject();
    540             fail();
    541         } catch (IllegalStateException expected) {
    542         }
    543 
    544         try {
    545             JsonReader reader = new JsonReader(new StringReader("{\"a\":true}"));
    546             reader.beginObject();
    547             reader.nextName();
    548             reader.peek();
    549             reader.close();
    550             reader.nextBoolean();
    551             fail();
    552         } catch (IllegalStateException expected) {
    553         }
    554     }
    555 
    556     @Test
    557     public void testNextFailuresDoNotAdvance() throws IOException {
    558         JsonReader reader = new JsonReader(new StringReader("{\"a\":true}"));
    559         reader.beginObject();
    560         try {
    561             reader.nextString();
    562             fail();
    563         } catch (IllegalStateException expected) {
    564         }
    565         assertEquals("a", reader.nextName());
    566         try {
    567             reader.nextName();
    568             fail();
    569         } catch (IllegalStateException expected) {
    570         }
    571         try {
    572             reader.beginArray();
    573             fail();
    574         } catch (IllegalStateException expected) {
    575         }
    576         try {
    577             reader.endArray();
    578             fail();
    579         } catch (IllegalStateException expected) {
    580         }
    581         try {
    582             reader.beginObject();
    583             fail();
    584         } catch (IllegalStateException expected) {
    585         }
    586         try {
    587             reader.endObject();
    588             fail();
    589         } catch (IllegalStateException expected) {
    590         }
    591         assertEquals(true, reader.nextBoolean());
    592         try {
    593             reader.nextString();
    594             fail();
    595         } catch (IllegalStateException expected) {
    596         }
    597         try {
    598             reader.nextName();
    599             fail();
    600         } catch (IllegalStateException expected) {
    601         }
    602         try {
    603             reader.beginArray();
    604             fail();
    605         } catch (IllegalStateException expected) {
    606         }
    607         try {
    608             reader.endArray();
    609             fail();
    610         } catch (IllegalStateException expected) {
    611         }
    612         reader.endObject();
    613         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
    614         reader.close();
    615     }
    616 
    617     @Test(expected=IllegalStateException.class)
    618     public void testStringNullIsNotNull() throws IOException {
    619         JsonReader reader = new JsonReader(new StringReader("[\"null\"]"));
    620         reader.beginArray();
    621         reader.nextNull();
    622     }
    623 
    624     @Test(expected=IllegalStateException.class)
    625     public void testNullLiteralIsNotAString() throws IOException {
    626         JsonReader reader = new JsonReader(new StringReader("[null]"));
    627         reader.beginArray();
    628         reader.nextString();
    629     }
    630 
    631     @Test
    632     public void testStrictNameValueSeparator() throws IOException {
    633         JsonReader reader = new JsonReader(new StringReader("{\"a\"=true}"));
    634         reader.beginObject();
    635         assertEquals("a", reader.nextName());
    636         try {
    637             reader.nextBoolean();
    638             fail();
    639         } catch (IOException expected) {
    640         }
    641 
    642         reader = new JsonReader(new StringReader("{\"a\"=>true}"));
    643         reader.beginObject();
    644         assertEquals("a", reader.nextName());
    645         try {
    646             reader.nextBoolean();
    647             fail();
    648         } catch (IOException expected) {
    649         }
    650     }
    651 
    652     @Test
    653     public void testLenientNameValueSeparator() throws IOException {
    654         JsonReader reader = new JsonReader(new StringReader("{\"a\"=true}"));
    655         reader.setLenient(true);
    656         reader.beginObject();
    657         assertEquals("a", reader.nextName());
    658         assertEquals(true, reader.nextBoolean());
    659 
    660         reader = new JsonReader(new StringReader("{\"a\"=>true}"));
    661         reader.setLenient(true);
    662         reader.beginObject();
    663         assertEquals("a", reader.nextName());
    664         assertEquals(true, reader.nextBoolean());
    665     }
    666 
    667     @Test
    668     public void testStrictComments() throws IOException {
    669         JsonReader reader = new JsonReader(new StringReader("[// comment \n true]"));
    670         reader.beginArray();
    671         try {
    672             reader.nextBoolean();
    673             fail();
    674         } catch (IOException expected) {
    675         }
    676 
    677         reader = new JsonReader(new StringReader("[# comment \n true]"));
    678         reader.beginArray();
    679         try {
    680             reader.nextBoolean();
    681             fail();
    682         } catch (IOException expected) {
    683         }
    684 
    685         reader = new JsonReader(new StringReader("[/* comment */ true]"));
    686         reader.beginArray();
    687         try {
    688             reader.nextBoolean();
    689             fail();
    690         } catch (IOException expected) {
    691         }
    692     }
    693 
    694     @Test
    695     public void testLenientComments() throws IOException {
    696         JsonReader reader = new JsonReader(new StringReader("[// comment \n true]"));
    697         reader.setLenient(true);
    698         reader.beginArray();
    699         assertEquals(true, reader.nextBoolean());
    700 
    701         reader = new JsonReader(new StringReader("[# comment \n true]"));
    702         reader.setLenient(true);
    703         reader.beginArray();
    704         assertEquals(true, reader.nextBoolean());
    705 
    706         reader = new JsonReader(new StringReader("[/* comment */ true]"));
    707         reader.setLenient(true);
    708         reader.beginArray();
    709         assertEquals(true, reader.nextBoolean());
    710     }
    711 
    712     @Test
    713     public void testStrictUnquotedNames() throws IOException {
    714         JsonReader reader = new JsonReader(new StringReader("{a:true}"));
    715         reader.beginObject();
    716         try {
    717             reader.nextName();
    718             fail();
    719         } catch (IOException expected) {
    720         }
    721     }
    722 
    723     @Test
    724     public void testLenientUnquotedNames() throws IOException {
    725         JsonReader reader = new JsonReader(new StringReader("{a:true}"));
    726         reader.setLenient(true);
    727         reader.beginObject();
    728         assertEquals("a", reader.nextName());
    729     }
    730 
    731     @Test
    732     public void testStrictSingleQuotedNames() throws IOException {
    733         JsonReader reader = new JsonReader(new StringReader("{'a':true}"));
    734         reader.beginObject();
    735         try {
    736             reader.nextName();
    737             fail();
    738         } catch (IOException expected) {
    739         }
    740     }
    741 
    742     @Test
    743     public void testLenientSingleQuotedNames() throws IOException {
    744         JsonReader reader = new JsonReader(new StringReader("{'a':true}"));
    745         reader.setLenient(true);
    746         reader.beginObject();
    747         assertEquals("a", reader.nextName());
    748     }
    749 
    750     @Test(expected=MalformedJsonException.class)
    751     public void testStrictUnquotedStrings() throws IOException {
    752         JsonReader reader = new JsonReader(new StringReader("[a]"));
    753         reader.beginArray();
    754         reader.nextString();
    755     }
    756 
    757     @Test
    758     public void testLenientUnquotedStrings() throws IOException {
    759         JsonReader reader = new JsonReader(new StringReader("[a]"));
    760         reader.setLenient(true);
    761         reader.beginArray();
    762         assertEquals("a", reader.nextString());
    763     }
    764 
    765     @Test
    766     public void testStrictSingleQuotedStrings() throws IOException {
    767         JsonReader reader = new JsonReader(new StringReader("['a']"));
    768         reader.beginArray();
    769         try {
    770             reader.nextString();
    771             fail();
    772         } catch (IOException expected) {
    773         }
    774     }
    775 
    776     @Test
    777     public void testLenientSingleQuotedStrings() throws IOException {
    778         JsonReader reader = new JsonReader(new StringReader("['a']"));
    779         reader.setLenient(true);
    780         reader.beginArray();
    781         assertEquals("a", reader.nextString());
    782     }
    783 
    784     @Test
    785     public void testStrictSemicolonDelimitedArray() throws IOException {
    786         JsonReader reader = new JsonReader(new StringReader("[true;true]"));
    787         reader.beginArray();
    788         try {
    789             reader.nextBoolean();
    790             reader.nextBoolean();
    791             fail();
    792         } catch (IOException expected) {
    793         }
    794     }
    795 
    796     @Test
    797     public void testLenientSemicolonDelimitedArray() throws IOException {
    798         JsonReader reader = new JsonReader(new StringReader("[true;true]"));
    799         reader.setLenient(true);
    800         reader.beginArray();
    801         assertEquals(true, reader.nextBoolean());
    802         assertEquals(true, reader.nextBoolean());
    803     }
    804 
    805     @Test
    806     public void testStrictSemicolonDelimitedNameValuePair() throws IOException {
    807         JsonReader reader = new JsonReader(new StringReader("{\"a\":true;\"b\":true}"));
    808         reader.beginObject();
    809         assertEquals("a", reader.nextName());
    810         try {
    811             reader.nextBoolean();
    812             reader.nextName();
    813             fail();
    814         } catch (IOException expected) {
    815         }
    816     }
    817 
    818     @Test
    819     public void testLenientSemicolonDelimitedNameValuePair() throws IOException {
    820         JsonReader reader = new JsonReader(new StringReader("{\"a\":true;\"b\":true}"));
    821         reader.setLenient(true);
    822         reader.beginObject();
    823         assertEquals("a", reader.nextName());
    824         assertEquals(true, reader.nextBoolean());
    825         assertEquals("b", reader.nextName());
    826     }
    827 
    828     @Test
    829     public void testStrictUnnecessaryArraySeparators() throws IOException {
    830         JsonReader reader = new JsonReader(new StringReader("[true,,true]"));
    831         reader.beginArray();
    832         assertEquals(true, reader.nextBoolean());
    833         try {
    834             reader.nextNull();
    835             fail();
    836         } catch (IOException expected) {
    837         }
    838 
    839         reader = new JsonReader(new StringReader("[,true]"));
    840         reader.beginArray();
    841         try {
    842             reader.nextNull();
    843             fail();
    844         } catch (IOException expected) {
    845         }
    846 
    847         reader = new JsonReader(new StringReader("[true,]"));
    848         reader.beginArray();
    849         assertEquals(true, reader.nextBoolean());
    850         try {
    851             reader.nextNull();
    852             fail();
    853         } catch (IOException expected) {
    854         }
    855 
    856         reader = new JsonReader(new StringReader("[,]"));
    857         reader.beginArray();
    858         try {
    859             reader.nextNull();
    860             fail();
    861         } catch (IOException expected) {
    862         }
    863     }
    864 
    865     @Test
    866     public void testLenientUnnecessaryArraySeparators() throws IOException {
    867         JsonReader reader = new JsonReader(new StringReader("[true,,true]"));
    868         reader.setLenient(true);
    869         reader.beginArray();
    870         assertEquals(true, reader.nextBoolean());
    871         reader.nextNull();
    872         assertEquals(true, reader.nextBoolean());
    873         reader.endArray();
    874 
    875         reader = new JsonReader(new StringReader("[,true]"));
    876         reader.setLenient(true);
    877         reader.beginArray();
    878         reader.nextNull();
    879         assertEquals(true, reader.nextBoolean());
    880         reader.endArray();
    881 
    882         reader = new JsonReader(new StringReader("[true,]"));
    883         reader.setLenient(true);
    884         reader.beginArray();
    885         assertEquals(true, reader.nextBoolean());
    886         reader.nextNull();
    887         reader.endArray();
    888 
    889         reader = new JsonReader(new StringReader("[,]"));
    890         reader.setLenient(true);
    891         reader.beginArray();
    892         reader.nextNull();
    893         reader.nextNull();
    894         reader.endArray();
    895     }
    896 
    897     @Test
    898     public void testStrictMultipleTopLevelValues() throws IOException {
    899         JsonReader reader = new JsonReader(new StringReader("[] []"));
    900         reader.beginArray();
    901         reader.endArray();
    902         try {
    903             reader.peek();
    904             fail();
    905         } catch (IOException expected) {
    906         }
    907     }
    908 
    909     @Test
    910     public void testLenientMultipleTopLevelValues() throws IOException {
    911         JsonReader reader = new JsonReader(new StringReader("[] true {}"));
    912         reader.setLenient(true);
    913         reader.beginArray();
    914         reader.endArray();
    915         assertEquals(true, reader.nextBoolean());
    916         reader.beginObject();
    917         reader.endObject();
    918         assertEquals(JsonToken.END_DOCUMENT, reader.peek());
    919     }
    920 
    921     @Test
    922     public void testStrictTopLevelValueType() {
    923         JsonReader reader = new JsonReader(new StringReader("true"));
    924         try {
    925             reader.nextBoolean();
    926             fail();
    927         } catch (IOException expected) {
    928         }
    929     }
    930 
    931     @Test
    932     public void testLenientTopLevelValueType() throws IOException {
    933         JsonReader reader = new JsonReader(new StringReader("true"));
    934         reader.setLenient(true);
    935         assertEquals(true, reader.nextBoolean());
    936     }
    937 
    938     @Test
    939     public void testStrictNonExecutePrefix() {
    940         JsonReader reader = new JsonReader(new StringReader(")]}'\n []"));
    941         try {
    942             reader.beginArray();
    943             fail();
    944         } catch (IOException expected) {
    945         }
    946     }
    947 
    948     @Test
    949     public void testBomIgnoredAsFirstCharacterOfDocument() throws IOException {
    950         JsonReader reader = new JsonReader(new StringReader("\ufeff[]"));
    951         reader.beginArray();
    952         reader.endArray();
    953     }
    954 
    955     @Test
    956     public void testBomForbiddenAsOtherCharacterInDocument() throws IOException {
    957         JsonReader reader = new JsonReader(new StringReader("[\ufeff]"));
    958         reader.beginArray();
    959         try {
    960             reader.endArray();
    961             fail();
    962         } catch (IOException expected) {
    963         }
    964     }
    965 
    966     @Test
    967     public void testFailWithPosition() throws IOException {
    968         verifyFailWithPosition("Expected literal value at line 6 column 3",
    969                 "[\n\n\n\n\n0,}]");
    970     }
    971 
    972     @Test
    973     public void testFailWithPositionIsOffsetByBom() throws IOException {
    974         verifyFailWithPosition("Expected literal value at line 1 column 4",
    975                 "\ufeff[0,}]");
    976     }
    977 
    978     @Test
    979     public void testFailWithPositionGreaterThanBufferSize() throws IOException {
    980         String spaces = repeat(' ', 8192);
    981         verifyFailWithPosition("Expected literal value at line 6 column 3",
    982                 "[\n\n" + spaces + "\n\n\n0,}]");
    983     }
    984 
    985     private void verifyFailWithPosition(String message, String json) throws IOException {
    986         JsonReader reader = new JsonReader(new StringReader(json));
    987         reader.beginArray();
    988         reader.nextInt();
    989         try {
    990             reader.peek();
    991             fail();
    992         } catch (IOException expected) {
    993             assertEquals(message, expected.getMessage());
    994         }
    995     }
    996 
    997     private String repeat(char c, int count) {
    998         char[] array = new char[count];
    999         Arrays.fill(array, c);
   1000         return new String(array);
   1001     }
   1002 }
   1003