Beispiel #1
0
 public static OurClass parse(JSONParser json) {
   json.enterList();
   String message = json.nextString();
   int number = json.nextInt();
   json.exit();
   return new OurClass(message, number);
 }
Beispiel #2
0
 public void testNullAsString() {
   json(swapQuotes("['hello',null,'there']"));
   json.enterList();
   assertStringsMatch("hello", json.nextString());
   assertNull(json.nextString());
   assertStringsMatch("there", json.nextString());
   assertFalse(json.hasNext());
 }
Beispiel #3
0
  public void testArray() {
    String orig = "[0,1,2,3,\"hello\"]";

    json(orig);
    json.enterList();
    for (int i = 0; i < 4; i++) {
      assertTrue(json.hasNext());
      assertEquals(i, json.nextInt());
    }
    assertTrue(json.hasNext());
    assertStringsMatch("hello", json.nextString());
    assertFalse(json.hasNext());
    json.exit();
  }
Beispiel #4
0
  public void testPeek() {
    int[] a = {5, 12, -1, 17, 3, -1, 42, -1};

    String s = "[5,12,null,17,3,null,42,null]";
    JSONParser p = new JSONParser(s);
    p.enterList();

    for (int i = 0; i < a.length; i++) {
      assertTrue(p.hasNext());
      Object q = p.peekNext();
      assertTrue((a[i] < 0) == (q == null));
      if (q == null) {
        assertTrue(p.nextIfNull());
      } else {
        assertFalse(p.nextIfNull());
        assertEquals(a[i], p.nextInt());
      }
    }
    assertFalse(p.hasNext());
    p.exit();
  }