Java 类com.alibaba.fastjson.parser.JSONScanner 实例源码

项目:GitHub    文件:LexerTest.java   
public void test_string3() throws Exception {
    StringBuilder buf = new StringBuilder();

    buf.append('"');
    for (int i = 0; i < 200; ++i) {
        buf.append("abcdefghijklmn012345689ABCDEFG");
    }
    buf.append('"');

    String text = buf.toString();

    JSONScanner lexer = new JSONScanner(text.toCharArray(), text.length());
    lexer.nextToken();

    Assert.assertEquals(0, lexer.pos());

    lexer.stringVal();
}
项目:GitHub    文件:JSONScannerTest_singQuoteString.java   
public void test_string2() throws Exception {
    StringBuilder buf = new StringBuilder();

    buf.append('\'');
    for (int i = 0; i < 200; ++i) {
        buf.append("\\\\\\/\\b\\f\\n\\r\\t\\u" + Integer.toHexString('中'));
    }
    buf.append('\'');

    String text = buf.toString();

    JSONScanner lexer = new JSONScanner(text.toCharArray(), text.length());
    lexer.config(Feature.AllowSingleQuotes, true);
    lexer.nextToken();

    Assert.assertEquals(0, lexer.pos());

    String stringVal = lexer.stringVal();

    // Assert.assertEquals("\"\\\\\\/\\b\\f\\n\\r\\t中\"",
    // JSON.toJSONString(stringVal));

    JSON.toJSONString(stringVal);
}
项目:GitHub    文件:JSONReaderTest_object_long.java   
public void test_read_1() throws Exception {
    JSONReader reader = new JSONReader(new JSONScanner(text));

    reader.startObject();

    int count = 0;
    while (reader.hasNext()) {
        String key = (String) reader.readObject();
        Long value = reader.readLong();
        count++;
    }
    Assert.assertEquals(10, count);

    reader.endObject();
    reader.close();
}
项目:GitHub    文件:JSONScannerTest_singQuoteString.java   
public void test_string4() throws Exception {
    StringBuilder buf = new StringBuilder();

    buf.append('\'');
    for (int i = 0; i < 200; ++i) {
        buf.append("\\tabcdefghijklmn012345689ABCDEFG");
    }
    buf.append('\'');

    String text = buf.toString();

    JSONScanner lexer = new JSONScanner(text.toCharArray(), text.length());
    lexer.config(Feature.AllowSingleQuotes, true);
    lexer.nextToken();

    Assert.assertEquals(0, lexer.pos());

    String stringVal = lexer.stringVal();

    // Assert.assertEquals("\"\\\\\\/\\b\\f\\n\\r\\t中\"",
    // JSON.toJSONString(stringVal));

    JSON.toJSONString(stringVal);
}
项目:GitHub    文件:JSONReaderTest_array_object.java   
public void test_read_3() throws Exception {
    JSONReader reader = new JSONReader(new JSONScanner(text));
    reader.startArray();



    Assert.assertTrue(reader.hasNext());
    reader.startObject();
    reader.endObject();

    Assert.assertTrue(reader.hasNext());
    reader.startObject();
    reader.endObject();

    int count = 2;

    while (reader.hasNext()) {
        reader.startObject();
        reader.endObject();
        count++;
    }
    Assert.assertEquals(10, count);

    reader.endArray();
    reader.close();
}
项目:GitHub    文件:TypeUtils.java   
public static java.sql.Date castToSqlDate(Object value){
    if(value == null){
        return null;
    }
    if(value instanceof java.sql.Date){
        return (java.sql.Date) value;
    }
    if(value instanceof java.util.Date){
        return new java.sql.Date(((java.util.Date) value).getTime());
    }
    if(value instanceof Calendar){
        return new java.sql.Date(((Calendar) value).getTimeInMillis());
    }
    long longValue = 0;
    if(value instanceof Number){
        longValue = ((Number) value).longValue();
    }
    if(value instanceof String){
        String strVal = (String) value;
        if(strVal.length() == 0 //
                || "null".equals(strVal) //
                || "NULL".equals(strVal)){
            return null;
        }
        if(isNumber(strVal)){
            longValue = Long.parseLong(strVal);
        } else{
            JSONScanner scanner = new JSONScanner(strVal);
            if(scanner.scanISO8601DateIfMatch(false)){
                longValue = scanner.getCalendar().getTime().getTime();
            } else{
                throw new JSONException("can not cast to Timestamp, value : " + strVal);
            }
        }
    }
    if(longValue <= 0){
        throw new JSONException("can not cast to Date, value : " + value); // TODO 忽略 1970-01-01 之前的时间处理?
    }
    return new java.sql.Date(longValue);
}
项目:GitHub    文件:JSONScannerTest_symbol.java   
public void test_0() throws Exception {
    SymbolTable symbolTable = new SymbolTable(512);

    JSONScanner lexer = new JSONScanner("\"name\"");
    String symbol = lexer.scanSymbol(symbolTable, '"');
    Assert.assertTrue("name".equals(symbol));
    lexer.close();
}
项目:GitHub    文件:JSONScannerTest_symbol.java   
public void test_1() throws Exception {
    SymbolTable symbolTable = new SymbolTable(512);

    JSONScanner lexer = new JSONScanner("\"nick name\"");
    String symbol = lexer.scanSymbol(symbolTable, '"');
    Assert.assertTrue("nick name".equals(symbol));
    lexer.close();
}
项目:GitHub    文件:JSONScannerTest_symbol.java   
public void test_5() throws Exception {
    SymbolTable symbolTable = new SymbolTable(512);

    JSONScanner lexer = new JSONScanner("\"nick \\bname\"");
    String symbol = lexer.scanSymbol(symbolTable, '"');
    Assert.assertTrue("nick \bname" == symbol);
    lexer.close();
}
项目:GitHub    文件:JSONScannerTest_symbol.java   
public void test_6() throws Exception {
    SymbolTable symbolTable = new SymbolTable(512);

    JSONScanner lexer = new JSONScanner("\"nick \\f name\"");
    String symbol = lexer.scanSymbol(symbolTable, '"');
    Assert.assertTrue("nick \f name" == symbol);
    lexer.close();
}
项目:GitHub    文件:LexerTest.java   
public void test_float() throws Exception {
    String text = "123456789.0123";
    JSONScanner lexer = new JSONScanner(text);
    lexer.nextToken();
    BigDecimal decimalValue = lexer.decimalValue();
    Assert.assertEquals(new BigDecimal("123456789.0123"), decimalValue);

}
项目:GitHub    文件:JSONScannerTest_symbol.java   
public void test_10() throws Exception {
    SymbolTable symbolTable = new SymbolTable(512);

    JSONScanner lexer = new JSONScanner("\"nick \\t name\"");
    String symbol = lexer.scanSymbol(symbolTable, '"');
    Assert.assertTrue("nick \t name" == symbol);
    lexer.close();
}
项目:GitHub    文件:JSONScannerTest_symbol.java   
public void test_11() throws Exception {
    SymbolTable symbolTable = new SymbolTable(512);

    JSONScanner lexer = new JSONScanner("\"nick \\u4e2d name\"");
    String symbol = lexer.scanSymbol(symbolTable, '"');
    Assert.assertTrue("nick 中 name" == symbol);
    lexer.close();
}
项目:GitHub    文件:Issue119.java   
public void test_for_issue_b() throws Exception {
    JSONScanner lexer = new JSONScanner("-10B");
    lexer.scanNumber();
    Assert.assertEquals(Byte.class, lexer.integerValue().getClass());
    Assert.assertEquals(-10, lexer.integerValue().byteValue());
    lexer.close();
}
项目:GitHub    文件:JSONScannerTest_symbol.java   
public void test_error() throws Exception {
    JSONException error = null;
    try {
        SymbolTable symbolTable = new SymbolTable(512);

        JSONScanner lexer = new JSONScanner("\"nick \\a name\"");
        lexer.scanSymbol(symbolTable, '"');
        lexer.close();
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONScannerTest_symbol.java   
public void test_error_2() throws Exception {
    JSONException error = null;
    try {
        SymbolTable symbolTable = new SymbolTable(512);

        JSONScanner lexer = new JSONScanner("\"name");
        lexer.scanSymbol(symbolTable, '"');
        lexer.close();
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONScannerTest_singQuoteString.java   
public void test_error() throws Exception {
    Exception error = null;
    try {
        JSONScanner lexer = new JSONScanner("'k");
        lexer.config(Feature.AllowSingleQuotes, true);
        lexer.nextToken();
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONScannerTest_int.java   
public void test_error_1() {
    Exception error = null;
    try {
        String text = Integer.MAX_VALUE + "1234";
        JSONScanner lexer = new JSONScanner(text);
        lexer.scanNumber();
        lexer.intValue();
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:LexerTest.java   
public void test_error1() throws Exception {
    Exception error = null;
    try {
        JSONScanner lexer = new JSONScanner("\"\\k\"");
        lexer.nextToken();
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONScannerTest_true.java   
public void test_scan_true_1() throws Exception {
    JSONException error = null;
    try {
        JSONScanner lexer = new JSONScanner("frue");
        lexer.scanTrue();
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONScannerTest_true.java   
public void test_scan_true_2() throws Exception {
    JSONException error = null;
    try {
        JSONScanner lexer = new JSONScanner("ttue");
        lexer.scanTrue();
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONScannerTest_true.java   
public void test_scan_true_3() throws Exception {
    JSONException error = null;
    try {
        JSONScanner lexer = new JSONScanner("trze");
        lexer.scanTrue();
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONScannerTest_true.java   
public void test_scan_true_4() throws Exception {
    JSONException error = null;
    try {
        JSONScanner lexer = new JSONScanner("truz");
        lexer.scanTrue();
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONScannerTest_true.java   
public void test_scan_true_5() throws Exception {
    JSONException error = null;
    try {
        JSONScanner lexer = new JSONScanner("truee");
        lexer.scanTrue();
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONScannerTest_new.java   
public void test_scan_new_4() throws Exception {
    JSONException error = null;
    try {
        JSONScanner lexer = new JSONScanner("neww");
        lexer.scanNullOrNew();
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONScannerTest_null.java   
public void test_scan_null_1() throws Exception {
    JSONException error = null;
    try {
        JSONScanner lexer = new JSONScanner("zull");
        lexer.scanNullOrNew();
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONScannerTest_null.java   
public void test_scan_null_3() throws Exception {
    JSONException error = null;
    try {
        JSONScanner lexer = new JSONScanner("nuzl");
        lexer.scanNullOrNew();
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:LexerTest.java   
public void test_isWhitespace() throws Exception {
    new JSONScanner("".toCharArray(), 0);
    Assert.assertTrue(JSONScanner.isWhitespace(' '));
    Assert.assertTrue(JSONScanner.isWhitespace('\b'));
    Assert.assertTrue(JSONScanner.isWhitespace('\f'));
    Assert.assertTrue(JSONScanner.isWhitespace('\n'));
    Assert.assertTrue(JSONScanner.isWhitespace('\r'));
    Assert.assertTrue(JSONScanner.isWhitespace('\t'));
    Assert.assertFalse(JSONScanner.isWhitespace('k'));
}
项目:GitHub    文件:JSONScannerTest_null.java   
public void test_scan_null_5() throws Exception {
    JSONException error = null;
    try {
        JSONScanner lexer = new JSONScanner("nulle");
        lexer.scanNullOrNew();
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONScannerTest_null.java   
public void test_scan_null_6() throws Exception {
    JSONException error = null;
    try {
        JSONScanner lexer = new JSONScanner("null\"");
        lexer.scanNullOrNew();
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONReaderTest_object_object.java   
public void test_read_2() throws Exception {
    JSONReader reader = new JSONReader(new JSONScanner("{{}:{},{}:{}}"));
    reader.startObject();

    Assert.assertTrue(reader.hasNext());
    reader.startObject();
    reader.endObject();

    reader.startObject();
    reader.endObject();

    Assert.assertTrue(reader.hasNext());
    reader.startObject();
    reader.endObject();

    reader.startObject();
    reader.endObject();

    Assert.assertFalse(reader.hasNext());

    reader.endObject();

    Exception error = null;
    try {
        reader.hasNext();
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);

    reader.close();
}
项目:boohee_v5.6    文件:TypeUtils.java   
public static final Long castToLong(Object value) {
    if (value == null) {
        return null;
    }
    if (value instanceof Number) {
        return Long.valueOf(((Number) value).longValue());
    }
    if (value instanceof String) {
        String strVal = (String) value;
        if (strVal.length() == 0 || "null".equals(strVal)) {
            return null;
        }
        try {
            return Long.valueOf(Long.parseLong(strVal));
        } catch (NumberFormatException e) {
            JSONScanner dateParser = new JSONScanner(strVal);
            Calendar calendar = null;
            if (dateParser.scanISO8601DateIfMatch(false)) {
                calendar = dateParser.getCalendar();
            }
            dateParser.close();
            if (calendar != null) {
                return Long.valueOf(calendar.getTimeInMillis());
            }
        }
    }
    throw new JSONException("can not cast to long, value : " + value);
}
项目:GitHub    文件:Issue119.java   
public void test_for_issue() throws Exception {
    JSONScanner lexer = new JSONScanner("-100S");
    lexer.resetStringPosition();
    lexer.scanNumber();
    Assert.assertEquals(Short.class, lexer.integerValue().getClass());
    Assert.assertEquals(-100, lexer.integerValue().shortValue());
    lexer.close();
}
项目:GitHub    文件:JSONScannerTest_ident.java   
public void test_Date() throws Exception {
    String text = "Date";
    JSONScanner lexer = new JSONScanner(text);
    lexer.scanIdent();
    Assert.assertEquals(JSONToken.IDENTIFIER, lexer.token());
    Assert.assertEquals(text, lexer.stringVal());
}
项目:GitHub    文件:JSONScannerTest_singQuoteString.java   
public void test_error_1() throws Exception {
    Exception error = null;
    try {
        JSONScanner lexer = new JSONScanner("'k\\k'");
        lexer.config(Feature.AllowSingleQuotes, true);
        lexer.nextToken();
        Assert.assertEquals(JSONToken.ERROR, lexer.token());
    } catch (JSONException ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONReaderTest_array_object.java   
public void test_read_1() throws Exception {
    JSONReader reader = new JSONReader(new JSONScanner(text));
    reader.startArray();

    int count = 0;
    while (reader.hasNext()) {
        reader.readObject();
        count++;
    }
    Assert.assertEquals(10, count);

    reader.endArray();
    reader.close();
}
项目:GitHub    文件:JSONScannerTest_scanSymbol.java   
public void test_1() throws Exception {
    JSONScanner lexer = new JSONScanner("\"value\":\"aa\"},");
    long hashCode = lexer.scanFieldSymbol("\"value\":".toCharArray());
    Assert.assertEquals(fnv_hash("aa"), hashCode);
    Assert.assertEquals(JSONScanner.END, lexer.matchStat());
    Assert.assertEquals(JSONToken.COMMA, lexer.token());
}
项目:GitHub    文件:JSONScannerTest_scanSymbol.java   
public void test_2() throws Exception {
    JSONScanner lexer = new JSONScanner("\"value\":\"aa\"}]");
    long hashCode = lexer.scanFieldSymbol("\"value\":".toCharArray());
    Assert.assertEquals(fnv_hash("aa"), hashCode);
    Assert.assertEquals(JSONScanner.END, lexer.matchStat());
    Assert.assertEquals(JSONToken.RBRACKET, lexer.token());
}
项目:GitHub    文件:JSONScannerTest_new.java   
public void test_scan_new_3() throws Exception {
    JSONException error = null;
    try {
        JSONScanner lexer = new JSONScanner("neel");
        lexer.scanNullOrNew();
    } catch (JSONException e) {
        error = e;
    }
    Assert.assertNotNull(error);
}
项目:GitHub    文件:JSONScannerTest_scanSymbol.java   
public void test_4() throws Exception {
    JSONScanner lexer = new JSONScanner("\"value\":\"aa\"}");
    long hashCode = lexer.scanFieldSymbol("\"value\":".toCharArray());
    Assert.assertEquals(fnv_hash("aa"), hashCode);
    Assert.assertEquals(JSONScanner.END, lexer.matchStat());
    Assert.assertEquals(JSONToken.EOF, lexer.token());
}