Search in sources :

Example 1 with JSONReaderScanner

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

the class JSONReaderScannerTest__entity_stringList 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(',');
        }
        // 1000000000000
        // 
        buf.append("{\"id\":[\"" + i + "\",\"" + (10000 + i) + "\"]}");
    }
    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.assertEquals(2, array.get(i).getId().size());
        Assert.assertEquals(Integer.toString(i), array.get(i).getId().get(0));
        Assert.assertEquals(Integer.toString(10000 + i), array.get(i).getId().get(1));
    }
}
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 2 with JSONReaderScanner

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

the class JSONReaderScannerTest_array_string 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(',');
        }
        // 1000000000000
        // 
        buf.append("\"" + i + "\"");
    }
    buf.append(']');
    Reader reader = new StringReader(buf.toString());
    JSONReaderScanner scanner = new JSONReaderScanner(reader);
    DefaultJSONParser parser = new DefaultJSONParser(scanner);
    JSONArray array = (JSONArray) parser.parse();
    for (int i = 0; i < array.size(); ++i) {
        Assert.assertEquals(Integer.toString(i), array.get(i));
    }
}
Also used : StringReader(java.io.StringReader) JSONArray(com.alibaba.fastjson.JSONArray) StringReader(java.io.StringReader) Reader(java.io.Reader) JSONReaderScanner(com.alibaba.fastjson.parser.JSONReaderScanner) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 3 with JSONReaderScanner

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

the class JSONReaderScannerTest_decimal method test_scanInt.

public void test_scanInt() throws Exception {
    StringBuffer buf = new StringBuffer();
    buf.append('[');
    for (int i = 0; i < 1024; ++i) {
        if (i != 0) {
            buf.append(',');
        }
        buf.append(i + ".0");
    }
    buf.append(']');
    Reader reader = new StringReader(buf.toString());
    JSONReaderScanner scanner = new JSONReaderScanner(reader);
    DefaultJSONParser parser = new DefaultJSONParser(scanner);
    JSONArray array = (JSONArray) parser.parse();
    for (int i = 0; i < array.size(); ++i) {
        BigDecimal value = new BigDecimal(i + ".0");
        Assert.assertEquals(value, array.get(i));
    }
}
Also used : StringReader(java.io.StringReader) JSONArray(com.alibaba.fastjson.JSONArray) StringReader(java.io.StringReader) Reader(java.io.Reader) JSONReaderScanner(com.alibaba.fastjson.parser.JSONReaderScanner) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser) BigDecimal(java.math.BigDecimal)

Example 4 with JSONReaderScanner

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

the class JSONReaderScannerTest_int method test_scanInt.

public void test_scanInt() throws Exception {
    StringBuffer buf = new StringBuffer();
    buf.append('[');
    for (int i = 0; i < 1024; ++i) {
        if (i != 0) {
            buf.append(',');
        }
        buf.append(i);
    }
    buf.append(']');
    Reader reader = new StringReader(buf.toString());
    JSONReaderScanner scanner = new JSONReaderScanner(reader);
    DefaultJSONParser parser = new DefaultJSONParser(scanner);
    JSONArray array = (JSONArray) parser.parse();
    for (int i = 0; i < array.size(); ++i) {
        Assert.assertEquals(i, ((Integer) array.get(i)).intValue());
    }
}
Also used : StringReader(java.io.StringReader) JSONArray(com.alibaba.fastjson.JSONArray) StringReader(java.io.StringReader) Reader(java.io.Reader) JSONReaderScanner(com.alibaba.fastjson.parser.JSONReaderScanner) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 5 with JSONReaderScanner

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

the class DateTest method test_reader.

public void test_reader() throws Exception {
    DefaultJSONParser parser = new DefaultJSONParser(new JSONReaderScanner("{\"date\":\"2012/04-01\"}", 0));
    parser.setDateFormat("yyyy/MM-dd");
    VO vo = parser.parseObject(VO.class);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM-dd", JSON.defaultLocale);
    dateFormat.setTimeZone(JSON.defaultTimeZone);
    Assert.assertEquals(dateFormat.parse("2012/04-01"), vo.getDate());
    parser.close();
}
Also used : JSONReaderScanner(com.alibaba.fastjson.parser.JSONReaderScanner) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

JSONReaderScanner (com.alibaba.fastjson.parser.JSONReaderScanner)36 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)35 JSONObject (com.alibaba.fastjson.JSONObject)14 Reader (java.io.Reader)14 StringReader (java.io.StringReader)14 JSONArray (com.alibaba.fastjson.JSONArray)5 BigDecimal (java.math.BigDecimal)3 SimpleDateFormat (java.text.SimpleDateFormat)1 LinkedHashMap (java.util.LinkedHashMap)1