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);
}
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));
}
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);
}
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);
}
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());
}
Aggregations