Skip to content

Commit

Permalink
using only Jackson iso8601 implementation for UTC date adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
inder123 committed Dec 5, 2014
1 parent 0e3708b commit f0f9ce4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,42 +31,14 @@
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);
}

Expand All @@ -81,16 +51,6 @@ public Date read(JsonReader in) throws IOException {
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,8 +56,19 @@ public void testDifferentTimeZones() {
*/
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());
}
}

0 comments on commit f0f9ce4

Please sign in to comment.