fixed gson -> jackson

This commit is contained in:
snoop 2018-04-26 14:13:31 +09:00
parent b48500832b
commit 4ee1df372b
2 changed files with 6 additions and 29 deletions

View File

@ -1,21 +0,0 @@
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<Date> {
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;
}
}

View File

@ -4,12 +4,13 @@
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;
import com.loafle.overflow.model.probe.Probe;
import com.loafle.overflow.service.central.target.TargetDiscoveryService;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -48,18 +49,15 @@
String json = readFileAsString(resourceLoader.getResource("classpath:2018-04-25-tds.json").getURI().getPath());
Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDateDeserializer()).create();
// Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDateDeserializer()).create();
// ObjectMapper mapper = new ObjectMapper();
//
// mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ObjectMapper mapper = new ObjectMapper();
// List<Host> hosts = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, Host.class));
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Type listType = new TypeToken<List<Host>>(){}.getType();
List<Host> hosts = mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, Host.class));
List<Host> hosts = gson.fromJson(json, listType);
Probe probe = new Probe();
probe.setId(1);