use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.
the class AwtCodec method parseFont.
protected Font parseFont(DefaultJSONParser parser) {
JSONLexer lexer = parser.lexer;
int size = 0, style = 0;
String name = null;
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");
}
if (key.equalsIgnoreCase("name")) {
if (lexer.token() == JSONToken.LITERAL_STRING) {
name = lexer.stringVal();
lexer.nextToken();
} else {
throw new JSONException("syntax error");
}
} else if (key.equalsIgnoreCase("style")) {
if (lexer.token() == JSONToken.LITERAL_INT) {
style = lexer.intValue();
lexer.nextToken();
} else {
throw new JSONException("syntax error");
}
} else if (key.equalsIgnoreCase("size")) {
if (lexer.token() == JSONToken.LITERAL_INT) {
size = lexer.intValue();
lexer.nextToken();
} else {
throw new JSONException("syntax error");
}
} else {
throw new JSONException("syntax error, " + key);
}
if (lexer.token() == JSONToken.COMMA) {
lexer.nextToken(JSONToken.LITERAL_STRING);
}
}
return new Font(name, style, size);
}
use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.
the class ClobSeriliazer method write.
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
try {
if (object == null) {
serializer.writeNull();
return;
}
Clob clob = (Clob) object;
Reader reader = clob.getCharacterStream();
StringBuilder buf = new StringBuilder();
try {
char[] chars = new char[2048];
for (; ; ) {
int len = reader.read(chars, 0, chars.length);
if (len < 0) {
break;
}
buf.append(chars, 0, len);
}
} catch (Exception ex) {
throw new JSONException("read string from reader error", ex);
}
String text = buf.toString();
reader.close();
serializer.write(text);
} catch (SQLException e) {
throw new IOException("write clob error", e);
}
}
use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.
the class TypeUtils method getCollectionItemClass.
public static Class<?> getCollectionItemClass(Type fieldType) {
if (fieldType instanceof ParameterizedType) {
Class<?> itemClass;
Type actualTypeArgument = ((ParameterizedType) fieldType).getActualTypeArguments()[0];
if (actualTypeArgument instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) actualTypeArgument;
Type[] upperBounds = wildcardType.getUpperBounds();
if (upperBounds.length == 1) {
actualTypeArgument = upperBounds[0];
}
}
if (actualTypeArgument instanceof Class) {
itemClass = (Class<?>) actualTypeArgument;
if (!Modifier.isPublic(itemClass.getModifiers())) {
throw new JSONException("can not create ASMParser");
}
} else {
throw new JSONException("can not create ASMParser");
}
return itemClass;
}
return Object.class;
}
use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.
the class TypeUtils method castToJavaBean.
@SuppressWarnings({ "unchecked" })
public static <T> T castToJavaBean(Map<String, Object> map, Class<T> clazz, ParserConfig config) {
try {
if (clazz == StackTraceElement.class) {
String declaringClass = (String) map.get("className");
String methodName = (String) map.get("methodName");
String fileName = (String) map.get("fileName");
int lineNumber;
{
Number value = (Number) map.get("lineNumber");
if (value == null) {
lineNumber = 0;
} else {
lineNumber = value.intValue();
}
}
return (T) new StackTraceElement(declaringClass, methodName, fileName, lineNumber);
}
{
Object iClassObject = map.get(JSON.DEFAULT_TYPE_KEY);
if (iClassObject instanceof String) {
String className = (String) iClassObject;
Class<?> loadClazz;
if (config == null) {
config = ParserConfig.global;
}
loadClazz = config.checkAutoType(className, null);
if (loadClazz == null) {
throw new ClassNotFoundException(className + " not found");
}
if (!loadClazz.equals(clazz)) {
return (T) castToJavaBean(map, loadClazz, config);
}
}
}
if (clazz.isInterface()) {
JSONObject object;
if (map instanceof JSONObject) {
object = (JSONObject) map;
} else {
object = new JSONObject(map);
}
return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { clazz }, object);
}
if (clazz == Locale.class) {
Object arg0 = map.get("language");
Object arg1 = map.get("country");
if (arg0 instanceof String) {
String language = (String) arg0;
if (arg1 instanceof String) {
String country = (String) arg1;
return (T) new Locale(language, country);
} else if (arg1 == null) {
return (T) new Locale(language);
}
}
}
if (config == null) {
config = ParserConfig.getGlobalInstance();
}
JavaBeanDeserializer javaBeanDeser = null;
ObjectDeserializer deserizer = config.getDeserializer(clazz);
if (deserizer instanceof JavaBeanDeserializer) {
javaBeanDeser = (JavaBeanDeserializer) deserizer;
}
if (javaBeanDeser == null) {
throw new JSONException("can not get javaBeanDeserializer. " + clazz.getName());
}
return (T) javaBeanDeser.createInstance(map, config);
} catch (Exception e) {
throw new JSONException(e.getMessage(), e);
}
}
use of com.alibaba.fastjson.JSONException in project fastjson by alibaba.
the class TypeUtils method castToEnum.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> T castToEnum(Object obj, Class<T> clazz, ParserConfig mapping) {
try {
if (obj instanceof String) {
String name = (String) obj;
if (name.length() == 0) {
return null;
}
return (T) Enum.valueOf((Class<? extends Enum>) clazz, name);
}
if (obj instanceof Number) {
int ordinal = ((Number) obj).intValue();
Object[] values = clazz.getEnumConstants();
if (ordinal < values.length) {
return (T) values[ordinal];
}
}
} catch (Exception ex) {
throw new JSONException("can not cast to : " + clazz.getName(), ex);
}
throw new JSONException("can not cast to : " + clazz.getName());
}
Aggregations