jsonをbeanに変換
10977 ワード
/**
* data={"id":"1"} json pojo.
*
* @param
* Object
* @param data
* json
* @param clazz
* bean
* @param excludes
*
* @param datePattern
*
* @return T
* @throws Exception
* java.lang.InstantiationException, java.beans.IntrospectionException, java.lang.IllegalAccessException
*/
public static T json2Bean(String data, Class clazz, String[] excludes, String datePattern)
throws Exception {
// JsonUtils.configJson(excludes, datePattern);
T entity = clazz.newInstance();
return json2Bean(data, entity, excludes, datePattern);
}
/**
* json bean.
*
* @param data
* json
* @param clazz
*
* @param
*
* @return clazz
* @throws Exception
*
*/
public static T json2Bean(String data, Class clazz) throws Exception {
return json2Bean(data, clazz, null, null);
}
/**
* data={"id":"1"} json , pojo.
*
* @param
* Object
* @param data
* json
* @param entity
* bean
* @param excludes
*
* @param datePattern
*
* @return T
* @throws Exception
* java.lang.InstantiationException, java.beans.IntrospectionException, java.lang.IllegalAccessException
*/
public static T json2Bean(String data, T entity, String[] excludes, String datePattern)
throws Exception {
// JsonUtils.configJson(excludes, datePattern);
JSONObject jsonObject = JSONObject.fromObject(data);
return json2Bean(jsonObject, entity, excludes, datePattern);
}
/**
* json bean.
*
* @param data
* json
* @param entity
*
* @param
*
* @return
* @throws Exception
*
*/
public static T json2Bean(String data, T entity) throws Exception {
return json2Bean(data, entity, null, null);
}
/**
* Class entity, JSONObject .
*
* @param
* Object
* @param jsonObject
* json
* @param clazz
* bean
* @param excludes
*
* @param datePattern
*
* @return T
* @throws Exception
* java.lang.InstantiationException, java.beans.IntrospectionException, java.lang.IllegalAccessException
*/
public static T json2Bean(JSONObject jsonObject, Class clazz, String[] excludes,
String datePattern) throws Exception {
// JsonUtils.configJson(excludes, datePattern);
T entity = clazz.newInstance();
return json2Bean(jsonObject, entity, excludes, datePattern);
}
/**
* json bean.
*
* @param jsonObject
* JSONObject
* @param clazz
*
* @param
*
* @return
* @throws Exception
*
*/
public static T json2Bean(JSONObject jsonObject, Class clazz) throws Exception {
return json2Bean(jsonObject, clazz, null, null);
}
/**
* JSONObject entity .
*
* @param
* Object
* @param jsonObject
* json
* @param entity
* node
* @param excludes
*
* @param datePattern
*
* @return T
* @throws Exception
* java.lang.InstantiationException, java.beans.IntrospectionException, java.lang.IllegalAccessException
*/
@SuppressWarnings("rawtypes")
public static T json2Bean(JSONObject jsonObject, T entity, String[] excludes, String datePattern)
throws Exception {
// JsonUtils.configJson(excludes, datePattern);
Set excludeSet = createExcludeSet(excludes);
for (Object object : jsonObject.entrySet()) {
Map.Entry entry = (Map.Entry) object;
String propertyName = entry.getKey().toString();
if (excludeSet.contains(propertyName)) {
continue;
}
String propertyValue = entry.getValue().toString();
try {
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(propertyName, entity.getClass());
Class propertyType = propertyDescriptor.getPropertyType();
Method writeMethod = propertyDescriptor.getWriteMethod();
invokeWriteMethod(entity, writeMethod, propertyType, propertyValue, datePattern);
} catch (IntrospectionException ex) {
logger.info(entity.getClass() + ":" + ex.getMessage());
continue;
}
}
return entity;
}
/**
* .
*
* @param excludes
* String[]
* @return exclude set
*/
public static Set createExcludeSet(String[] excludes) {
Set excludeSet = new HashSet();
if (excludes != null) {
for (String exclude : excludes) {
excludeSet.add(exclude);
}
} else {
excludeSet.add("hibernateLazyInitializer");
}
return excludeSet;
}
/**
* , setter .
*
* @param entity
*
* @param writeMethod
* setter
* @param propertyType
*
* @param propertyValue
*
* @param datePattern
*
* @throws IntrospectionException
* methed
* @throws Exception
* e
*/
@SuppressWarnings("rawtypes")
public static void invokeWriteMethod(Object entity, Method writeMethod, Class propertyType, String propertyValue,
String datePattern) throws IntrospectionException, Exception {
try {
/** **/
if (!StringUtil.notNullorEmpty(propertyValue)) {
return;
}
/** **/
if (isPrimivite(propertyType)) {
invokePrimivite(entity, writeMethod, propertyType, propertyValue);
/** String **/
} else if (propertyType == String.class) {
writeMethod.invoke(entity, propertyValue);
/** date **/
} else if (propertyType == Date.class && StringUtil.notNullorEmpty(propertyValue) && !"null".equals(propertyValue)) {
SimpleDateFormat dateFormat = getDateFormat(datePattern);
/** datePattern ,SimpleDateFormat yyyy-MM-dd T HH:mm:ss **/
try {
/** :(2011-11-11) **/
writeMethod.invoke(entity, dateFormat.parse(propertyValue));
} catch (ParseException e) {
/** **/
writeMethod.invoke(entity, getDateFormat("yyyy-MM-dd").parse(propertyValue));
}
}
} catch (IntrospectionException e) {
/** bean json weiterMethod **/
throw new IntrospectionException(" " + writeMethod + " !");
} catch (Exception exception) {
/** **/
logger.error(exception);
throw new Exception(exception);
}
}
/**
* .
*
* @param entity
*
* @param writeMethod
* setter
* @param propertyType
*
* @param propertyValue
*
* @throws Exception
*
*/
@SuppressWarnings("rawtypes")
public static void invokePrimivite(Object entity, Method writeMethod, Class propertyType, String propertyValue)
throws Exception {
if (isByte(propertyType)) {
writeMethod.invoke(entity, Byte.parseByte(propertyValue));
} else if (isShort(propertyType)) {
writeMethod.invoke(entity, Short.parseShort(propertyValue));
} else if (isInt(propertyType)) {
writeMethod.invoke(entity, Integer.parseInt(propertyValue));
} else if (isLong(propertyType)) {
writeMethod.invoke(entity, Long.parseLong(propertyValue));
} else if (isFloat(propertyType)) {
writeMethod.invoke(entity, Float.parseFloat(propertyValue));
} else if (isDouble(propertyType)) {
writeMethod.invoke(entity, Double.parseDouble(propertyValue));
} else if (isBoolean(propertyType)) {
writeMethod.invoke(entity, Boolean.parseBoolean(propertyValue));
} else if (isChar(propertyType)) {
writeMethod.invoke(entity, propertyValue.charAt(0));
}
}
/**
* .
*
* @param clazz
*
* @return boolean
*/
@SuppressWarnings("rawtypes")
public static boolean isPrimivite(Class clazz) {
if (isByte(clazz)) {
return true;
} else if (isShort(clazz)) {
return true;
} else if (isInt(clazz)) {
return true;
} else if (isLong(clazz)) {
return true;
} else if (isFloat(clazz)) {
return true;
} else if (isDouble(clazz)) {
return true;
} else if (isBoolean(clazz)) {
return true;
} else if (isChar(clazz)) {
return true;
}
return false;
}
/**
*
*
* : lzy
* :2012-3-30 10:44:42
*
* @since 1.0
* @param clazz
*
* @return bollean
*/
public static boolean isBigDecimal(@SuppressWarnings("rawtypes") Class clazz) {
return clazz == BigDecimal.class;
}
/**
* byte .
*
* @param clazz
*
* @return boolean
*/
@SuppressWarnings("rawtypes")
public static boolean isByte(Class clazz) {
return (clazz == Byte.class) || (clazz == byte.class);
}