コード例 #1
0
ファイル: JSONTest.java プロジェクト: BenBE/jetty.project
  @Test
  public void testEnumConvertor() {
    JSON json = new JSON();
    Locale l = new Locale("en", "US");
    json.addConvertor(
        Date.class,
        new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), false, l));
    json.addConvertor(Enum.class, new JSONEnumConvertor(false));
    json.addConvertor(Object.class, new JSONObjectConvertor());

    Woggle w0 = new Woggle();
    Gizmo g0 = new Gizmo();

    w0.name = "woggle0";
    w0.nested = g0;
    w0.number = 100;
    w0.other = Color.Blue;
    g0.name = "woggle1";
    g0.nested = null;
    g0.number = -101;
    g0.tested = true;
    g0.other = Color.Green;

    HashMap map = new HashMap();
    map.put("date", new Date(1));
    map.put("w0", w0);
    map.put("g0", g0);

    StringBuffer buf = new StringBuffer();
    json.append((Appendable) buf, map);
    String js = buf.toString();

    assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") >= 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0);
    assertTrue(js.indexOf("\"tested\":true") >= 0);
    assertTrue(js.indexOf("\"Green\"") >= 0);
    assertTrue(js.indexOf("\"Blue\"") < 0);

    json.addConvertor(
        Date.class,
        new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), true, l));
    json.addConvertor(Enum.class, new JSONEnumConvertor(false));
    w0.nested = null;
    buf = new StringBuffer();
    json.append((Appendable) buf, map);
    js = buf.toString();
    assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") < 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0);

    Map map2 = (HashMap) json.parse(new JSON.StringSource(js));

    assertTrue(map2.get("date") instanceof Date);
    assertTrue(map2.get("w0") instanceof Woggle);
    assertEquals(null, ((Woggle) map2.get("w0")).getOther());
    assertEquals(Color.Green.toString(), ((Map) map2.get("g0")).get("other"));

    json.addConvertor(
        Date.class,
        new JSONDateConvertor(DateCache.DEFAULT_FORMAT, TimeZone.getTimeZone("GMT"), true, l));
    json.addConvertor(Enum.class, new JSONEnumConvertor(true));
    buf = new StringBuffer();
    json.append((Appendable) buf, map);
    js = buf.toString();
    map2 = (HashMap) json.parse(new JSON.StringSource(js));

    assertTrue(map2.get("date") instanceof Date);
    assertTrue(map2.get("w0") instanceof Woggle);
    assertEquals(null, ((Woggle) map2.get("w0")).getOther());
    Object o = ((Map) map2.get("g0")).get("other");
    assertEquals(Color.Green, o);
  }
コード例 #2
0
ファイル: JSONTest.java プロジェクト: BenBE/jetty.project
  /* ------------------------------------------------------------ */
  @Test
  public void testConvertor() {
    // test case#1 - force timezone to GMT
    JSON json = new JSON();
    json.addConvertor(
        Date.class,
        new JSONDateConvertor("MM/dd/yyyy HH:mm:ss zzz", TimeZone.getTimeZone("GMT"), false));
    json.addConvertor(Object.class, new JSONObjectConvertor());

    Woggle w0 = new Woggle();
    Gizmo g0 = new Gizmo();

    w0.name = "woggle0";
    w0.nested = g0;
    w0.number = 100;
    g0.name = "woggle1";
    g0.nested = null;
    g0.number = -101;
    g0.tested = true;

    HashMap map = new HashMap();
    Date dummyDate = new Date(1);
    map.put("date", dummyDate);
    map.put("w0", w0);

    StringBuffer buf = new StringBuffer();
    json.append(buf, map);
    String js = buf.toString();

    assertTrue(js.indexOf("\"date\":\"01/01/1970 00:00:00 GMT\"") >= 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0);
    assertTrue(js.indexOf("\"tested\":true") >= 0);

    // test case#3
    TimeZone tzone = TimeZone.getTimeZone("JST");
    String tzone3Letter = tzone.getDisplayName(false, TimeZone.SHORT);
    String format = "EEE MMMMM dd HH:mm:ss zzz yyyy";

    Locale l = new Locale("ja", "JP");
    if (l != null) {
      json.addConvertor(Date.class, new JSONDateConvertor(format, tzone, false, l));
      buf = new StringBuffer();
      json.append(buf, map);
      js = buf.toString();
      // assertTrue(js.indexOf("\"date\":\"\u6728 1\u6708 01 09:00:00 JST 1970\"")>=0);
      assertTrue(js.indexOf(" 01 09:00:00 JST 1970\"") >= 0);
      assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0);
      assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0);
      assertTrue(js.indexOf("\"tested\":true") >= 0);
    }

    // test case#4
    json.addConvertor(Date.class, new JSONDateConvertor(true));
    w0.nested = null;
    buf = new StringBuffer();
    json.append(buf, map);
    js = buf.toString();
    assertTrue(js.indexOf("\"date\":\"Thu Jan 01 00:00:00 GMT 1970\"") < 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Woggle") >= 0);
    assertTrue(js.indexOf("org.eclipse.jetty.util.ajax.JSONTest$Gizmo") < 0);

    map = (HashMap) json.parse(new JSON.StringSource(js));

    assertTrue(map.get("date") instanceof Date);
    assertTrue(map.get("w0") instanceof Woggle);
  }