Ensuring that UTC date adapter serializes nulls correctly

This commit is contained in:
Inderjeet Singh 2014-12-05 22:12:54 +00:00
parent f0f9ce42f4
commit 0c3b9670f0
2 changed files with 10 additions and 4 deletions

View File

@ -37,10 +37,11 @@ public final class UtcDateTypeAdapter extends TypeAdapter<Date> {
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();
} } else {
String value = format(date, true, UTC_TIME_ZONE); String value = format(date, true, UTC_TIME_ZONE);
out.value(value); out.value(value);
} }
}
@Override @Override
public Date read(JsonReader in) throws IOException { public Date read(JsonReader in) throws IOException {

View File

@ -71,4 +71,9 @@ public final class UtcDateTypeAdapterTest extends TestCase {
Date actual = gson.fromJson(expectedJson, Date.class); Date actual = gson.fromJson(expectedJson, Date.class);
assertEquals(expected.getTime(), actual.getTime()); assertEquals(expected.getTime(), actual.getTime());
} }
public void testNullDateSerialization() {
String json = gson.toJson(null, Date.class);
assertEquals("null", json);
}
} }