Converted Date type adapter to the new style. This was the last remaining old style default type adapter.

This commit is contained in:
Inderjeet Singh 2011-09-25 21:51:28 +00:00
parent 81854db4ac
commit c1bac6debf
3 changed files with 65 additions and 29 deletions

View File

@ -36,8 +36,6 @@ import com.google.gson.internal.ParameterizedTypeHandlerMap;
*/
final class DefaultTypeAdapters {
private static final DefaultDateTypeAdapter DATE_TYPE_ADAPTER = new DefaultDateTypeAdapter();
// The constants DEFAULT_SERIALIZERS, DEFAULT_DESERIALIZERS, and DEFAULT_INSTANCE_CREATORS
// must be defined after the constants for the type adapters. Otherwise, the type adapter
// constants will appear as nulls.
@ -51,10 +49,6 @@ final class DefaultTypeAdapters {
private static ParameterizedTypeHandlerMap<JsonSerializer<?>> createDefaultSerializers() {
ParameterizedTypeHandlerMap<JsonSerializer<?>> map =
new ParameterizedTypeHandlerMap<JsonSerializer<?>>();
map.register(Date.class, DATE_TYPE_ADAPTER, true);
map.register(Timestamp.class, DATE_TYPE_ADAPTER, true);
map.makeUnmodifiable();
return map;
}
@ -62,8 +56,6 @@ final class DefaultTypeAdapters {
private static ParameterizedTypeHandlerMap<JsonDeserializer<?>> createDefaultDeserializers() {
ParameterizedTypeHandlerMap<JsonDeserializer<?>> map =
new ParameterizedTypeHandlerMap<JsonDeserializer<?>>();
map.register(Date.class, wrapDeserializer(DATE_TYPE_ADAPTER), true);
map.makeUnmodifiable();
return map;
}
@ -75,11 +67,6 @@ final class DefaultTypeAdapters {
return map;
}
@SuppressWarnings("unchecked")
private static JsonDeserializer<?> wrapDeserializer(JsonDeserializer<?> deserializer) {
return new JsonDeserializerExceptionWrapper(deserializer);
}
/**
* This type adapter supports three subclasses of date: Date, Timestamp, and
* java.sql.Date.

View File

@ -16,6 +16,21 @@
package com.google.gson;
import java.io.EOFException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import com.google.gson.internal.ConstructorConstructor;
import com.google.gson.internal.ParameterizedTypeHandlerMap;
import com.google.gson.internal.Primitives;
@ -36,20 +51,6 @@ import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.google.gson.stream.MalformedJsonException;
import java.io.EOFException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* This is the main class for using Gson. Gson is typically used by first constructing a
@ -254,6 +255,7 @@ public final class Gson {
.factory(TypeAdapters.LOCALE_FACTORY)
.factory(TypeAdapters.INET_ADDRESS_FACTORY)
.factory(TypeAdapters.BIT_SET_FACTORY)
.factory(TypeAdapters.DATE_FACTORY)
.factory(TypeAdapters.CALENDAR_FACTORY)
.factory(TypeAdapters.SQL_TIME_FACTORY)
.factory(TypeAdapters.SQL_DATE_FACTORY)

View File

@ -32,6 +32,7 @@ import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.TimeZone;
import java.util.UUID;
import com.google.gson.JsonIOException;
@ -411,6 +412,52 @@ public final class TypeAdapters {
}
};
private static DateFormat buildIso8601Format() {
DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC"));
return iso8601Format;
}
public static final TypeAdapter<Date> DATE = new TypeAdapter<Date>() {
private final DateFormat enUsFormat =
DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US);
private final DateFormat localFormat =
DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT);
private final DateFormat iso8601Format = buildIso8601Format();
@Override
public Date read(JsonReader reader) throws IOException {
return deserializeToDate(reader.nextString());
}
private Date deserializeToDate(String json) {
synchronized (localFormat) {
try {
return localFormat.parse(json);
} catch (ParseException ignored) {
}
try {
return enUsFormat.parse(json);
} catch (ParseException ignored) {
}
try {
return iso8601Format.parse(json);
} catch (ParseException e) {
throw new JsonSyntaxException(json, e);
}
}
}
@Override
public void write(JsonWriter writer, Date value) throws IOException {
synchronized (localFormat) {
String dateFormatAsString = enUsFormat.format(value);
writer.value(dateFormatAsString);
}
}
};
public static final TypeAdapter.Factory DATE_FACTORY = newFactory(Date.class, DATE);
public static final TypeAdapter.Factory UUID_FACTORY = newFactory(UUID.class, UUID);
public static final TypeAdapter<Time> SQL_TIME = new TypeAdapter<Time>() {
@ -435,7 +482,6 @@ public final class TypeAdapters {
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;
@ -448,7 +494,8 @@ public final class TypeAdapters {
}
@Override
public void write(JsonWriter writer, Timestamp value) throws IOException {
writer.value(format.format(value));
TypeAdapter<Date> dateTypeAdapter = context.getAdapter(Date.class);
dateTypeAdapter.write(writer, value);
}
};
public static final TypeAdapter.Factory SQL_TIMESTAMP_FACTORY = new TypeAdapter.Factory() {