use of com.alibaba.fastjson.parser.JSONLexer in project fastjson by alibaba.
the class IntegerCodec method deserialze.
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
final JSONLexer lexer = parser.lexer;
final int token = lexer.token();
if (token == JSONToken.NULL) {
lexer.nextToken(JSONToken.COMMA);
return null;
}
Integer intObj;
try {
if (token == JSONToken.LITERAL_INT) {
int val = lexer.intValue();
lexer.nextToken(JSONToken.COMMA);
intObj = Integer.valueOf(val);
} else if (token == JSONToken.LITERAL_FLOAT) {
BigDecimal number = lexer.decimalValue();
intObj = TypeUtils.intValue(number);
lexer.nextToken(JSONToken.COMMA);
} else {
if (token == JSONToken.LBRACE) {
JSONObject jsonObject = new JSONObject(true);
parser.parseObject(jsonObject);
intObj = TypeUtils.castToInt(jsonObject);
} else {
Object value = parser.parse();
intObj = TypeUtils.castToInt(value);
}
}
} catch (Exception ex) {
String message = "parseInt error";
if (fieldName != null) {
message += (", field : " + fieldName);
}
throw new JSONException(message, ex);
}
if (clazz == AtomicInteger.class) {
return (T) new AtomicInteger(intObj.intValue());
}
return (T) intObj;
}
use of com.alibaba.fastjson.parser.JSONLexer in project fastjson by alibaba.
the class LongCodec method deserialze.
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
final JSONLexer lexer = parser.lexer;
Long longObject;
try {
final int token = lexer.token();
if (token == JSONToken.LITERAL_INT) {
long longValue = lexer.longValue();
lexer.nextToken(JSONToken.COMMA);
longObject = Long.valueOf(longValue);
} else if (token == JSONToken.LITERAL_FLOAT) {
BigDecimal number = lexer.decimalValue();
longObject = TypeUtils.longValue(number);
lexer.nextToken(JSONToken.COMMA);
} else {
if (token == JSONToken.LBRACE) {
JSONObject jsonObject = new JSONObject(true);
parser.parseObject(jsonObject);
longObject = TypeUtils.castToLong(jsonObject);
} else {
Object value = parser.parse();
longObject = TypeUtils.castToLong(value);
}
if (longObject == null) {
return null;
}
}
} catch (Exception ex) {
throw new JSONException("parseLong error, field : " + fieldName, ex);
}
return //
clazz == AtomicLong.class ? //
(T) new AtomicLong(longObject.longValue()) : (T) longObject;
}
use of com.alibaba.fastjson.parser.JSONLexer in project fastjson by alibaba.
the class TimeDeserializer method deserialze.
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.COMMA) {
lexer.nextToken(JSONToken.LITERAL_STRING);
if (lexer.token() != JSONToken.LITERAL_STRING) {
throw new JSONException("syntax error");
}
lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
if (lexer.token() != JSONToken.LITERAL_INT) {
throw new JSONException("syntax error");
}
long time = lexer.longValue();
lexer.nextToken(JSONToken.RBRACE);
if (lexer.token() != JSONToken.RBRACE) {
throw new JSONException("syntax error");
}
lexer.nextToken(JSONToken.COMMA);
return (T) new java.sql.Time(time);
}
Object val = parser.parse();
if (val == null) {
return null;
}
if (val instanceof java.sql.Time) {
return (T) val;
} else if (val instanceof BigDecimal) {
return (T) new java.sql.Time(TypeUtils.longValue((BigDecimal) val));
} else if (val instanceof Number) {
return (T) new java.sql.Time(((Number) val).longValue());
} else if (val instanceof String) {
String strVal = (String) val;
if (strVal.length() == 0) {
return null;
}
long longVal;
JSONScanner dateLexer = new JSONScanner(strVal);
if (dateLexer.scanISO8601DateIfMatch()) {
longVal = dateLexer.getCalendar().getTimeInMillis();
} else {
boolean isDigit = true;
for (int i = 0; i < strVal.length(); ++i) {
char ch = strVal.charAt(i);
if (ch < '0' || ch > '9') {
isDigit = false;
break;
}
}
if (!isDigit) {
dateLexer.close();
return (T) java.sql.Time.valueOf(strVal);
}
longVal = Long.parseLong(strVal);
}
dateLexer.close();
return (T) new java.sql.Time(longVal);
}
throw new JSONException("parse error");
}
use of com.alibaba.fastjson.parser.JSONLexer in project fastjson by alibaba.
the class BigDecimalCodec method deserialze.
@SuppressWarnings("unchecked")
public static <T> T deserialze(DefaultJSONParser parser) {
final JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.LITERAL_INT) {
BigDecimal decimalValue = lexer.decimalValue();
lexer.nextToken(JSONToken.COMMA);
return (T) decimalValue;
}
if (lexer.token() == JSONToken.LITERAL_FLOAT) {
BigDecimal val = lexer.decimalValue();
lexer.nextToken(JSONToken.COMMA);
return (T) val;
}
Object value = parser.parse();
return //
value == null ? //
null : (T) TypeUtils.castToBigDecimal(value);
}
use of com.alibaba.fastjson.parser.JSONLexer in project fastjson by alibaba.
the class CharArrayCodec method deserialze.
@SuppressWarnings("unchecked")
public static <T> T deserialze(DefaultJSONParser parser) {
final JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.LITERAL_STRING) {
String val = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);
return (T) val.toCharArray();
}
if (lexer.token() == JSONToken.LITERAL_INT) {
Number val = lexer.integerValue();
lexer.nextToken(JSONToken.COMMA);
return (T) val.toString().toCharArray();
}
Object value = parser.parse();
if (value instanceof String) {
return (T) ((String) value).toCharArray();
}
if (value instanceof Collection) {
Collection<?> collection = (Collection) value;
boolean accept = true;
for (Object item : collection) {
if (item instanceof String) {
int itemLength = ((String) item).length();
if (itemLength != 1) {
accept = false;
break;
}
}
}
if (!accept) {
throw new JSONException("can not cast to char[]");
}
char[] chars = new char[collection.size()];
int pos = 0;
for (Object item : collection) {
chars[pos++] = ((String) item).charAt(0);
}
return (T) chars;
}
return //
value == null ? //
null : (T) JSON.toJSONString(value).toCharArray();
}
Aggregations