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(); //