Search in sources :

Example 96 with DefaultJSONParser

use of com.alibaba.fastjson.parser.DefaultJSONParser in project fastjson by alibaba.

the class DefaultExtJSONParserTest_2 method test_error_3.

public void test_error_3() throws Exception {
    JSONException error = null;
    try {
        DefaultJSONParser parser = new DefaultJSONParser("{");
        parser.config(Feature.AllowUnQuotedFieldNames, true);
        parser.parseObject(A.class);
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
Also used : JSONException(com.alibaba.fastjson.JSONException) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 97 with DefaultJSONParser

use of com.alibaba.fastjson.parser.DefaultJSONParser in project fastjson by alibaba.

the class DefaultExtJSONParserTest_4 method test_1.

public void test_1() throws Exception {
    JSONObject res = new JSONObject();
    res.put("a", 1);
    res.put("b", 2);
    res.put("c", 3);
    String[] tests = { "{ 'a':1, 'b':2, 'c':3 }", "{ 'a':1,,'b':2, 'c':3 }", "{,'a':1, 'b':2, 'c':3 }", "{'a':1, 'b':2, 'c':3,,}", "{,,'a':1,,,,'b':2,'c':3,,,,,}" };
    for (String t : tests) {
        DefaultJSONParser ext = new DefaultJSONParser(t);
        ext.config(Feature.AllowArbitraryCommas, true);
        JSONObject extRes = ext.parseObject();
        Assert.assertEquals(res, extRes);
        DefaultJSONParser basic = new DefaultJSONParser(t);
        basic.config(Feature.AllowArbitraryCommas, true);
        JSONObject basicRes = basic.parseObject();
        Assert.assertEquals(res, basicRes);
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 98 with DefaultJSONParser

use of com.alibaba.fastjson.parser.DefaultJSONParser in project fastjson by alibaba.

the class DefaultExtJSONParserTest_4 method test_2.

public void test_2() throws Exception {
    A res = new A();
    res.setA(1);
    res.setB(2);
    res.setC(3);
    String[] tests = { "{ 'a':1, 'b':2, 'c':3 }", "{ 'a':1,,'b':2, 'c':3 }", "{,'a':1, 'b':2, 'c':3 }", "{'a':1, 'b':2, 'c':3,,}", "{,,'a':1,,,,'b':2,,'c':3,,,,,}" };
    for (String t : tests) {
        DefaultJSONParser ext = new DefaultJSONParser(t);
        ext.config(Feature.AllowArbitraryCommas, true);
        A extRes = ext.parseObject(A.class);
        Assert.assertEquals(res, extRes);
    }
}
Also used : DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 99 with DefaultJSONParser

use of com.alibaba.fastjson.parser.DefaultJSONParser in project fastjson by alibaba.

the class JSONReaderScannerTest__entity_boolean method test_scanInt.

public void test_scanInt() throws Exception {
    StringBuffer buf = new StringBuffer();
    buf.append('[');
    for (int i = 0; i < 10; ++i) {
        if (i != 0) {
            buf.append(',');
        }
        // 
        if (i % 2 == 0) {
            buf.append("{\"id\":true}");
        } else {
            buf.append("{\"id\":false}");
        }
    }
    buf.append(']');
    Reader reader = new StringReader(buf.toString());
    JSONReaderScanner scanner = new JSONReaderScanner(reader);
    DefaultJSONParser parser = new DefaultJSONParser(scanner);
    List<VO> array = parser.parseArray(VO.class);
    for (int i = 0; i < array.size(); ++i) {
        if (i % 2 == 0) {
            Assert.assertEquals(true, array.get(i).getId());
        } else {
            Assert.assertEquals(false, array.get(i).getId());
        }
    }
}
Also used : StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) JSONReaderScanner(com.alibaba.fastjson.parser.JSONReaderScanner) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 100 with DefaultJSONParser

use of com.alibaba.fastjson.parser.DefaultJSONParser in project fastjson by alibaba.

the class JSONReaderScannerTest__entity_double_2 method test_scanFloat.

public void test_scanFloat() throws Exception {
    StringBuffer buf = new StringBuffer();
    buf.append('[');
    for (int i = 0; i < 1024; ++i) {
        if (i != 0) {
            buf.append(',');
        }
        buf.append("{\"id\":" + i + ".0}");
    }
    buf.append(']');
    Reader reader = new StringReader(buf.toString());
    JSONReaderScanner scanner = new JSONReaderScanner(reader);
    DefaultJSONParser parser = new DefaultJSONParser(scanner);
    List<VO> array = parser.parseArray(VO.class);
    for (int i = 0; i < array.size(); ++i) {
        Assert.assertTrue(Integer.toString(i), (double) i == array.get(i).getId());
    }
    parser.close();
}
Also used : StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) JSONReaderScanner(com.alibaba.fastjson.parser.JSONReaderScanner) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Aggregations

DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)258 JSONObject (com.alibaba.fastjson.JSONObject)42 JSONReaderScanner (com.alibaba.fastjson.parser.JSONReaderScanner)35 JSONException (com.alibaba.fastjson.JSONException)29 ArrayList (java.util.ArrayList)21 Reader (java.io.Reader)14 StringReader (java.io.StringReader)14 ParserConfig (com.alibaba.fastjson.parser.ParserConfig)11 BigDecimal (java.math.BigDecimal)10 Date (java.util.Date)10 Feature (com.alibaba.fastjson.parser.Feature)9 List (java.util.List)9 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)8 Type (java.lang.reflect.Type)7 JSONArray (com.alibaba.fastjson.JSONArray)6 JSONLexer (com.alibaba.fastjson.parser.JSONLexer)5 ObjectDeserializer (com.alibaba.fastjson.parser.deserializer.ObjectDeserializer)5 TypeUtils.castToString (com.alibaba.fastjson.util.TypeUtils.castToString)5 LinkedHashMap (java.util.LinkedHashMap)5 MiscCodec (com.alibaba.fastjson.serializer.MiscCodec)4