@Test
public void testParser() throws ParseException
{

    Object obj = JSONParser.parseJSON("{\"foo\":\"b\\\\\\\"ar\",\"a\":5,\"b\":2.37e24,c:Infinity,\"d\":NaN,\"e\":[1,2,3,{\"a\":4}]}");
    String json = JSONUtil.toJSON(obj);
    assertEquals("{\"foo\":\"b\\\\\\\"ar\",\"a\":5,\"b\":2.37E24,\"c\":\"Infinity\",\"d\":\"NaN\",\"e\":[1,2,3,{\"a\":4}]}", json);
    obj = JSONParser.parseJSON("'foo'");
    assertEquals("foo", obj);
    obj = JSONParser.parseJSON("2.37e24");
    assertEquals(2.37e24, obj);
    obj = JSONParser.parseJSON("Infinity");
    assertTrue(Double.isInfinite((Double)obj));
    obj = JSONParser.parseJSON("NaN");
    assertTrue(Double.isNaN((Double)obj));
    obj = JSONParser.parseJSON("false");
    assertEquals(Boolean.FALSE, obj);
    obj = JSONParser.parseJSON("null");
    assertEquals(null, obj);
    JSONConfig cfg = new JSONConfig().setUsePrimitiveArrays(true);
    obj = JSONParser.parseJSON("[1.1,2.2,-3.134598765,4.0]", cfg);
    double[] doubles = (double[])obj;
    assertEquals(new Double(1.1), new Double(doubles[0]));
    assertEquals(new Double(2.2), new Double(doubles[1]));
    assertEquals(new Double(-3.134598765), new Double(doubles[2]));
    assertEquals(new Double(4.0), new Double(doubles[3]));
    obj = JSONParser.parseJSON("[1.1,2.2,-3.134,4.0]", cfg);
    float[] floats = (float[])obj;
    assertEquals(new Float(1.1), new Float(floats[0]));
    assertEquals(new Float(2.2), new Float(floats[1]));
    assertEquals(new Float(-3.134), new Float(floats[2]));
    assertEquals(new Float(4.0), new Float(floats[3]));
    obj = JSONParser.parseJSON("[1,2,-3,4]", cfg);
    byte[] bytes = (byte[])obj;
    assertEquals(new Byte((byte)1), new Byte(bytes[0]));
    assertEquals(new Byte((byte)2), new Byte(bytes[1]));
    assertEquals(new Byte((byte)-3), new Byte(bytes[2]));
    assertEquals(new Byte((byte)4), new Byte(bytes[3]));
    // parse various forms of date strings.
    cfg.setEncodeDatesAsStrings(true);
    DateFormat fmt = cfg.getDateGenFormat();
    Date dt = (Date)JSONParser.parseJSON("new Date(\"2015-09-16T14:08:34.034Z\")", cfg);
    assertEquals("2015-09-16T14:08:34.034Z", fmt.format(dt));
    dt = (Date)JSONParser.parseJSON("\"2015-09-16T14:08:34.034Z\"", cfg);
    assertEquals("2015-09-16T14:08:34.034Z", fmt.format(dt));
    dt = (Date)JSONParser.parseJSON("\"2015-09-16T14:08:34.034+01\"", cfg);
    assertEquals("2015-09-16T14:08:34.034Z", fmt.format(dt));
    dt = (Date)JSONParser.parseJSON("\"2015-09-16T14:08:34.034+01:30\"", cfg);
    assertEquals("2015-09-16T12:38:34.034Z", fmt.format(dt));
    dt = (Date)JSONParser.parseJSON("\"2015-09-16T14:08:34\"", cfg);
    assertEquals("2015-09-16T14:08:34.034Z", fmt.format(dt));
    dt = (Date)JSONParser.parseJSON("\"2015-09-16T14:08:34+01:30\"", cfg);
    assertEquals("2015-09-16T12:38:34.034Z", fmt.format(dt));
    // custom formats.
    DateFormat nfmt = cfg.setDateGenFormat("EEE, d MMM yyyy HH:mm:ss Z");
    nfmt.setTimeZone(TimeZone.getTimeZone("US/Eastern"));
    cfg.addDateParseFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
    dt = (Date)JSONParser.parseJSON("\"2001.07.04 AD at 12:08:56 EDT\"", cfg);
    assertEquals("Wed, 4 Jul 2001 12:08:56 -0400", nfmt.format(dt));
    // test that the old one still works.
    dt = (Date)JSONParser.parseJSON("\"2015-09-16T14:08:34+01:30\"", cfg);
    assertEquals("2015-09-16T12:38:34.034Z", fmt.format(dt));
    try{
        JSONParser.parseJSON("{\"foo\":\"b\\\\\\\"ar\",\"a\":5,\"b\":2.37e24,\"c\":&*^,\"d\":NaN,\"e\":[1,2,3,{\"a\":4}]}");
        fail("Expected JSONParserException for bad data");
    }catch ( JSONParserException e ){
    }
}