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,9 +37,10 @@ public final class UtcDateTypeAdapter extends TypeAdapter<Date> {
public void write(JsonWriter out, Date date) throws IOException {
if (date == null) {
out.nullValue();
} else {
String value = format(date, true, UTC_TIME_ZONE);
out.value(value);
}
String value = format(date, true, UTC_TIME_ZONE);
out.value(value);
}
@Override

View File

@ -29,8 +29,8 @@ import com.google.gson.GsonBuilder;
public final class UtcDateTypeAdapterTest extends TestCase {
private final Gson gson = new GsonBuilder()
.registerTypeAdapter(Date.class, new UtcDateTypeAdapter())
.create();
.registerTypeAdapter(Date.class, new UtcDateTypeAdapter())
.create();
public void testLocalTimeZone() {
Date expected = new Date();
@ -71,4 +71,9 @@ public final class UtcDateTypeAdapterTest extends TestCase {
Date actual = gson.fromJson(expectedJson, Date.class);
assertEquals(expected.getTime(), actual.getTime());
}
public void testNullDateSerialization() {
String json = gson.toJson(null, Date.class);
assertEquals("null", json);
}
}