From 15fa10943c6e9db6d5db147efdec9640e86838a9 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 4 Dec 2009 18:15:00 +0000 Subject: [PATCH] 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. --- .../com/google/gson/DefaultTypeAdapters.java | 72 ++++++++++++++++--- .../functional/DefaultTypeAdaptersTest.java | 58 +++++++++------ 2 files changed, 98 insertions(+), 32 deletions(-) diff --git a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java index 6552815f..4a4011fa 100644 --- a/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java +++ b/gson/src/main/java/com/google/gson/DefaultTypeAdapters.java @@ -24,6 +24,7 @@ import java.net.MalformedURLException; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; +import java.sql.Time; import java.sql.Timestamp; import java.text.DateFormat; import java.text.ParseException; @@ -56,8 +57,10 @@ import java.util.UUID; final class DefaultTypeAdapters { private static final DefaultDateTypeAdapter DATE_TYPE_ADAPTER = new DefaultDateTypeAdapter(); - private static final DefaultJavaSqlDateDeserializer JAVA_SQL_DATE_DESERIALIZER = - new DefaultJavaSqlDateDeserializer(); + private static final DefaultJavaSqlDateTypeAdapter JAVA_SQL_DATE_TYPE_ADAPTER = + new DefaultJavaSqlDateTypeAdapter(); + private static final DefaultTimeTypeAdapter TIME_TYPE_ADAPTER = + new DefaultTimeTypeAdapter(); private static final DefaultTimestampDeserializer TIMESTAMP_DESERIALIZER = new DefaultTimestampDeserializer(); @@ -111,8 +114,9 @@ final class DefaultTypeAdapters { map.register(Collection.class, COLLECTION_TYPE_ADAPTER); map.register(Map.class, MAP_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(java.sql.Date.class, DATE_TYPE_ADAPTER); + map.register(Time.class, TIME_TYPE_ADAPTER); map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_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(Map.class, wrapDeserializer(MAP_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(Time.class, wrapDeserializer(TIME_TYPE_ADAPTER)); map.register(Calendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); map.register(GregorianCalendar.class, GREGORIAN_CALENDAR_TYPE_ADAPTER); map.register(BigDecimal.class, wrapDeserializer(BIG_DECIMAL_TYPE_ADAPTER)); @@ -290,12 +295,34 @@ final class DefaultTypeAdapters { } } - static class DefaultJavaSqlDateDeserializer implements JsonDeserializer { + static class DefaultJavaSqlDateTypeAdapter implements JsonSerializer, + JsonDeserializer { + 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, JsonDeserializationContext context) throws JsonParseException { - Date date = context.deserialize(json, Date.class); - return new java.sql.Date(date.getTime()); - } + 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 java.sql.Date(date.getTime()); + } + } catch (ParseException e) { + throw new JsonParseException(e); + } + } } static class DefaultTimestampDeserializer implements JsonDeserializer { @@ -303,7 +330,34 @@ final class DefaultTypeAdapters { JsonDeserializationContext context) throws JsonParseException { Date date = context.deserialize(json, Date.class); return new Timestamp(date.getTime()); - } + } + } + + static class DefaultTimeTypeAdapter implements JsonSerializer