主要特点是解决timestamp无法序列化的问题
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import com.yunlsp.mixmicro.okr.admin.api.base.service.BaseService;
import com.yunlsp.mixmicro.okr.admin.api.constants.DatePattern;
import javax.validation.constraints.NotNull;
import java.lang.reflect.Type;
import java.util.*;
/**
* {@link BaseService} google gson tools
*
* @author <a href="mailto:tangtongda@gmail.com">Tino.Tang</a>
* @version ${project.version} - 2020/10/19
*/
public class GsonKit {
private static final Gson gson;
static {
// 解决gson格式化日期时,无法序列化timestamp的问题
gson =
new GsonBuilder()
.setDateFormat(DatePattern.NORM_DATETIME_PATTERN)
.registerTypeAdapter(
new TypeToken<HashMap<String, Object>>() {
}.getType(),
(JsonDeserializer<HashMap<String, Object>>)
(json, typeOfT, context) -> {
HashMap<String, Object> hashMap = new HashMap<>();
JsonObject jsonObject = json.getAsJsonObject();
Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
for (Map.Entry<String, JsonElement>