From 7a9fd5962dce7f277efa15fcc996606be0733bac Mon Sep 17 00:00:00 2001 From: Lyubomyr Shaydariv Date: Mon, 18 Sep 2017 09:49:13 +0300 Subject: [PATCH] Fixed DefaultDateTypeAdapter nullability issue and JSON primitives contract (#1100) * Fixed DefaultDateTypeAdapter nullability issue and JSON primitives contract Regression in: * b8f616c939c652b8540c95fa2b377b8c628ef3ff - Migrate DefaultDateTypeAdapter to streaming adapter (#1070) Bug reports: * https://github.com/google/gson/issues/1096 - 2.8.1 can't serialize and deserialize date null (2.8.0 works fine) * https://github.com/google/gson/issues/1098 - Gson 2.8.1 DefaultDateTypeAdapter is not null safe. * Fixed DefaultDateTypeAdapter nullability on write --- .../com/google/gson/DefaultDateTypeAdapter.java | 9 +++++++-- .../google/gson/DefaultDateTypeAdapterTest.java | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/gson/src/main/java/com/google/gson/DefaultDateTypeAdapter.java b/gson/src/main/java/com/google/gson/DefaultDateTypeAdapter.java index 95eb42be..3ce97fe8 100644 --- a/gson/src/main/java/com/google/gson/DefaultDateTypeAdapter.java +++ b/gson/src/main/java/com/google/gson/DefaultDateTypeAdapter.java @@ -84,6 +84,10 @@ final class DefaultDateTypeAdapter extends TypeAdapter { // See issue 162 @Override public void write(JsonWriter out, Date value) throws IOException { + if (value == null) { + out.nullValue(); + return; + } synchronized (localFormat) { String dateFormatAsString = enUsFormat.format(value); out.value(dateFormatAsString); @@ -92,8 +96,9 @@ final class DefaultDateTypeAdapter extends TypeAdapter { @Override public Date read(JsonReader in) throws IOException { - if (in.peek() != JsonToken.STRING) { - throw new JsonParseException("The date should be a string value"); + if (in.peek() == JsonToken.NULL) { + in.nextNull(); + return null; } Date date = deserializeToDate(in.nextString()); if (dateType == Date.class) { diff --git a/gson/src/test/java/com/google/gson/DefaultDateTypeAdapterTest.java b/gson/src/test/java/com/google/gson/DefaultDateTypeAdapterTest.java index b3ee5a6f..3c787a66 100644 --- a/gson/src/test/java/com/google/gson/DefaultDateTypeAdapterTest.java +++ b/gson/src/test/java/com/google/gson/DefaultDateTypeAdapterTest.java @@ -161,6 +161,20 @@ public class DefaultDateTypeAdapterTest extends TestCase { } catch (IllegalArgumentException expected) { } } + public void testNullValue() throws Exception { + DefaultDateTypeAdapter adapter = new DefaultDateTypeAdapter(Date.class); + assertNull(adapter.fromJson("null")); + assertEquals("null", adapter.toJson(null)); + } + + public void testUnexpectedToken() throws Exception { + try { + DefaultDateTypeAdapter adapter = new DefaultDateTypeAdapter(Date.class); + adapter.fromJson("{}"); + fail("Unexpected token should fail."); + } catch (IllegalStateException expected) { } + } + private void assertFormatted(String formatted, DefaultDateTypeAdapter adapter) { assertEquals(toLiteral(formatted), adapter.toJson(new Date(0))); }