Search in sources :

Example 26 with JSONReaderScanner

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

the class JSONReaderScannerTest__entity_double 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((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 27 with JSONReaderScanner

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

the class JSONReaderScannerTest__entity_float 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((float) 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 28 with JSONReaderScanner

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

the class JSONReaderScannerTest__entity_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("{\"id\":" + 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(i, 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 29 with JSONReaderScanner

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

the class JSONReaderScannerTest_type method test_true.

@SuppressWarnings("rawtypes")
public void test_true() throws Exception {
    DefaultJSONParser parser = new DefaultJSONParser(new JSONReaderScanner("{\"@type\":\"java.util.LinkedHashMap\",\"name\":\"张三\"}"));
    LinkedHashMap json = (LinkedHashMap) parser.parse();
    Assert.assertEquals("张三", json.get("name"));
    parser.close();
}
Also used : JSONReaderScanner(com.alibaba.fastjson.parser.JSONReaderScanner) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser) LinkedHashMap(java.util.LinkedHashMap)

Example 30 with JSONReaderScanner

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

the class JSONReaderScannerTest method test_doubleQuote_2.

public void test_doubleQuote_2() throws Exception {
    DefaultJSONParser parser = new DefaultJSONParser(new JSONReaderScanner("{name:\"张三\\'\\n\\r\\\"\"}"));
    JSONObject json = parser.parseObject();
    Assert.assertEquals("张三\'\n\r\"", 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