use of io.leopard.json.JsonException in project leopard by tanhaichao.
the class LeopardBeanPropertyRowMapper method getColumnValue.
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Object getColumnValue(ResultSet rs, int index, ResultSetMetaData resultSetMetaData, String columnName, Field field) throws SQLException {
Class<?> requiredType = field.getType();
// JdbcUtils.getResultSetValue(rs, index, requiredType);// TODO 20171012 这是多余的代码吧?
Object value;
if (String.class.equals(requiredType)) {
value = rs.getString(index);
} else if (boolean.class.equals(requiredType) || Boolean.class.equals(requiredType)) {
value = rs.getBoolean(index);
} else if (byte.class.equals(requiredType) || Byte.class.equals(requiredType)) {
value = rs.getByte(index);
} else if (short.class.equals(requiredType) || Short.class.equals(requiredType)) {
value = rs.getShort(index);
} else if (int.class.equals(requiredType) || Integer.class.equals(requiredType)) {
value = rs.getInt(index);
} else if (long.class.equals(requiredType) || Long.class.equals(requiredType)) {
value = rs.getLong(index);
} else if (float.class.equals(requiredType) || Float.class.equals(requiredType)) {
value = rs.getFloat(index);
} else if (double.class.equals(requiredType) || Double.class.equals(requiredType) || Number.class.equals(requiredType)) {
value = rs.getDouble(index);
} else if (byte[].class.equals(requiredType)) {
value = rs.getBytes(index);
} else if (java.sql.Timestamp.class.equals(requiredType) || java.util.Date.class.equals(requiredType)) {
value = rs.getTimestamp(index);
} else if (java.util.Date.class.isAssignableFrom(requiredType)) {
Timestamp timestamp = rs.getTimestamp(index);
if (timestamp == null) {
return null;
}
Date date;
try {
date = (Date) requiredType.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(requiredType.getName() + "没有默认构造方法.", e);
} catch (IllegalAccessException e) {
throw new RuntimeException(requiredType.getName() + "默认构造方法实例化失败.", e);
}
date.setTime(timestamp.getTime());
value = date;
} else if (List.class.equals(requiredType)) {
// System.out.println("name:" + field.getName() + " json:" + json);
ParameterizedType type = (ParameterizedType) field.getGenericType();
String elementClassName = type.getActualTypeArguments()[0].getTypeName();
if (int.class.getName().equals(elementClassName) || Integer.class.getName().equals(elementClassName)) {
String json = rs.getString(index);
value = Json.toListObject(json, Integer.class);
} else if (long.class.getName().equals(elementClassName) || Long.class.getName().equals(elementClassName)) {
String json = rs.getString(index);
value = Json.toListObject(json, Long.class);
} else if (String.class.getName().equals(elementClassName)) {
String json = rs.getString(index);
value = Json.toListObject(json, String.class);
} else {
Class<?> elementType;
try {
elementType = Class.forName(elementClassName);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e.getMessage(), e);
}
if (elementType.isEnum()) {
String json = rs.getString(index);
return toEnumList(json, elementType, field);
} else {
Class<?> clazz = (Class<?>) type.getActualTypeArguments()[0];
String json = rs.getString(index);
// System.out.println("clazz:" + clazz.getName());
value = Json.toListObject(json, clazz, true);
// throw new IllegalArgumentException("未知数据类型[" +
// elementClassName + "].");
}
}
} else if (requiredType.isEnum()) {
if (Snum.class.isAssignableFrom(requiredType)) {
String key = rs.getString(index);
if (key == null) {
return null;
}
try {
return onumResolver.toEnum(key, (Class<? extends Enum>) requiredType, field);
// return EnumUtil.toEnum(key, (Class<? extends Enum>) requiredType);
} catch (EnumConstantInvalidException e) {
if ("".equals(key)) {
return null;
}
throw e;
}
} else if (Inum.class.isAssignableFrom(requiredType)) {
int key = rs.getInt(index);
return onumResolver.toEnum(key, (Class<? extends Enum>) requiredType, field);
// return EnumUtil.toEnum(key, (Class<? extends Enum>) requiredType);
} else if (Bnum.class.isAssignableFrom(requiredType)) {
byte key = rs.getByte(index);
return onumResolver.toEnum(key, (Class<? extends Enum>) requiredType, field);
// return EnumUtil.toEnum(key, (Class<? extends Enum>) requiredType);
} else {
throw new RuntimeException("未知枚举类型[" + requiredType.getName() + "].");
}
} else if (DynamicEnum.class.isAssignableFrom(requiredType)) {
if (Snum.class.isAssignableFrom(requiredType)) {
String key = rs.getString(index);
if (key == null) {
return null;
}
try {
return DynamicEnumUtil.toEnum(key, (Class<? extends DynamicEnum>) requiredType);
} catch (EnumConstantInvalidException e) {
// e.printStackTrace();
if ("".equals(key)) {
return null;
}
throw e;
}
} else if (Inum.class.isAssignableFrom(requiredType)) {
int key = rs.getInt(index);
// return EnumUtil.toEnum(key, (Class<? extends Enum>) requiredType);
return DynamicEnumUtil.toEnum(key, (Class<? extends DynamicEnum>) requiredType);
} else if (Bnum.class.isAssignableFrom(requiredType)) {
byte key = rs.getByte(index);
// return EnumUtil.toEnum(key, (Class<? extends Enum>) requiredType);
return DynamicEnumUtil.toEnum(key, (Class<? extends DynamicEnum>) requiredType);
} else {
throw new RuntimeException("未知枚举类型[" + requiredType.getName() + "].");
}
} else if (Object.class.equals(requiredType)) {
throw new RuntimeException("实体类的字段类型不能使用Object.");
} else {
String json = rs.getString(index);
try {
value = Json.toObject(json, requiredType, true);
} catch (JsonException e) {
System.err.println("JsonException fieldName:" + field.getName() + " " + requiredType.getName() + " json:" + json);
throw e;
}
// throw new SQLException("未知数据类型[" + requiredType.getName() +
// "].");
}
return value;
}
use of io.leopard.json.JsonException in project leopard by tanhaichao.
the class UnderlineJson method toEnumListByOnlyKey.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static <T> List<T> toEnumListByOnlyKey(String json, Class<T> clazz) {
JavaType javaType = getObjectMapper().getTypeFactory().constructParametrizedType(ArrayList.class, List.class, Object.class);
List<Object> keyList;
try {
keyList = getObjectMapper().readValue(json, javaType);
} catch (Exception e) {
logger.error("clazz:" + clazz.getName() + " json:" + json);
throw new JsonException(e.getMessage(), e);
}
// System.err.println("keyList:" + keyList);
List<T> list = new ArrayList<T>();
for (Object key : keyList) {
T onum = (T) EnumUtil.toEnum(key, (Class<Enum>) clazz);
// System.err.println("key:" + key + " onum:" + onum);
list.add(onum);
}
return list;
}
use of io.leopard.json.JsonException in project leopard by tanhaichao.
the class UnderlineJson method toEnumList.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T> List<T> toEnumList(String json, Class<T> clazz) {
JavaType javaType = getObjectMapper().getTypeFactory().constructParametrizedType(ArrayList.class, List.class, Map.class);
List<Map<String, Object>> mapList;
try {
mapList = getObjectMapper().readValue(json, javaType);
} catch (Exception e) {
// throw new JsonException(e.getMessage(), e);
return toEnumListByOnlyKey(json, clazz);
}
List<T> list = new ArrayList<T>();
for (Map<String, Object> map : mapList) {
Object key = map.get("key");
if (key == null) {
throw new EnumConstantInvalidException("枚举的key不允许为null.");
}
list.add((T) EnumUtil.toEnum(key, (Class<Enum>) clazz));
}
return list;
}
Aggregations