JsonObject转换Bean对象和Bean对象转换JsonObject工具类(填坑后)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lvhao2813/article/details/80805154

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;


import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;


import com.boco.framework.common.util.JsonDateValueProcessor;
import com.boco.framework.common.util.StringUtils;


public class ConverterUtil {
/**
* 用来接下来实现内部类的转换,待完善
* */
@Deprecated
public static JSONObject build(Object source, JSONObject dto)
throws Exception {
@SuppressWarnings("rawtypes")
Class sourceClass = (Class) source.getClass();
// 反射无法获得父类,循环获得属性
List<Field> fieldList = new ArrayList<Field>();
while (sourceClass != null) {
fieldList.addAll(Arrays.asList(sourceClass.getDeclaredFields()));
sourceClass = sourceClass.getSuperclass();
}


for (Field field : fieldList) {
field.setAccessible(true);
@SuppressWarnings("unused")
Object obj = field.get(source);
field.getModifiers();// public 1 private 2 protected 4
@SuppressWarnings("rawtypes")
Class fieldType = field.getType();
if (fieldType.isPrimitive()) {
dto.element(field.getName(), field.get(source));
continue;
} else if ("java.lang".equals(fieldType.getPackage().getName())) {
dto.element(field.getName(), field.get(source));
continue;
} else if ("java.util".equals(fieldType.getPackage().getName())) {
if ("class java.util.Date".equals(fieldType.toString())) {
dto.element(field.getName(), StringUtils
.dateToString(((Date) field.get(source))));
}
continue;
} else if ("java.math".equals(fieldType.getPackage().getName())) {
dto.element(field.getName(), field.get(source));
} else if (StringUtils.contains(fieldType.getPackage().getName(),
"com.lvhao")) {
// 可以递归调用,实现内部,不过名字就需要考虑,目前只弄一层吧

}
}
return dto;
}


public static JSONObject beanToDto(Object obj) {
JsonConfig c = new JsonConfig();
c.setExcludes(getExcludeFields(obj));
c.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor(
"yyyy-MM-dd"));
return JSONObject.fromObject(obj, c);
}
/**
* 使用注意:
* 1.实体类需要重写clone方法
* 2.保存使用hibernateTamplate.merge()

* @param dto
* @param target 实体类的clone对象
* @return
*/
public static Object dtoToBean(JSONObject dto, Object target) {
JsonConfig c = new JsonConfig();
c.setExcludes(getExcludeFields(target));
c.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor(
"yyyy-MM-dd"));
c.setRootClass(target.getClass());
return JSONObject.toBean(dto, target, c);
}

        //这里用不到,不用这个方法
public static <M> void merge(M target, M destination) throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(target.getClass());
for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {


if (descriptor.getWriteMethod() != null) {
Object originalValue = descriptor.getReadMethod()
.invoke(target);


if (originalValue == null) {
Object defaultValue = descriptor.getReadMethod().invoke(
destination);
descriptor.getWriteMethod().invoke(target, defaultValue);
}


}
}
}


public static String[] getExcludeFields(Object bean) {
Set<String> list = new HashSet<String>();
list.add("handler");
list.add("hibernateLazyInitializer");
for (Class<?> superClass = bean.getClass(); superClass != Object.class; superClass = superClass
.getSuperclass()) {


Field[] fields = superClass.getDeclaredFields();
for (Field field : fields) {
if (field.getAnnotation(OneToOne.class) != null
|| field.getAnnotation(OneToMany.class) != null
|| field.getAnnotation(ManyToOne.class) != null
|| field.getAnnotation(ManyToMany.class) != null) {
list.add(field.getName());
}
}
}
return list.toArray(new String[list.size()]);
}


}



期间使用过很多的方式,自己写反射等等。填了无数的坑。hibernate的,JsonObject的,辛苦,欢迎各位讨论留言! 分享出去希望大家可以用到!

猜你喜欢

转载自blog.csdn.net/lvhao2813/article/details/80805154