From 1cce4f9888e337d1cc7b5728ef53cc39ab208c75 Mon Sep 17 00:00:00 2001 From: wangliwen Date: Fri, 13 May 2022 10:23:20 +0800 Subject: [PATCH] =?UTF-8?q?gson=20=E6=97=B6=E9=97=B4=E6=88=B3=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../listener/DemandDataListener.java | 1 - .../listener/CorrectionListener.java | 32 ++++++++++++++++--- .../listener/DataCenterListener.java | 14 ++++++-- .../ResourceUndercarriageListener.java | 9 +++++- .../listener/ResourceOwnerListener.java | 32 ++++++++++++++++--- 5 files changed, 73 insertions(+), 15 deletions(-) diff --git a/renren-admin/src/main/java/io/renren/modules/demanData/listener/DemandDataListener.java b/renren-admin/src/main/java/io/renren/modules/demanData/listener/DemandDataListener.java index bc85748f..40d1824c 100644 --- a/renren-admin/src/main/java/io/renren/modules/demanData/listener/DemandDataListener.java +++ b/renren-admin/src/main/java/io/renren/modules/demanData/listener/DemandDataListener.java @@ -115,7 +115,6 @@ public class DemandDataListener implements TaskListener, ExecutionListener, Acti }); Gson gson = builder.create(); -// Gson gson = new Gson(); JsonElement jsonElement = gson.toJsonTree(kv); TDemandDataDTO demandDataDTO = gson.fromJson(jsonElement, TDemandDataDTO.class); diff --git a/renren-admin/src/main/java/io/renren/modules/processForm/listener/CorrectionListener.java b/renren-admin/src/main/java/io/renren/modules/processForm/listener/CorrectionListener.java index af77b410..5a7f9745 100644 --- a/renren-admin/src/main/java/io/renren/modules/processForm/listener/CorrectionListener.java +++ b/renren-admin/src/main/java/io/renren/modules/processForm/listener/CorrectionListener.java @@ -1,7 +1,6 @@ package io.renren.modules.processForm.listener; -import com.google.gson.Gson; -import com.google.gson.JsonElement; +import com.google.gson.*; import io.renren.modules.activiti.service.ActTaskService; import io.renren.modules.processForm.dto.TAbilityApplicationDTO; import io.renren.modules.processForm.service.ApiGatewayService; @@ -27,6 +26,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import java.lang.reflect.Type; +import java.util.Date; import java.util.Map; import java.util.Optional; import java.util.UUID; @@ -112,7 +113,14 @@ public class CorrectionListener implements TaskListener, ExecutionListener, Acti * @param kv */ private void endTake(Map kv) { // 进入最后结束节点 - Gson gson = new Gson(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Date.class, new JsonDeserializer() { + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return new Date(json.getAsJsonPrimitive().getAsLong()); + } + }); + + Gson gson = builder.create(); JsonElement jsonElement = gson.toJsonTree(kv); TAbilityApplicationDTO abilityApplicationDTO = gson.fromJson(jsonElement, TAbilityApplicationDTO.class); if (abilityApplicationDTO != null) { @@ -130,7 +138,14 @@ public class CorrectionListener implements TaskListener, ExecutionListener, Acti */ private void create(DelegateTask delegateTask) { Map kv = delegateTask.getVariables(); - Gson gson = new Gson(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Date.class, new JsonDeserializer() { + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return new Date(json.getAsJsonPrimitive().getAsLong()); + } + }); + + Gson gson = builder.create(); JsonElement jsonElement = gson.toJsonTree(kv); TAbilityApplicationDTO abilityApplicationDTO = gson.fromJson(jsonElement, TAbilityApplicationDTO.class); @@ -196,7 +211,14 @@ public class CorrectionListener implements TaskListener, ExecutionListener, Acti if (kv.get("gatewayCode") != null || !ActTaskService.Task_HANDLE_STATE_AGREE.equals(kv.get(ActTaskService.Task_HANDLE_STATE))) return; try { - Gson gson = new Gson(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Date.class, new JsonDeserializer() { + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return new Date(json.getAsJsonPrimitive().getAsLong()); + } + }); + + Gson gson = builder.create(); JsonElement jsonElement = gson.toJsonTree(kv); TAbilityApplicationDTO abilityApplicationDTO = gson.fromJson(jsonElement, TAbilityApplicationDTO.class); applyCode(delegateTask, abilityApplicationDTO); diff --git a/renren-admin/src/main/java/io/renren/modules/processForm/listener/DataCenterListener.java b/renren-admin/src/main/java/io/renren/modules/processForm/listener/DataCenterListener.java index 24cf119c..96fa9528 100644 --- a/renren-admin/src/main/java/io/renren/modules/processForm/listener/DataCenterListener.java +++ b/renren-admin/src/main/java/io/renren/modules/processForm/listener/DataCenterListener.java @@ -1,7 +1,6 @@ package io.renren.modules.processForm.listener; -import com.google.gson.Gson; -import com.google.gson.JsonElement; +import com.google.gson.*; import io.renren.modules.processForm.dto.TAbilityApplicationDTO; import io.renren.modules.resource.dto.ResourceDTO; import io.renren.modules.resource.service.ResourceService; @@ -22,6 +21,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import java.lang.reflect.Type; +import java.util.Date; import java.util.Map; import java.util.Optional; @@ -108,7 +109,14 @@ public class DataCenterListener implements TaskListener, ExecutionListener, Acti } Map kv = delegateTask.getVariables(); - Gson gson = new Gson(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Date.class, new JsonDeserializer() { + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return new Date(json.getAsJsonPrimitive().getAsLong()); + } + }); + + Gson gson = builder.create(); JsonElement jsonElement = gson.toJsonTree(kv); TAbilityApplicationDTO abilityApplicationDTO = gson.fromJson(jsonElement, TAbilityApplicationDTO.class); Optional resourceDTOOptional = Optional.ofNullable(resourceService.get(Long.valueOf(abilityApplicationDTO.getResourceId()))); diff --git a/renren-admin/src/main/java/io/renren/modules/resource/listener/ResourceUndercarriageListener.java b/renren-admin/src/main/java/io/renren/modules/resource/listener/ResourceUndercarriageListener.java index d611bbfb..df5b8095 100644 --- a/renren-admin/src/main/java/io/renren/modules/resource/listener/ResourceUndercarriageListener.java +++ b/renren-admin/src/main/java/io/renren/modules/resource/listener/ResourceUndercarriageListener.java @@ -114,7 +114,14 @@ public class ResourceUndercarriageListener implements TaskListener, ExecutionLis */ private void create(DelegateTask delegateTask, final SysRoleDTO roleDTO) { Map kv = delegateTask.getVariables(); - Gson gson = new Gson(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Date.class, new JsonDeserializer() { + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return new Date(json.getAsJsonPrimitive().getAsLong()); + } + }); + + Gson gson = builder.create(); JsonElement jsonElement = gson.toJsonTree(kv); ResourceDTO re = gson.fromJson(jsonElement, ResourceDTO.class); if (re.getDeptId() != null) { diff --git a/renren-admin/src/main/java/io/renren/modules/resourceMountApply/listener/ResourceOwnerListener.java b/renren-admin/src/main/java/io/renren/modules/resourceMountApply/listener/ResourceOwnerListener.java index 5396fa21..462a70b6 100644 --- a/renren-admin/src/main/java/io/renren/modules/resourceMountApply/listener/ResourceOwnerListener.java +++ b/renren-admin/src/main/java/io/renren/modules/resourceMountApply/listener/ResourceOwnerListener.java @@ -1,8 +1,7 @@ package io.renren.modules.resourceMountApply.listener; import com.alibaba.fastjson.JSONObject; -import com.google.gson.Gson; -import com.google.gson.JsonElement; +import com.google.gson.*; import io.renren.modules.processForm.service.ApiGatewayService; import io.renren.modules.resource.dto.ResourceDTO; import io.renren.modules.resource.service.ResourceService; @@ -23,6 +22,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import java.lang.reflect.Type; +import java.util.Date; import java.util.Map; /** @@ -114,7 +115,14 @@ public class ResourceOwnerListener implements TaskListener, ExecutionListener, A * @param kv */ private void endTake(Map kv) { // 进入最后结束节点 - Gson gson = new Gson(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Date.class, new JsonDeserializer() { + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return new Date(json.getAsJsonPrimitive().getAsLong()); + } + }); + + Gson gson = builder.create(); JsonElement jsonElement = gson.toJsonTree(kv); TResourceMountApplyDTO resourceMountApplyDTO = gson.fromJson(jsonElement, TResourceMountApplyDTO.class); ResourceDTO re = resourceMountApplyDTO.getResourceDTO(); @@ -133,7 +141,14 @@ public class ResourceOwnerListener implements TaskListener, ExecutionListener, A */ private void complete(DelegateTask delegateTask) { Map kv = delegateTask.getVariables(); - Gson gson = new Gson(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Date.class, new JsonDeserializer() { + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return new Date(json.getAsJsonPrimitive().getAsLong()); + } + }); + + Gson gson = builder.create(); JsonElement jsonElement = gson.toJsonTree(kv); TResourceMountApplyDTO resourceMountApplyDTO = gson.fromJson(jsonElement, TResourceMountApplyDTO.class); Long resourceID = resourceMountApplyDTO.getResourceDTO().getId(); @@ -155,7 +170,14 @@ public class ResourceOwnerListener implements TaskListener, ExecutionListener, A */ private void create(DelegateTask delegateTask, final SysRoleDTO roleDTO) { Map kv = delegateTask.getVariables(); - Gson gson = new Gson(); + GsonBuilder builder = new GsonBuilder(); + builder.registerTypeAdapter(Date.class, new JsonDeserializer() { + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + return new Date(json.getAsJsonPrimitive().getAsLong()); + } + }); + + Gson gson = builder.create(); JsonElement jsonElement = gson.toJsonTree(kv); TResourceMountApplyDTO resourceMountApplyDTO = gson.fromJson(jsonElement, TResourceMountApplyDTO.class); logger.error(JSONObject.toJSONString(resourceMountApplyDTO));