From b8971008be88a0fefe967e480b1dbc6630c5f9b6 Mon Sep 17 00:00:00 2001 From: snoop Date: Wed, 25 Apr 2018 21:45:11 +0900 Subject: [PATCH] fixed gson date --- .../commons/utils/JsonDateDeserializer.java | 21 +++++++++++++++++++ .../service/TargetDiscoveryServiceTest.java | 6 +++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/loafle/overflow/central/commons/utils/JsonDateDeserializer.java diff --git a/src/main/java/com/loafle/overflow/central/commons/utils/JsonDateDeserializer.java b/src/main/java/com/loafle/overflow/central/commons/utils/JsonDateDeserializer.java new file mode 100644 index 0000000..72e0b0e --- /dev/null +++ b/src/main/java/com/loafle/overflow/central/commons/utils/JsonDateDeserializer.java @@ -0,0 +1,21 @@ +package com.loafle.overflow.central.commons.utils; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; + +import java.lang.reflect.Type; +import java.util.Date; + +/** + * Created by snoop on 18. 4. 25. + */ +public class JsonDateDeserializer implements JsonDeserializer { + public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + String s = json.getAsJsonPrimitive().getAsString(); + long l = Long.parseLong(s.substring(6, s.length() - 2)); + Date d = new Date(l); + return d; + } +} diff --git a/src/test/java/com/loafle/overflow/central/module/target/service/TargetDiscoveryServiceTest.java b/src/test/java/com/loafle/overflow/central/module/target/service/TargetDiscoveryServiceTest.java index d535065..b2e6a26 100644 --- a/src/test/java/com/loafle/overflow/central/module/target/service/TargetDiscoveryServiceTest.java +++ b/src/test/java/com/loafle/overflow/central/module/target/service/TargetDiscoveryServiceTest.java @@ -2,7 +2,9 @@ import com.google.gson.Gson; + import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; + import com.loafle.overflow.central.commons.utils.JsonDateDeserializer; import com.loafle.overflow.central.spring.AppConfigTest; import com.loafle.overflow.model.discovery.Host; @@ -23,6 +25,7 @@ import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; + import java.util.Date; import java.util.List; /** @@ -45,7 +48,8 @@ String json = readFileAsString(resourceLoader.getResource("classpath:2018-04-25-tds.json").getURI().getPath()); - Gson gson = new Gson(); + Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDateDeserializer()).create(); + // ObjectMapper mapper = new ObjectMapper(); //