Example #1
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;
 }
Example #2
0
  public void testMap() {
    String orig = "{\"u\":14,\"m\":false,\"w\":null,\"k\":true}";
    json(orig);

    json.enterMap();
    Map m = new HashMap();
    for (int i = 0; i < 4; i++) {
      String key = json.nextKey();
      Object value = json.next();
      assertFalse(m.containsKey(key));
      m.put(key, value);
    }
    json.exit();

    assertStringsMatch(m.get("u"), "14.0");
    assertStringsMatch(m.get("m"), "false");
    assertTrue(m.get("w") == null);
    assertStringsMatch(m.get("k"), "true");
  }
Example #3
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);
  }