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 java.lang.reflect.Type;
import java.util.Date;
public class DbFlareDateDeserializer implements JsonDeserializer
{
@Override
public Object deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException
{
try
{
return new Date(jsonElement.getAsJsonPrimitive().getAsLong()); // Try to parse first if passed value is an epoch time
}
catch (Exception exception1)
{
try
{
return new DateTime(jsonElement.getAsString()).toDate(); // Parse as string following Joda DateTime
}
catch(Exception exception2)
{
}
}
return null;
}
}