Пример #1
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();
  }
Пример #2
0
  public void testPeek2() {
    String s = "{'a':5,'b':null,'c':17}";
    s = JSONTools.swapQuotes(s);

    JSONParser p = new JSONParser(s);
    int sum = 0;
    p.enterMap();
    while (p.hasNext()) {
      String key = p.nextKey();
      if (!p.nextIfNull()) {
        if (!key.equals("a")) {
          assertEquals("c", key);
        }
        int val = p.nextInt();
        sum += val;
      }
    }
    p.exit();
    assertEquals(5 + 17, sum);
  }