diff --git a/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnClassesTest.java b/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnClassesTest.java index 1ce1bee025..b2b8dc0abf 100644 --- a/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnClassesTest.java +++ b/gson/src/test/java/com/google/gson/functional/JsonAdapterAnnotationOnClassesTest.java @@ -153,8 +153,14 @@ public void testNullSafeObject() { NullableClass fromJson = gson.fromJson("null", NullableClass.class); assertThat(fromJson).isNull(); + fromJson = gson.fromJson("\"ignored\"", NullableClass.class); + assertThat(fromJson).isNotNull(); + String json = gson.toJson(null, NullableClass.class); assertThat(json).isEqualTo("null"); + + json = gson.toJson(new NullableClass()); + assertThat(json).isEqualTo("\"nullable\""); } /** @@ -182,8 +188,8 @@ public void testFactoryReturningNull() { assertThat(deserialized.t).isEqualTo(1); assertThat(gson.toJson(new WithNullReturningFactory<>(2), numberTypeArg.getType())).isEqualTo("{\"t\":2}"); } - // Also set `nullSafe = true` to verify that this does not cause a NullPointerException by calling - // `nullSafe()` on null + // Also set `nullSafe = true` to verify that this does not cause a NullPointerException if the + // factory would accidentally call `nullSafe()` on null adapter @JsonAdapter(value = WithNullReturningFactory.NullReturningFactory.class, nullSafe = true) private static class WithNullReturningFactory { T t; @@ -303,6 +309,7 @@ private static class UserJsonAdapter extends TypeAdapter { } } + // Implicit `nullSafe=true` @JsonAdapter(value = NullableClassJsonAdapter.class) private static class NullableClass { }