答えは受け入れられましたが、同様のより拡張可能なソリューションを共有したいと思いました。ここで要点を見つけることができます。
DateDeserializer.java
public class DateDeserializer<T extends Date> implements JsonDeserializer<T> {
private static final String TAG = DateDeserializer.class.getSimpleName();
private final SimpleDateFormat mSimpleDateFormat;
private final Class<T> mClazz;
public DateDeserializer(SimpleDateFormat simpleDateFormat, Class<T> clazz) {
mSimpleDateFormat = simpleDateFormat;
mClazz = clazz;
}
@Override
public T deserialize(JsonElement element, Type arg1, JsonDeserializationContext context) throws JsonParseException {
String dateString = element.getAsString();
try {
T date = mClazz.newInstance();
date.setTime(mSimpleDateFormat.parse(dateString).getTime());
return date;
} catch (InstantiationException e) {
throw new JsonParseException(e.getMessage(), e);
} catch (IllegalAccessException e) {
throw new JsonParseException(e.getMessage(), e);
} catch (ParseException e) {
throw new JsonParseException(e.getMessage(), e);
}
}
}
次に、さまざまな形式を次のように登録します...
sGson = new GsonBuilder()
.registerTypeAdapter(Event.EventDateTime.class,
new DateDeserializer<Event.EventDateTime>(
Event.EventDateTime.DATE_FORMAT, Event.EventDateTime.class))
.registerTypeAdapter(Event.StartEndDateTime.class,
new DateDeserializer<Event.StartEndDateTime>(
Event.StartEndDateTime.DATE_FORMAT, Event.StartEndDateTime.class))
.registerTypeAdapter(Event.SimpleDate.class,
new DateDeserializer<Event.SimpleDate>(
Event.SimpleDate.DATE_FORMAT, Event.SimpleDate.class))
.create();
次に、各形式がクラスにマップされます...
public class Event {
@SerializedName("created")
private EventDateTime mCreated;
//@SerializedName("updated")
private EventDateTime mUpdated;
...
@SerializedName("start")
private ConditionalDateTime mStart;
@SerializedName("end")
private ConditionalDateTime mEnd;
public static class ConditionalDateTime {
@SerializedName("dateTime")
private StartEndDateTime mDateTime;
@SerializedName("date")
private SimpleDate mDate;
public SimpleDate getDate() {
return mDate;
}
public StartEndDateTime getDateTime() {
return mDateTime;
}
/**
* If it is an all day event then only date is populated (not DateTime)
* @return
*/
public boolean isAllDayEvent() {
return mDate != null;
}
}
public static class EventDateTime extends Date {
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
}
public static class StartEndDateTime extends Date {
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZZZZZ");
}
public static class SimpleDate extends java.util.Date {
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
}
}