Converted java.sql.Time type adapter to the new style.

This commit is contained in:
Inderjeet Singh 2011-09-23 18:42:45 +00:00
parent 467011c7ab
commit f9b6c2095f
3 changed files with 23 additions and 32 deletions

View File

@ -17,7 +17,6 @@
package com.google.gson;
import java.lang.reflect.Type;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
@ -38,8 +37,6 @@ import com.google.gson.internal.ParameterizedTypeHandlerMap;
final class DefaultTypeAdapters {
private static final DefaultDateTypeAdapter DATE_TYPE_ADAPTER = new DefaultDateTypeAdapter();
private static final DefaultTimeTypeAdapter TIME_TYPE_ADAPTER =
new DefaultTimeTypeAdapter();
private static final DefaultTimestampDeserializer TIMESTAMP_DESERIALIZER =
new DefaultTimestampDeserializer();
@ -59,7 +56,6 @@ final class DefaultTypeAdapters {
map.register(Date.class, DATE_TYPE_ADAPTER, true);
map.register(Timestamp.class, DATE_TYPE_ADAPTER, true);
map.register(Time.class, TIME_TYPE_ADAPTER, true);
map.makeUnmodifiable();
return map;
@ -70,7 +66,6 @@ final class DefaultTypeAdapters {
new ParameterizedTypeHandlerMap<JsonDeserializer<?>>();
map.register(Date.class, wrapDeserializer(DATE_TYPE_ADAPTER), true);
map.register(Timestamp.class, wrapDeserializer(TIMESTAMP_DESERIALIZER), true);
map.register(Time.class, wrapDeserializer(TIME_TYPE_ADAPTER), true);
map.makeUnmodifiable();
return map;
@ -182,31 +177,4 @@ final class DefaultTypeAdapters {
return new Timestamp(date.getTime());
}
}
static final class DefaultTimeTypeAdapter implements JsonSerializer<Time>, JsonDeserializer<Time> {
private final DateFormat format;
DefaultTimeTypeAdapter() {
this.format = new SimpleDateFormat("hh:mm:ss a");
}
public JsonElement serialize(Time src, Type typeOfSrc, JsonSerializationContext context) {
synchronized (format) {
String dateFormatAsString = format.format(src);
return new JsonPrimitive(dateFormatAsString);
}
}
public Time deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
if (!(json instanceof JsonPrimitive)) {
throw new JsonParseException("The date should be a string value");
}
try {
synchronized (format) {
Date date = format.parse(json.getAsString());
return new Time(date.getTime());
}
} catch (ParseException e) {
throw new JsonSyntaxException(e);
}
}
}
}

View File

@ -255,6 +255,7 @@ public final class Gson {
.factory(TypeAdapters.INET_ADDRESS_FACTORY)
.factory(TypeAdapters.BIT_SET_FACTORY)
.factory(TypeAdapters.CALENDAR_FACTORY)
.factory(TypeAdapters.SQL_TIME_FACTORY)
.factory(TypeAdapters.SQL_DATE_FACTORY)
.factory(new MapTypeAdapterFactory(constructorConstructor, complexMapKeySerialization))
.factory(ArrayTypeAdapter.FACTORY)

View File

@ -21,6 +21,7 @@ import java.net.InetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@ -411,6 +412,27 @@ public final class TypeAdapters {
public static final TypeAdapter.Factory UUID_FACTORY = newFactory(UUID.class, UUID);
public static final TypeAdapter<Time> SQL_TIME = new TypeAdapter<Time>() {
private final DateFormat format = new SimpleDateFormat("hh:mm:ss a");
@Override
public Time read(JsonReader reader) throws IOException {
try {
synchronized (format) {
Date date = format.parse(reader.nextString());
return new java.sql.Time(date.getTime());
}
} catch (ParseException e) {
throw new JsonSyntaxException(e);
}
}
@Override
public void write(JsonWriter writer, Time value) throws IOException {
writer.value(format.format(value));
}
};
public static final TypeAdapter.Factory SQL_TIME_FACTORY = newFactory(Time.class, SQL_TIME);
public static final TypeAdapter<java.sql.Date> SQL_DATE = new TypeAdapter<java.sql.Date>() {
private final DateFormat format = new SimpleDateFormat("MMM d, yyyy");
@Override