Esempio n. 1
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();
  }
Esempio n. 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());
 }
Esempio n. 3
0
 public static Alpha parse(JSONParser json) {
   Alpha a = new Alpha();
   json.enterMap();
   while (json.hasNext()) {
     String key = json.nextKey();
     double d = json.nextDouble();
     a.map.put(key, d);
   }
   return a;
 }
Esempio n. 4
0
  public void testNumbers() {
    String script[] = {"0", "1", "-123.52e20", "-123.52e-20", "0.5"};
    for (int i = 0; i < script.length; i++) {
      String s = script[i];
      json(s);

      double d = json.nextDouble();
      assertFalse(json.hasNext());
      assertEquals(Double.parseDouble(s), d, 1e-10);
    }
  }
Esempio n. 5
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();
  }
Esempio n. 6
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);
  }
Esempio n. 7
0
  public void testSymmetry() {
    String script[] = { //
      "0", //
      "1", //
      "-1.2352E20", //
      "-1.2352E-20", //
      "0.5", //
      "{'hey':42}", //
      "[1,2,3,4]", //
      "{'hey':42,'you':43}", //
      "{'hey':{'you':17},'array':[1,2,3,4]}", //
      "{'trailing number':5}", //
      "{  'favorite song': { '_skip_order': 3, 'type': 'text','hint': 'name of song','zminlines': 5 },'wow':12 } ", //
    };

    for (int i = 0; i < script.length; i++) {
      String s = swapQuotes(script[i]);
      if (db) pr("\n testing '" + s + "'");

      json(s);
      Object obj = json.next();
      assertFalse(json.hasNext());
      if (db) pr("  parsed " + obj + " (type = " + obj.getClass() + ")");

      newEnc().encode(obj);
      String s2 = enc.toString();
      if (db) pr(" encoded is " + s2);

      Object obj2 = json(s2).next();
      if (db) pr("parsed object: " + obj2);

      newEnc().encode(obj2);
      String s3 = enc.toString();
      assertStringsMatch(s2, s3);
    }
  }