Search in sources :

Example 6 with JSONReaderScanner

use of com.alibaba.fastjson.parser.JSONReaderScanner 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)

Example 7 with JSONReaderScanner

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

the class JSONReaderScannerTest__entity_enum 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
        //
        Type type;
        if (i % 3 == 0) {
            type = Type.A;
        } else if (i % 3 == 1) {
            type = Type.AA;
        } else {
            type = Type.AAA;
        }
        buf.append("{\"id\":\"" + type.name() + "\"}");
    }
    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) {
        Type type;
        if (i % 3 == 0) {
            type = Type.A;
        } else if (i % 3 == 1) {
            type = Type.AA;
        } else {
            type = Type.AAA;
        }
        Assert.assertEquals(type, 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 8 with JSONReaderScanner

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

the class JSONReaderScannerTest__entity_long 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
        //
        long value = (long) 1000000000000L + 1L + (long) i;
        buf.append("{\"id\":" + value + "}");
    }
    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) {
        long value = (long) 1000000000000L + 1L + (long) i;
        Assert.assertEquals(value, 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 9 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 10 with JSONReaderScanner

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

the class JSONReaderScannerTest_boolean method test_true.

public void test_true() throws Exception {
    DefaultJSONParser parser = new DefaultJSONParser(new JSONReaderScanner("{\"name\":true}"));
    JSONObject json = parser.parseObject();
    Assert.assertEquals(Boolean.TRUE, json.get("name"));
    parser.close();
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONReaderScanner(com.alibaba.fastjson.parser.JSONReaderScanner) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Aggregations

JSONReaderScanner (com.alibaba.fastjson.parser.JSONReaderScanner)35 DefaultJSONParser (com.alibaba.fastjson.parser.DefaultJSONParser)34 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 LinkedHashMap (java.util.LinkedHashMap)1