Skip to content

Commit

Permalink
Extend test
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcono1234 committed Aug 23, 2023
1 parent e0ebdfa commit e1a0c21
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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\"");
}

/**
Expand Down Expand Up @@ -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 t;
Expand Down Expand Up @@ -303,6 +309,7 @@ private static class UserJsonAdapter extends TypeAdapter<User> {
}
}

// Implicit `nullSafe=true`
@JsonAdapter(value = NullableClassJsonAdapter.class)
private static class NullableClass {
}
Expand Down

0 comments on commit e1a0c21

Please sign in to comment.