using only Jackson iso8601 implementation for UTC date adapter
This commit is contained in:
parent
0e3708be82
commit
f0f9ce42f4
@ -17,10 +17,8 @@
|
|||||||
package com.google.gson.typeadapters;
|
package com.google.gson.typeadapters;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.DateFormat;
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
import java.text.ParsePosition;
|
import java.text.ParsePosition;
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.GregorianCalendar;
|
import java.util.GregorianCalendar;
|
||||||
@ -33,42 +31,14 @@ import com.google.gson.stream.JsonReader;
|
|||||||
import com.google.gson.stream.JsonWriter;
|
import com.google.gson.stream.JsonWriter;
|
||||||
|
|
||||||
public final class UtcDateTypeAdapter extends TypeAdapter<Date> {
|
public final class UtcDateTypeAdapter extends TypeAdapter<Date> {
|
||||||
private final DateFormat iso8601Format;
|
|
||||||
private final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC");
|
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
|
@Override
|
||||||
public void write(JsonWriter out, Date date) throws IOException {
|
public void write(JsonWriter out, Date date) throws IOException {
|
||||||
if (date == null) {
|
if (date == null) {
|
||||||
out.nullValue();
|
out.nullValue();
|
||||||
}
|
}
|
||||||
String value = null;
|
String value = format(date, true, UTC_TIME_ZONE);
|
||||||
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);
|
|
||||||
}
|
|
||||||
out.value(value);
|
out.value(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,16 +51,6 @@ public final class UtcDateTypeAdapter extends TypeAdapter<Date> {
|
|||||||
return null;
|
return null;
|
||||||
default:
|
default:
|
||||||
String date = in.nextString();
|
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
|
// 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
|
// This is because Android doesn't support XXX because it is JDK 1.6
|
||||||
return parse(date, new ParsePosition(0));
|
return parse(date, new ParsePosition(0));
|
||||||
|
@ -16,8 +16,10 @@
|
|||||||
|
|
||||||
package com.google.gson.typeadapters;
|
package com.google.gson.typeadapters;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
import junit.framework.TestCase;
|
||||||
@ -54,8 +56,19 @@ public final class UtcDateTypeAdapterTest extends TestCase {
|
|||||||
*/
|
*/
|
||||||
public void testUtcDatesOnJdkBefore1_7() {
|
public void testUtcDatesOnJdkBefore1_7() {
|
||||||
Gson gson = new GsonBuilder()
|
Gson gson = new GsonBuilder()
|
||||||
.registerTypeAdapter(Date.class, new UtcDateTypeAdapter(true))
|
.registerTypeAdapter(Date.class, new UtcDateTypeAdapter())
|
||||||
.create();
|
.create();
|
||||||
gson.fromJson("'2014-12-05T04:00:00.000Z'", Date.class);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user