Converted java.sql.Timestamp type adapter to the new style.
This commit is contained in:
parent
f9b6c2095f
commit
81854db4ac
|
@ -37,8 +37,6 @@ import com.google.gson.internal.ParameterizedTypeHandlerMap;
|
||||||
final class DefaultTypeAdapters {
|
final class DefaultTypeAdapters {
|
||||||
|
|
||||||
private static final DefaultDateTypeAdapter DATE_TYPE_ADAPTER = new DefaultDateTypeAdapter();
|
private static final DefaultDateTypeAdapter DATE_TYPE_ADAPTER = new DefaultDateTypeAdapter();
|
||||||
private static final DefaultTimestampDeserializer TIMESTAMP_DESERIALIZER =
|
|
||||||
new DefaultTimestampDeserializer();
|
|
||||||
|
|
||||||
// The constants DEFAULT_SERIALIZERS, DEFAULT_DESERIALIZERS, and DEFAULT_INSTANCE_CREATORS
|
// The constants DEFAULT_SERIALIZERS, DEFAULT_DESERIALIZERS, and DEFAULT_INSTANCE_CREATORS
|
||||||
// must be defined after the constants for the type adapters. Otherwise, the type adapter
|
// must be defined after the constants for the type adapters. Otherwise, the type adapter
|
||||||
|
@ -65,7 +63,6 @@ final class DefaultTypeAdapters {
|
||||||
ParameterizedTypeHandlerMap<JsonDeserializer<?>> map =
|
ParameterizedTypeHandlerMap<JsonDeserializer<?>> map =
|
||||||
new ParameterizedTypeHandlerMap<JsonDeserializer<?>>();
|
new ParameterizedTypeHandlerMap<JsonDeserializer<?>>();
|
||||||
map.register(Date.class, wrapDeserializer(DATE_TYPE_ADAPTER), true);
|
map.register(Date.class, wrapDeserializer(DATE_TYPE_ADAPTER), true);
|
||||||
map.register(Timestamp.class, wrapDeserializer(TIMESTAMP_DESERIALIZER), true);
|
|
||||||
|
|
||||||
map.makeUnmodifiable();
|
map.makeUnmodifiable();
|
||||||
return map;
|
return map;
|
||||||
|
@ -169,12 +166,4 @@ final class DefaultTypeAdapters {
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static final class DefaultTimestampDeserializer implements JsonDeserializer<Timestamp> {
|
|
||||||
public Timestamp deserialize(JsonElement json, Type typeOfT,
|
|
||||||
JsonDeserializationContext context) throws JsonParseException {
|
|
||||||
Date date = context.deserialize(json, Date.class);
|
|
||||||
return new Timestamp(date.getTime());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -257,6 +257,7 @@ public final class Gson {
|
||||||
.factory(TypeAdapters.CALENDAR_FACTORY)
|
.factory(TypeAdapters.CALENDAR_FACTORY)
|
||||||
.factory(TypeAdapters.SQL_TIME_FACTORY)
|
.factory(TypeAdapters.SQL_TIME_FACTORY)
|
||||||
.factory(TypeAdapters.SQL_DATE_FACTORY)
|
.factory(TypeAdapters.SQL_DATE_FACTORY)
|
||||||
|
.factory(TypeAdapters.SQL_TIMESTAMP_FACTORY)
|
||||||
.factory(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization))
|
.factory(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization))
|
||||||
.factory(ArrayTypeAdapter.FACTORY)
|
.factory(ArrayTypeAdapter.FACTORY)
|
||||||
.factory(TypeAdapters.ENUM_FACTORY)
|
.factory(TypeAdapters.ENUM_FACTORY)
|
||||||
|
|
|
@ -22,6 +22,7 @@ import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.sql.Time;
|
import java.sql.Time;
|
||||||
|
import java.sql.Timestamp;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
@ -433,6 +434,31 @@ public final class TypeAdapters {
|
||||||
|
|
||||||
public static final TypeAdapter.Factory SQL_TIME_FACTORY = newFactory(Time.class, SQL_TIME);
|
public static final TypeAdapter.Factory SQL_TIME_FACTORY = newFactory(Time.class, SQL_TIME);
|
||||||
|
|
||||||
|
private static final class TimestampTypeAdapter extends TypeAdapter<Timestamp> {
|
||||||
|
private final DateFormat format = new SimpleDateFormat("hh:mm:ss a");
|
||||||
|
private final MiniGson context;
|
||||||
|
public TimestampTypeAdapter(MiniGson context) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public Timestamp read(JsonReader reader) throws IOException {
|
||||||
|
TypeAdapter<Date> dateTypeAdapter = context.getAdapter(Date.class);
|
||||||
|
Date date = dateTypeAdapter.read(reader);
|
||||||
|
return new java.sql.Timestamp(date.getTime());
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void write(JsonWriter writer, Timestamp value) throws IOException {
|
||||||
|
writer.value(format.format(value));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
public static final TypeAdapter.Factory SQL_TIMESTAMP_FACTORY = new TypeAdapter.Factory() {
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> TypeAdapter<T> create(MiniGson context, TypeToken<T> typeToken) {
|
||||||
|
return typeToken.getRawType() == Timestamp.class
|
||||||
|
? (TypeAdapter<T>) new TimestampTypeAdapter(context) : null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public static final TypeAdapter<java.sql.Date> SQL_DATE = new TypeAdapter<java.sql.Date>() {
|
public static final TypeAdapter<java.sql.Date> SQL_DATE = new TypeAdapter<java.sql.Date>() {
|
||||||
private final DateFormat format = new SimpleDateFormat("MMM d, yyyy");
|
private final DateFormat format = new SimpleDateFormat("MMM d, yyyy");
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue
Block a user