com.droidlogix.dbflare.a2e.DbFlareDateDeserializer Maven / Gradle / Ivy
package com.droidlogix.dbflare.a2e;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Type;
import java.util.Date;
public class DbFlareDateDeserializer implements JsonDeserializer
{
private static final Logger logger = LoggerFactory.getLogger(DbFlareDateDeserializer.class);
@Override
public Object deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException
{
try
{
return new Date(jsonElement.getAsJsonPrimitive().getAsLong());
}
catch (Exception exception1)
{
logger.error(exception1.getMessage());
try
{
return new DateTime(jsonElement.getAsString()).toDate();
}
catch(Exception exception2)
{
logger.error(exception2.getMessage());
}
}
return null;
}
}