From f0f9ce42f499a3f4eccf53ad0bc2470cfef08a6d Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Fri, 5 Dec 2014 08:28:25 +0000 Subject: [PATCH] using only Jackson iso8601 implementation for UTC date adapter --- .../gson/typeadapters/UtcDateTypeAdapter.java | 42 +------------------ .../typeadapters/UtcDateTypeAdapterTest.java | 15 ++++++- 2 files changed, 15 insertions(+), 42 deletions(-) diff --git a/extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java b/extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java index f4340088..d9269df7 100644 --- a/extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java +++ b/extras/src/main/java/com/google/gson/typeadapters/UtcDateTypeAdapter.java @@ -17,10 +17,8 @@ package com.google.gson.typeadapters; import java.io.IOException; -import java.text.DateFormat; import java.text.ParseException; import java.text.ParsePosition; -import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; @@ -33,42 +31,14 @@ import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonWriter; public final class UtcDateTypeAdapter extends TypeAdapter { - private final DateFormat iso8601Format; private final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC"); - public UtcDateTypeAdapter() { - this(false); - } - - public UtcDateTypeAdapter(boolean jdk6Compatible) { - if (jdk6Compatible) { - this.iso8601Format = null; - } else { - // XXX is only supported by JDK 1.7+ - this.iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US); - this.iso8601Format.setTimeZone(UTC_TIME_ZONE); - } - } - @Override public void write(JsonWriter out, Date date) throws IOException { if (date == null) { out.nullValue(); } - String value = null; - if (iso8601Format != null) { - synchronized (iso8601Format) { - // Need synchronization since JDK DateFormat classes are not thread-safe - try { - value = iso8601Format.format(date); - } catch (Exception e) { - value = null; - } - } - } - if (value == null) { // Try other formatter - value = format(date, true, UTC_TIME_ZONE); - } + String value = format(date, true, UTC_TIME_ZONE); out.value(value); } @@ -81,16 +51,6 @@ public final class UtcDateTypeAdapter extends TypeAdapter { return null; default: String date = in.nextString(); - if (iso8601Format != null) { - synchronized (iso8601Format) { - // Need synchronization since JDK DateFormat classes are not thread-safe - try { - return iso8601Format.parse(date); - } catch (Exception e) { - // Ignore and try the other parser - } - } - } // Instead of using iso8601Format.parse(value), we use Jackson's date parsing // This is because Android doesn't support XXX because it is JDK 1.6 return parse(date, new ParsePosition(0)); diff --git a/extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java b/extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java index 701dd304..c69b4c59 100644 --- a/extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java +++ b/extras/src/test/java/com/google/gson/typeadapters/UtcDateTypeAdapterTest.java @@ -16,8 +16,10 @@ package com.google.gson.typeadapters; +import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; +import java.util.Locale; import java.util.TimeZone; import junit.framework.TestCase; @@ -54,8 +56,19 @@ public final class UtcDateTypeAdapterTest extends TestCase { */ public void testUtcDatesOnJdkBefore1_7() { Gson gson = new GsonBuilder() - .registerTypeAdapter(Date.class, new UtcDateTypeAdapter(true)) + .registerTypeAdapter(Date.class, new UtcDateTypeAdapter()) .create(); gson.fromJson("'2014-12-05T04:00:00.000Z'", Date.class); } + + public void testUtcWithJdk7Default() { + Date expected = new Date(); + SimpleDateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US); + iso8601Format.setTimeZone(TimeZone.getTimeZone("UTC")); + String expectedJson = "\"" + iso8601Format.format(expected) + "\""; + String actualJson = gson.toJson(expected); + assertEquals(expectedJson, actualJson); + Date actual = gson.fromJson(expectedJson, Date.class); + assertEquals(expected.getTime(), actual.getTime()); + } }