using only Jackson iso8601 implementation for UTC date adapter

This commit is contained in:
Inderjeet Singh 2014-12-05 08:28:25 +00:00
parent 0e3708be82
commit f0f9ce42f4
2 changed files with 15 additions and 42 deletions

View File

@ -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<Date> {
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<Date> {
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));

View File

@ -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());
}
}