11 changed files with 313 additions and 30 deletions
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
package cc.niushuai.dididone.biz.entity; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
import cn.hutool.core.bean.BeanUtil; |
||||
import cn.hutool.core.collection.CollUtil; |
||||
|
||||
public class ProjectRecord extends Project { |
||||
private List<Record> recordList; |
||||
public ProjectRecord(Project project, List<Record> recordList) { |
||||
if (null == project) { |
||||
throw new IllegalArgumentException("project should not be null"); |
||||
} |
||||
|
||||
BeanUtil.copyProperties(project, this); |
||||
|
||||
if (CollUtil.isEmpty(recordList)) { |
||||
this.recordList = new ArrayList<>(0); |
||||
} else { |
||||
this.recordList = recordList; |
||||
} |
||||
} |
||||
|
||||
public List<Record> getRecordList() { |
||||
return recordList; |
||||
} |
||||
|
||||
public void setRecordList(List<Record> recordList) { |
||||
this.recordList = recordList; |
||||
} |
||||
} |
@ -0,0 +1,116 @@
@@ -0,0 +1,116 @@
|
||||
package cc.niushuai.dididone.util; |
||||
|
||||
import com.google.gson.Gson; |
||||
import com.google.gson.GsonBuilder; |
||||
import com.google.gson.reflect.TypeToken; |
||||
|
||||
import java.lang.reflect.Type; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
public class GsonUtil { |
||||
|
||||
//线程安全的
|
||||
private static final Gson GSON; |
||||
private static final Gson GSON_NULL; // 不过滤空值
|
||||
|
||||
static { |
||||
GSON = new GsonBuilder().enableComplexMapKeySerialization() //当Map的key为复杂对象时,需要开启该方法
|
||||
// .serializeNulls() //当字段值为空或null时,依然对该字段进行转换
|
||||
// .excludeFieldsWithoutExposeAnnotation()//打开Export注解,但打开了这个注解,副作用,要转换和不转换都要加注解
|
||||
.setDateFormat("yyyy-MM-dd HH:mm:ss")//序列化日期格式 "yyyy-MM-dd"
|
||||
.setPrettyPrinting() //自动格式化换行
|
||||
.disableHtmlEscaping() //防止特殊字符出现乱码
|
||||
.create(); |
||||
GSON_NULL = new GsonBuilder().enableComplexMapKeySerialization() //当Map的key为复杂对象时,需要开启该方法
|
||||
.serializeNulls() //当字段值为空或null时,依然对该字段进行转换
|
||||
// .excludeFieldsWithoutExposeAnnotation()//打开Export注解,但打开了这个注解,副作用,要转换和不转换都要加注解
|
||||
.setDateFormat("yyyy-MM-dd HH:mm:ss")//序列化日期格式 "yyyy-MM-dd"
|
||||
.setPrettyPrinting() //自动格式化换行
|
||||
.disableHtmlEscaping() //防止特殊字符出现乱码
|
||||
.create(); |
||||
} |
||||
|
||||
//获取gson解析器
|
||||
public static Gson getGson() { |
||||
return GSON; |
||||
} |
||||
|
||||
//获取gson解析器 有空值 解析
|
||||
public static Gson getWriteNullGson() { |
||||
return GSON_NULL; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 根据对象返回json 过滤空值字段 |
||||
*/ |
||||
public static String toJsonStringIgnoreNull(Object object) { |
||||
return GSON.toJson(object); |
||||
} |
||||
|
||||
/** |
||||
* 根据对象返回json 不过滤空值字段 |
||||
*/ |
||||
public static String toJsonString(Object object) { |
||||
return GSON_NULL.toJson(object); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* 将字符串转化对象 |
||||
* |
||||
* @param json 源字符串 |
||||
* @param classOfT 目标对象类型 |
||||
* @param <T> |
||||
* @return |
||||
*/ |
||||
public static <T> T strToJavaBean(String json, Class<T> classOfT) { |
||||
return GSON.fromJson(json, classOfT); |
||||
} |
||||
|
||||
/** |
||||
* 将json转化为对应的实体对象 |
||||
* new TypeToken<List<T>>() {}.getType() |
||||
* new TypeToken<Map<String, T>>() {}.getType() |
||||
* new TypeToken<List<Map<String, T>>>() {}.getType() |
||||
*/ |
||||
public static <T> T fromJson(String json, Type typeOfT) { |
||||
return GSON.fromJson(json, typeOfT); |
||||
} |
||||
|
||||
/** |
||||
* 转成list |
||||
* |
||||
* @param gsonString |
||||
* @param cls |
||||
* @return |
||||
*/ |
||||
public static <T> List<T> strToList(String gsonString, Class<T> cls) { |
||||
return GSON.fromJson(gsonString, new TypeToken<List<T>>() { |
||||
}.getType()); |
||||
} |
||||
|
||||
/** |
||||
* 转成list中有map的 |
||||
* |
||||
* @param gsonString |
||||
* @return |
||||
*/ |
||||
public static <T> List<Map<String, T>> strToListMaps(String gsonString) { |
||||
return GSON.fromJson(gsonString, new TypeToken<List<Map<String, String>>>() { |
||||
}.getType()); |
||||
} |
||||
|
||||
/** |
||||
* 转成map |
||||
* |
||||
* @param gsonString |
||||
* @return |
||||
*/ |
||||
public static <T> Map<String, T> strToMaps(String gsonString) { |
||||
return GSON.fromJson(gsonString, new TypeToken<Map<String, T>>() { |
||||
}.getType()); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue