Search in sources :

Example 21 with JSONReaderScanner

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

the class JSONReaderScannerTest method test_doubleQuote.

public void test_doubleQuote() 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)

Example 22 with JSONReaderScanner

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

the class JSONLexerAllowCommentTest method test_0.

public void test_0() throws Exception {
    String jsonWithComment = "{ /*tes****\n\r\n*t*/\"a\":1 /*****test88888*****/ /*test*/ , /*test*/ //test\n //est\n \"b\":2}";
    JSONObject object = JSON.parseObject(jsonWithComment, Feature.AllowComment, Feature.OrderedField);
    System.out.println(object.toJSONString());
    Assert.assertEquals("{\"a\":1,\"b\":2}", object.toJSONString());
    DefaultJSONParser parser = new DefaultJSONParser(new JSONReaderScanner(jsonWithComment, Feature.AllowComment.getMask() | Feature.OrderedField.getMask()));
    JSONObject object1 = parser.parseObject();
    Assert.assertEquals("{\"a\":1,\"b\":2}", object1.toJSONString());
    System.out.println(object1.toJSONString());
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) JSONReaderScanner(com.alibaba.fastjson.parser.JSONReaderScanner) DefaultJSONParser(com.alibaba.fastjson.parser.DefaultJSONParser)

Example 23 with JSONReaderScanner

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

the class JSONReaderScannerTest__map_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("{\"id\":\"" + 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.getJSONObject(i).get("id"));
    }
}
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 24 with JSONReaderScanner

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

the class JSONReaderScannerTest_long 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(',');
        }
        long value = (long) Integer.MAX_VALUE + 1L + (long) i;
        buf.append(value);
    }
    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) {
        long value = (long) Integer.MAX_VALUE + 1L + (long) i;
        Assert.assertEquals(value, ((Long) array.get(i)).longValue());
    }
}
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 25 with JSONReaderScanner

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

the class JSONLexerTest_8 method test_ident_3.

public void test_ident_3() throws Exception {
    JSONReaderScanner lexer = new JSONReaderScanner("123");
    lexer.nextIdent();
    org.junit.Assert.assertEquals(JSONToken.LITERAL_INT, lexer.token());
    lexer.close();
}
Also used : JSONReaderScanner(com.alibaba.fastjson.parser.JSONReaderScanner)

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