Skip to content

Commit

Permalink
Ensuring that UTC date adapter serializes nulls correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
inder123 committed Dec 5, 2014
1 parent f0f9ce4 commit 0c3b967
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

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();
Expand Down Expand Up @@ -71,4 +71,9 @@ public void testUtcWithJdk7Default() {
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);
}
}

0 comments on commit 0c3b967

Please sign in to comment.