use of com.alibaba.fastjson.parser.JSONLexer in project fastjson by alibaba.
the class StringCodec method deserialze.
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type clazz, Object fieldName) {
if (clazz == StringBuffer.class) {
final JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.LITERAL_STRING) {
String val = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);
return (T) new StringBuffer(val);
}
Object value = parser.parse();
if (value == null) {
return null;
}
return (T) new StringBuffer(value.toString());
}
if (clazz == StringBuilder.class) {
final JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.LITERAL_STRING) {
String val = lexer.stringVal();
lexer.nextToken(JSONToken.COMMA);
return (T) new StringBuilder(val);
}
Object value = parser.parse();
if (value == null) {
return null;
}
return (T) new StringBuilder(value.toString());
}
return (T) deserialze(parser);
}
use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
the class JSONReader method readString.
public String readString() {
Object object;
if (context == null) {
object = parser.parse();
} else {
readBefore();
JSONLexer lexer = parser.lexer;
if (context.state == JSONStreamContext.StartObject && lexer.token() == JSONToken.IDENTIFIER) {
object = lexer.stringVal();
lexer.nextToken();
} else {
object = parser.parse();
}
readAfter();
}
return TypeUtils.castToString(object);
}
use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
the class ThrowableDeserializer method deserialze.
@SuppressWarnings("unchecked")
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
JSONLexer lexer = parser.lexer;
if (lexer.token() == JSONToken.NULL) {
lexer.nextToken();
return null;
}
if (parser.getResolveStatus() == DefaultJSONParser.TypeNameRedirect) {
parser.setResolveStatus(DefaultJSONParser.NONE);
} else {
if (lexer.token() != JSONToken.LBRACE) {
throw new JSONException("syntax error");
}
}
Throwable cause = null;
Class<?> exClass = null;
if (type != null && type instanceof Class) {
Class<?> clazz = (Class<?>) type;
if (Throwable.class.isAssignableFrom(clazz)) {
exClass = clazz;
}
}
String message = null;
StackTraceElement[] stackTrace = null;
Map<String, Object> otherValues = null;
for (; ; ) {
// lexer.scanSymbol
String key = lexer.scanSymbol(parser.getSymbolTable());
if (key == null) {
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
break;
}
if (lexer.token() == JSONToken.COMMA) {
if (lexer.isEnabled(Feature.AllowArbitraryCommas)) {
continue;
}
}
}
lexer.nextTokenWithColon(JSONToken.LITERAL_STRING);
if (JSON.DEFAULT_TYPE_KEY.equals(key)) {
if (lexer.token() == JSONToken.LITERAL_STRING) {
String exClassName = lexer.stringVal();
exClass = parser.getConfig().checkAutoType(exClassName, Throwable.class, lexer.getFeatures());
} else {
throw new JSONException("syntax error");
}
lexer.nextToken(JSONToken.COMMA);
} else if ("message".equals(key)) {
if (lexer.token() == JSONToken.NULL) {
message = null;
} else if (lexer.token() == JSONToken.LITERAL_STRING) {
message = lexer.stringVal();
} else {
throw new JSONException("syntax error");
}
lexer.nextToken();
} else if ("cause".equals(key)) {
cause = deserialze(parser, null, "cause");
} else if ("stackTrace".equals(key)) {
stackTrace = parser.parseObject(StackTraceElement[].class);
} else {
if (otherValues == null) {
otherValues = new HashMap<String, Object>();
}
otherValues.put(key, parser.parse());
}
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken(JSONToken.COMMA);
break;
}
}
Throwable ex = null;
if (exClass == null) {
ex = new Exception(message, cause);
} else {
if (!Throwable.class.isAssignableFrom(exClass)) {
throw new JSONException("type not match, not Throwable. " + exClass.getName());
}
try {
ex = createException(message, cause, exClass);
if (ex == null) {
ex = new Exception(message, cause);
}
} catch (Exception e) {
throw new JSONException("create instance error", e);
}
}
if (stackTrace != null) {
ex.setStackTrace(stackTrace);
}
if (otherValues != null) {
JavaBeanDeserializer exBeanDeser = null;
if (exClass != null) {
if (exClass == clazz) {
exBeanDeser = this;
} else {
ObjectDeserializer exDeser = parser.getConfig().getDeserializer(exClass);
if (exDeser instanceof JavaBeanDeserializer) {
exBeanDeser = (JavaBeanDeserializer) exDeser;
}
}
}
if (exBeanDeser != null) {
for (Map.Entry<String, Object> entry : otherValues.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
FieldDeserializer fieldDeserializer = exBeanDeser.getFieldDeserializer(key);
if (fieldDeserializer != null) {
fieldDeserializer.setValue(ex, value);
}
}
}
}
return (T) ex;
}
use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
the class AwtCodec method parseColor.
protected Color parseColor(DefaultJSONParser parser) {
JSONLexer lexer = parser.lexer;
int r = 0, g = 0, b = 0, alpha = 0;
for (; ; ) {
if (lexer.token() == JSONToken.RBRACE) {
lexer.nextToken();
break;
}
String key;
if (lexer.token() == JSONToken.LITERAL_STRING) {
key = lexer.stringVal();
lexer.nextTokenWithColon(JSONToken.LITERAL_INT);
} else {
throw new JSONException("syntax error");
}
int val;
if (lexer.token() == JSONToken.LITERAL_INT) {
val = lexer.intValue();
lexer.nextToken();
} else {
throw new JSONException("syntax error");
}
if (key.equalsIgnoreCase("r")) {
r = val;
} else if (key.equalsIgnoreCase("g")) {
g = val;
} else if (key.equalsIgnoreCase("b")) {
b = val;
} else if (key.equalsIgnoreCase("alpha")) {
alpha = val;
} else {
throw new JSONException("syntax error, " + key);
}
if (lexer.token() == JSONToken.COMMA) {
lexer.nextToken(JSONToken.LITERAL_STRING);
}
}
return new Color(r, g, b, alpha);
}
use of com.alibaba.fastjson.parser.JSONLexer in project uavstack by uavorg.
the class JavaBeanDeserializer method deserialzeArrayMapping.
@SuppressWarnings({ "unchecked" })
public <T> T deserialzeArrayMapping(DefaultJSONParser parser, Type type, Object fieldName, Object object) {
// xxx
final JSONLexer lexer = parser.lexer;
if (lexer.token() != JSONToken.LBRACKET) {
throw new JSONException("error");
}
object = createInstance(parser, type);
for (int i = 0, size = sortedFieldDeserializers.length; i < size; ++i) {
final char seperator = (i == size - 1) ? ']' : ',';
FieldDeserializer fieldDeser = sortedFieldDeserializers[i];
Class<?> fieldClass = fieldDeser.fieldInfo.fieldClass;
if (fieldClass == int.class) {
int value = lexer.scanInt(seperator);
fieldDeser.setValue(object, value);
} else if (fieldClass == String.class) {
String value = lexer.scanString(seperator);
fieldDeser.setValue(object, value);
} else if (fieldClass == long.class) {
long value = lexer.scanLong(seperator);
fieldDeser.setValue(object, value);
} else if (fieldClass.isEnum()) {
char ch = lexer.getCurrent();
Object value;
if (ch == '\"' || ch == 'n') {
value = lexer.scanEnum(fieldClass, parser.getSymbolTable(), seperator);
} else if (ch >= '0' && ch <= '9') {
int ordinal = lexer.scanInt(seperator);
EnumDeserializer enumDeser = (EnumDeserializer) ((DefaultFieldDeserializer) fieldDeser).getFieldValueDeserilizer(parser.getConfig());
value = enumDeser.valueOf(ordinal);
} else {
value = scanEnum(lexer, seperator);
}
fieldDeser.setValue(object, value);
} else if (fieldClass == boolean.class) {
boolean value = lexer.scanBoolean(seperator);
fieldDeser.setValue(object, value);
} else if (fieldClass == float.class) {
float value = lexer.scanFloat(seperator);
fieldDeser.setValue(object, value);
} else if (fieldClass == double.class) {
double value = lexer.scanDouble(seperator);
fieldDeser.setValue(object, value);
} else if (fieldClass == java.util.Date.class && lexer.getCurrent() == '1') {
long longValue = lexer.scanLong(seperator);
fieldDeser.setValue(object, new java.util.Date(longValue));
} else if (fieldClass == BigDecimal.class) {
BigDecimal value = lexer.scanDecimal(seperator);
fieldDeser.setValue(object, value);
} else {
lexer.nextToken(JSONToken.LBRACKET);
Object value = parser.parseObject(fieldDeser.fieldInfo.fieldType, fieldDeser.fieldInfo.name);
fieldDeser.setValue(object, value);
if (lexer.token() == JSONToken.RBRACKET) {
break;
}
check(lexer, seperator == ']' ? JSONToken.RBRACKET : JSONToken.COMMA);
// parser.accept(seperator == ']' ? JSONToken.RBRACKET : JSONToken.COMMA);
}
}
lexer.nextToken(JSONToken.COMMA);
return (T) object;
}
Aggregations