@Test
public void testDate() throws ScriptException, NoSuchMethodException
{

    JSONConfig cfg = new JSONConfig();
    // non-standard JSON - only works with eval() and my parser.
    cfg.setEncodeDatesAsObjects(true);
    Map<String,Object> jsonObj = new LinkedHashMap<>();
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    cal.set(2015, 8, 16, 14, 8, 34);
    cal.set(Calendar.MILLISECOND, 34);
    jsonObj.put("t", cal.getTime());
    String json = JSONUtil.toJSON(jsonObj, cfg);
    assertThat(json, is("{\"t\":new Date(\"2015-09-16T14:08:34.034Z\")}"));
    // examine the Javascript object created by this JSON and eval().
    Object result = evalJSON(json);
    if ( result instanceof ScriptObjectMirror ){
        ScriptObjectMirror mirror = (ScriptObjectMirror)result;
        Object obj = mirror.get("t");
        if ( obj instanceof ScriptObjectMirror ){
            ScriptObjectMirror t = (ScriptObjectMirror)obj;
            if ( t.getClassName().equals("Date") ){
                assertEquals(new Double(2015), t.callMember("getUTCFullYear"));
                assertEquals(new Double(8), t.callMember("getUTCMonth"));
                assertEquals(new Double(16), t.callMember("getUTCDate"));
                assertEquals(new Double(14), t.callMember("getUTCHours"));
                assertEquals(new Double(8), t.callMember("getUTCMinutes"));
                assertEquals(new Double(34), t.callMember("getUTCSeconds"));
                assertEquals(new Double(34), t.callMember("getUTCMilliseconds"));
            }else{
                fail("Expected Date from t");
            }
        }else{
            fail("Expected ScriptObjectMirror from result");
        }
    }else{
        fail("Expected ScriptObjectMirror from evalJSON");
    }
    cfg.setEncodeDatesAsStrings(true);
    json = JSONUtil.toJSON(jsonObj, cfg);
    validateJSON(json);
    assertThat(json, is("{\"t\":\"2015-09-16T14:08:34.034Z\"}"));
}