gson自带的转bean有格式要求,使用很不方便。浅浅的自己写了个支持复杂的类对象和List的方法
分享一下
package com.unary.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.una.comlog.Logger;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
/**
* @author dontmind
* @date 2023/9/25 16:05
*/
public class Json2BeanUtil {
private static Logger logger = Logger.getLogger(Json2BeanUtil.class.getName());
/**
* 读gson任务参数
* */
public ProcessParam getParam(String path){
ProcessParam processParam=new ProcessParam();
String paramStr = processFileUtil.readFile(path);
JsonObject jsonObject =new Gson().fromJson(paramStr, JsonObject.class);
if (Json2BeanUtil.json2Bean(processParam,jsonObject)){
return processParam;
}else {
return null;
}
}
/**
* 复杂gson转bean
* */
public static <T> boolean json2Bean(T bean, JsonObject jsonObject){
try {
Field[] fields = bean.getClass().getDeclaredFields();
for (Field field : fields) {
logger.info("param name:"+field.getName()+",type:"+field.getGenericType().toString());
if (field.getGenericType().toString().equals("int")) {
JsonElement jsonElement=jsonObject.get(field.getName());
if (jsonElement!=null){
field.set(bean,jsonElement.getAsInt());
}
} else if (field.getGenericType().toString().equals("long")) {
JsonElement jsonElement=jsonObject.get(field.getName());
if (jsonElement!=null){
field.set(bean,jsonElement.getAsLong());
}
} else if (field.getGenericType().toString().equals("boolean")) {
JsonElement jsonElement=jsonObject.get(field.getName());
if (jsonElement!=null){
field.set(bean,jsonElement.getAsBoolean());
}
}else if (field.getGenericType().toString().equals("class java.lang.String")||
field.getGenericType().toString().equals("java.lang.String")) {
JsonElement jsonElement=jsonObject.get(field.getName());
if (jsonElement!=null){
field.set(bean,jsonElement.getAsString());
}
}else if (field.getGenericType().toString().contains("java.util.List")) {
JsonElement jsonElement=jsonObject.get(field.getName());
if (jsonElement!=null){
JsonArray jsonArray=jsonElement.getAsJsonArray();
String clazzName=getListClassName(field.getGenericType().toString());
field.set(bean,getListBean(clazzName,jsonArray));
}
}else if (field.getGenericType().toString().contains("class ")){
//自定义bean泛型
String clazzName=getClassname(field.getGenericType().toString());
Object clazz=getClazz(clazzName);
JsonElement jsonElement=jsonObject.get(field.getName());
json2Bean(clazz,jsonElement.getAsJsonObject());
field.set(bean,clazz);
}
}
return true;
}catch (Exception e){
logger.error("error",e);
return false;
}
}
/**
* 依据list类名获取class名称
* */
public static String getListClassName(String clazzName){
String[] a=clazzName.split("<");
String[] b=a[1].split(">");
String beanClazzname=b[0];
return beanClazzname;
}
/**
* 获取类名
* */
public static String getClassname(String clazzName){
String[] a=clazzName.split("class ");
return a[1];
}
/**
* jsonArray转指定bean的list
* */
public static <T> List getListBean(String clazzName,JsonArray jsonArray){
List list=new ArrayList<T>();
for (JsonElement jsonElement:jsonArray){
Object clazz=getClazz(clazzName);
if (jsonElement.isJsonObject()){
json2Bean(clazz,jsonElement.getAsJsonObject());
list.add(clazz);
}else if (jsonElement.isJsonPrimitive()){
if (((JsonPrimitive) jsonElement).isString()){
list.add(jsonElement.getAsString());
}else if (((JsonPrimitive) jsonElement).isBoolean()){
list.add(jsonElement.getAsBoolean());
}else if (((JsonPrimitive) jsonElement).isNumber()){
if (clazzName.equals("int")){
list.add(jsonElement.getAsInt());
}else if (clazzName.equals("long")){
list.add(jsonElement.getAsLong());
}
}
}
}
return list;
}
/**
* 通过类名获取类对象
* */
public static Object getClazz(String clazzName){
try {
return Class.forName(clazzName).newInstance();
} catch (Exception e) {
logger.error("error",e);
return null;
}
}
/**
* gson bean转txt
* */
public static <T> String bean2gsonTxt( T bean,String clazzName){
//processParam json转txt
try {
Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
return gson.toJson(bean, Class.forName(clazzName));
} catch (ClassNotFoundException e) {
e.printStackTrace();
logger.error("error",e);
return null;
}
}
public static <T> JsonObject bean2gson( T bean,String clazzName){
//processParam json转txt
try {
Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
String txt= gson.toJson(bean, Class.forName(clazzName));
JsonObject jsonObject =new Gson().fromJson(txt, JsonObject.class);
return jsonObject;
} catch (ClassNotFoundException e) {
e.printStackTrace();
logger.error("error",e);
return null;
}
}
}