Beispiel #1
0
 public void testMapParsing() {
   Alpha a = Alpha.generateRandomObject(this.random());
   String s = JSONEncoder.toJSON(a);
   Alpha a2 = Alpha.parse(new JSONParser(s));
   String s2 = JSONEncoder.toJSON(a2);
   assertStringsMatch(s, s2);
 }
Beispiel #2
0
  public void testTrailingCommas() {

    String script[] = { //
      "{'hey':42,'you':'outthere',}", //
      "{'hey':42,'you':'outthere'}", //
      "[42,15,16,17,]", //
      "[42,15,16,17]", //
      "[42,15,16,17,null,]", //
      "[42,15,16,17,null]", //
    };

    for (int i = 0; i < script.length; i += 2) {
      String s = swapQuotes(script[i + 0]);
      Object obj = json(s).next();

      String s2 = swapQuotes(script[i + 1]);
      Object obj2 = json(s2).next();

      newEnc().encode(obj);
      String enc1 = enc.toString();

      newEnc().encode(obj2);
      String enc2 = enc.toString();

      assertStringsMatch(enc1, enc2);
    }
  }
Beispiel #3
0
  public void testComments() {

    String script[] = { //
      "{'hey':// this is a comment\n  // this is also a comment\n 42}", //
      "{'hey':42}", //
      "[42,15// start of comment\n//Another comment immediately\n    //Another comment after spaces\n,16]", //
      "[42,15, 16]", //
      "[42,15// zzz\n//zzz\n//zzz\n,16]",
      "[42,15,16]", //
    };

    for (int i = 0; i < script.length; i += 2) {
      String s = swapQuotes(script[i + 0]);
      Object obj = json(s).next();

      String s2 = swapQuotes(script[i + 1]);
      Object obj2 = json(s2).next();

      newEnc().encode(obj);
      String enc1 = enc.toString();

      newEnc().encode(obj2);
      String enc2 = enc.toString();

      assertStringsMatch(enc1, enc2);
    }
  }
Beispiel #4
0
 @Override
 public void encode(JSONEncoder encoder) {
   // TODO Auto-generated method stub
   encoder.enterMap();
   for (String s : map.keySet()) {
     encoder.encode(s);
     encoder.encode((Double) map.get(s));
   }
   encoder.exit();
 }
Beispiel #5
0
  public void testEncodeMap() {
    JSONEncoder enc = new JSONEncoder();
    enc.enterMap();
    enc.encode("a");

    enc.enterList();
    enc.encode(12);
    enc.encode(17);
    enc.exit();

    enc.encode("b");
    enc.encode(true);
    enc.exit();
    String s = enc.toString();
    assertStringsMatch("{\"a\":[12,17],\"b\":true}", s);
  }
Beispiel #6
0
  public void testStreamConstructor() throws UnsupportedEncodingException {
    String orig = "[0,1,2,3,\"hello\"]";
    InputStream stream = new ByteArrayInputStream(orig.getBytes("UTF-8"));
    json = new JSONParser(stream);
    Object a = json.next();
    assertTrue(a instanceof ArrayList);

    enc().encode(a);
    String s = enc.toString();
    assertStringsMatch(s, orig);
  }
Beispiel #7
0
 public void testString() {
   StringBuilder sb = new StringBuilder();
   for (int i = 0; i < 1000; i++) {
     sb.append((char) i);
   }
   String originalString = sb.toString();
   enc().encode(originalString);
   String jsonString = enc.toString();
   json(jsonString);
   String decodedString = json.nextString();
   assertStringsMatch(decodedString, originalString);
 }
Beispiel #8
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);
    }
  }
Beispiel #9
0
 @Override
 public void encode(JSONEncoder encoder) {
   Object[] items = {map.get("message"), map.get("number")};
   encoder.encode(items);
 }
Beispiel #10
0
 public void testArrays() {
   int[] intArray = {1, 2, 3, 4};
   enc().encode(intArray);
   String s = enc.toString();
   assertStringsMatch(s, "[1,2,3,4]");
 }