Search in sources :

Example 1 with JSONScanner

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

the class TypeUtils method castToLong.

public static Long castToLong(Object value) {
    if (value == null) {
        return null;
    }
    if (value instanceof Number) {
        return ((Number) value).longValue();
    }
    if (value instanceof String) {
        String strVal = (String) value;
        if (//
        strVal.length() == 0 || //
        "null".equals(strVal) || "NULL".equals(strVal)) {
            return null;
        }
        if (strVal.indexOf(',') != 0) {
            strVal = strVal.replaceAll(",", "");
        }
        try {
            return Long.parseLong(strVal);
        } catch (NumberFormatException ex) {
        //
        }
        JSONScanner dateParser = new JSONScanner(strVal);
        Calendar calendar = null;
        if (dateParser.scanISO8601DateIfMatch(false)) {
            calendar = dateParser.getCalendar();
        }
        dateParser.close();
        if (calendar != null) {
            return calendar.getTimeInMillis();
        }
    }
    throw new JSONException("can not cast to long, value : " + value);
}
Also used : JSONScanner(com.alibaba.fastjson.parser.JSONScanner) JSONException(com.alibaba.fastjson.JSONException)

Example 2 with JSONScanner

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

the class LexerTest method test_string.

public void test_string() throws Exception {
    {
        JSONScanner lexer = new JSONScanner("\"中国\"");
        lexer.nextToken();
        Assert.assertEquals("中国", lexer.stringVal());
    }
    {
        JSONScanner lexer = new JSONScanner("\"中国\t\"");
        lexer.nextToken();
        Assert.assertEquals("中国\t", lexer.stringVal());
    }
    {
        JSONScanner lexer = new JSONScanner("\"中国\tV5\"");
        lexer.nextToken();
        Assert.assertEquals("中国\tV5", lexer.stringVal());
    }
    StringBuilder buf = new StringBuilder();
    buf.append('"');
    buf.append("\\\\\\/\\b\\f\\n\\r\\t\\u" + Integer.toHexString('中'));
    buf.append('"');
    buf.append(' ');
    String text = buf.toString();
    JSONScanner lexer = new JSONScanner(text.toCharArray(), text.length() - 1);
    lexer.nextToken();
    Assert.assertEquals(0, lexer.pos());
    String stringVal = lexer.stringVal();
    Assert.assertEquals("\"\\\\/\\b\\f\\n\\r\\t中\"", JSON.toJSONString(stringVal));
}
Also used : JSONScanner(com.alibaba.fastjson.parser.JSONScanner)

Example 3 with JSONScanner

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

the class LexerTest method test_error1.

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);
}
Also used : JSONScanner(com.alibaba.fastjson.parser.JSONScanner)

Example 4 with JSONScanner

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

the class LexerTest method test_error3.

public void test_error3() {
    Exception error = null;
    try {
        JSONScanner lexer = new JSONScanner("");
        lexer.nextToken();
        lexer.nextToken();
        lexer.integerValue();
    } catch (Exception ex) {
        error = ex;
    }
    Assert.assertNotNull(error);
}
Also used : JSONScanner(com.alibaba.fastjson.parser.JSONScanner)

Example 5 with JSONScanner

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

the class LexerTest method test_big_integer_3.

public void test_big_integer_3() throws Exception {
    String text = "9223372036854775809";
    JSONScanner lexer = new JSONScanner(text);
    lexer.nextToken();
    Assert.assertEquals(new BigInteger(text), lexer.integerValue());
}
Also used : JSONScanner(com.alibaba.fastjson.parser.JSONScanner) BigInteger(java.math.BigInteger)

Aggregations

JSONScanner (com.alibaba.fastjson.parser.JSONScanner)179 JSONException (com.alibaba.fastjson.JSONException)37 SymbolTable (com.alibaba.fastjson.parser.SymbolTable)15 JSONReader (com.alibaba.fastjson.JSONReader)11 ParseException (java.text.ParseException)5 BigInteger (java.math.BigInteger)3 DateFormat (java.text.DateFormat)3 SimpleDateFormat (java.text.SimpleDateFormat)3 Date (java.util.Date)3 JSONLexer (com.alibaba.fastjson.parser.JSONLexer)2 JSONObject (com.alibaba.fastjson.JSONObject)1 AccessibleObject (java.lang.reflect.AccessibleObject)1 BigDecimal (java.math.BigDecimal)1 AccessControlException (java.security.AccessControlException)1 Calendar (java.util.Calendar)1