Fixed serializers and deserializers for java.sql Date and Time to ensure that Date does't serialize time, and time does't serialize the date portion.

This commit is contained in:
Inderjeet Singh 2009-12-04 18:15:00 +00:00
parent ebf24fbda5
commit 15fa10943c
2 changed files with 98 additions and 32 deletions

View File

@ -24,6 +24,7 @@ import java.net.MalformedURLException;
import java.net.URI; 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.Timestamp; import java.sql.Timestamp;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
@ -56,8 +57,10 @@ import java.util.UUID;
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 DefaultJavaSqlDateDeserializer JAVA_SQL_DATE_DESERIALIZER = private static final DefaultJavaSqlDateTypeAdapter JAVA_SQL_DATE_TYPE_ADAPTER =
new DefaultJavaSqlDateDeserializer(); new DefaultJavaSqlDateTypeAdapter();
private static final DefaultTimeTypeAdapter TIME_TYPE_ADAPTER =
new DefaultTimeTypeAdapter();
private static final DefaultTimestampDeserializer TIMESTAMP_DESERIALIZER = private static final DefaultTimestampDeserializer TIMESTAMP_DESERIALIZER =
new DefaultTimestampDeserializer(); new DefaultTimestampDeserializer();
@ -111,8 +114,9 @@ final class DefaultTypeAdapters {
map.register(Collection.class, COLLECTION_TYPE_ADAPTER); map.register(Collection.class, COLLECTION_TYPE_ADAPTER);
map.register(Map.class, MAP_TYPE_ADAPTER); map.register(Map.class, MAP_TYPE_ADAPTER);
map.register(Date.class, DATE_TYPE_ADAPTER); map.register(Date.class, DATE_TYPE_ADAPTER);
map.register(java.sql.Date.class, JAVA_SQL_DATE_TYPE_ADAPTER);
map.register(Timestamp.class, DATE_TYPE_ADAPTER); map.register(Timestamp.class, DATE_TYPE_ADAPTER);
map.register(java.sql.Date.class, DATE_TYPE_ADAPTER); map.register(Time.class, TIME_TYPE_ADAPTER);
map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER);
map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER);
map.register(BigDecimal.class, BIG_DECIMAL_TYPE_ADAPTER); map.register(BigDecimal.class, BIG_DECIMAL_TYPE_ADAPTER);
@ -147,8 +151,9 @@ final class DefaultTypeAdapters {
map.register(Collection.class, wrapDeserializer(COLLECTION_TYPE_ADAPTER)); map.register(Collection.class, wrapDeserializer(COLLECTION_TYPE_ADAPTER));
map.register(Map.class, wrapDeserializer(MAP_TYPE_ADAPTER)); map.register(Map.class, wrapDeserializer(MAP_TYPE_ADAPTER));
map.register(Date.class, wrapDeserializer(DATE_TYPE_ADAPTER)); map.register(Date.class, wrapDeserializer(DATE_TYPE_ADAPTER));
map.register(java.sql.Date.class, wrapDeserializer(JAVA_SQL_DATE_DESERIALIZER)); map.register(java.sql.Date.class, wrapDeserializer(JAVA_SQL_DATE_TYPE_ADAPTER));
map.register(Timestamp.class, wrapDeserializer(TIMESTAMP_DESERIALIZER)); map.register(Timestamp.class, wrapDeserializer(TIMESTAMP_DESERIALIZER));
map.register(Time.class, wrapDeserializer(TIME_TYPE_ADAPTER));
map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER);
map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER);
map.register(BigDecimal.class, wrapDeserializer(BIG_DECIMAL_TYPE_ADAPTER)); map.register(BigDecimal.class, wrapDeserializer(BIG_DECIMAL_TYPE_ADAPTER));
@ -290,11 +295,33 @@ final class DefaultTypeAdapters {
} }
} }
static class DefaultJavaSqlDateDeserializer implements JsonDeserializer<java.sql.Date> { static class DefaultJavaSqlDateTypeAdapter implements JsonSerializer<java.sql.Date>,
JsonDeserializer<java.sql.Date> {
private final DateFormat format;
DefaultJavaSqlDateTypeAdapter() {
this.format = new SimpleDateFormat("MMM d, yyyy");
}
public JsonElement serialize(java.sql.Date src, Type typeOfSrc,
JsonSerializationContext context) {
synchronized (format) {
String dateFormatAsString = format.format(src);
return new JsonPrimitive(dateFormatAsString);
}
}
public java.sql.Date deserialize(JsonElement json, Type typeOfT, public java.sql.Date deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException { JsonDeserializationContext context) throws JsonParseException {
Date date = context.deserialize(json, Date.class); if (!(json instanceof JsonPrimitive)) {
return new java.sql.Date(date.getTime()); throw new JsonParseException("The date should be a string value");
}
try {
synchronized (format) {
Date date = format.parse(json.getAsString());
return new java.sql.Date(date.getTime());
}
} catch (ParseException e) {
throw new JsonParseException(e);
}
} }
} }
@ -306,6 +333,33 @@ final class DefaultTypeAdapters {
} }
} }
static 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 JsonParseException(e);
}
}
}
private static class GregorianCalendarTypeAdapter private static class GregorianCalendarTypeAdapter
implements JsonSerializer<GregorianCalendar>, JsonDeserializer<GregorianCalendar> { implements JsonSerializer<GregorianCalendar>, JsonDeserializer<GregorianCalendar> {

View File

@ -25,6 +25,7 @@ import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -200,50 +201,61 @@ public class DefaultTypeAdaptersTest extends TestCase {
public void testDefaultDateDeserialization() { public void testDefaultDateDeserialization() {
String json = "'Dec 13, 2009 07:18:02 AM'"; String json = "'Dec 13, 2009 07:18:02 AM'";
Date extracted = gson.fromJson(json, Date.class); Date extracted = gson.fromJson(json, Date.class);
assertEquals(extracted, 2009, 11, 13, 7, 18, 02); assertEqualsDate(extracted, 2009, 11, 13);
assertEqualsTime(extracted, 7, 18, 02);
} }
// Date can not directly be compared with another instance since the deserialization loses the // Date can not directly be compared with another instance since the deserialization loses the
// millisecond portion. // millisecond portion.
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
private void assertEquals(Date date, int year, int month, int day, int hours, int minutes, private void assertEqualsDate(Date date, int year, int month, int day) {
int seconds) { assertEquals(year-1900, date.getYear());
assertEquals(year-1900, date.getYear()); assertEquals(month, date.getMonth());
assertEquals(month, date.getMonth()); assertEquals(day, date.getDate());
assertEquals(day, date.getDate()); }
if (!(date instanceof java.sql.Date)) {
assertEquals(hours, date.getHours()); @SuppressWarnings("deprecation")
assertEquals(minutes, date.getMinutes()); private void assertEqualsTime(Date date, int hours, int minutes, int seconds) {
assertEquals(seconds, date.getSeconds()); assertEquals(hours, date.getHours());
} assertEquals(minutes, date.getMinutes());
assertEquals(seconds, date.getSeconds());
} }
public void testDefaultJavaSqlDateSerialization() { public void testDefaultJavaSqlDateSerialization() {
long currentTimeMillis = System.currentTimeMillis(); java.sql.Date instant = new java.sql.Date(1259875082000L);
java.sql.Date now = new java.sql.Date(currentTimeMillis); String json = gson.toJson(instant);
String json = gson.toJson(now); assertEquals("\"Dec 3, 2009\"", json);
assertEquals("\"" + DateFormat.getDateTimeInstance().format(now) + "\"", json);
} }
@SuppressWarnings("deprecation")
public void testDefaultJavaSqlDateDeserialization() { public void testDefaultJavaSqlDateDeserialization() {
String json = "'Dec 3, 2009 1:18:02 PM'"; String json = "'Dec 3, 2009'";
java.sql.Date extracted = gson.fromJson(json, java.sql.Date.class); java.sql.Date extracted = gson.fromJson(json, java.sql.Date.class);
assertEquals(extracted, 2009, 11, 3, 13, 18, 02); assertEqualsDate(extracted, 2009, 11, 3);
} }
public void testDefaultJavaSqlTimestampSerialization() { public void testDefaultJavaSqlTimestampSerialization() {
long currentTimeMillis = System.currentTimeMillis(); Timestamp now = new java.sql.Timestamp(1259875082000L);
Timestamp now = new java.sql.Timestamp(currentTimeMillis);
String json = gson.toJson(now); String json = gson.toJson(now);
assertEquals("\"" + DateFormat.getDateTimeInstance().format(now) + "\"", json); assertEquals("\"Dec 3, 2009 1:18:02 PM\"", json);
} }
@SuppressWarnings("deprecation")
public void testDefaultJavaSqlTimestampDeserialization() { public void testDefaultJavaSqlTimestampDeserialization() {
String json = "'Dec 3, 2009 1:18:02 PM'"; String json = "'Dec 3, 2009 1:18:02 PM'";
Timestamp extracted = gson.fromJson(json, Timestamp.class); Timestamp extracted = gson.fromJson(json, Timestamp.class);
assertEquals(extracted, 2009, 11, 3, 13, 18, 02); assertEqualsDate(extracted, 2009, 11, 3);
assertEqualsTime(extracted, 13, 18, 02);
}
public void testDefaultJavaSqlTimeSerialization() {
Time now = new Time(1259875082000L);
String json = gson.toJson(now);
assertEquals("\"01:18:02 PM\"", json);
}
public void testDefaultJavaSqlTimeDeserialization() {
String json = "'1:18:02 PM'";
Time extracted = gson.fromJson(json, Time.class);
assertEqualsTime(extracted, 13, 18, 02);
} }
public void testDefaultDateSerializationUsingBuilder() throws Exception { public void testDefaultDateSerializationUsingBuilder() throws Exception {