Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions gson/src/main/java/com/google/gson/TypeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,24 +289,34 @@ public final T fromJsonTree(JsonElement jsonTree) {
* Note that we didn't need to check for nulls in our type adapter after we used nullSafe.
*/
public final TypeAdapter<T> nullSafe() {
return new TypeAdapter<T>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
if (value == null) {
out.nullValue();
} else {
TypeAdapter.this.write(out, value);
}
if (!(this instanceof TypeAdapter.NullSafeTypeAdapter)) {
return new NullSafeTypeAdapter();
}
return this;
}

private final class NullSafeTypeAdapter extends TypeAdapter<T> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As suggested in #2729 (comment), do you want to take the chance and implement toString as well. I think at least some other adapters implement it, and it would then make it easier to see which adapter is wrapped by this one.

@lyubomyr-shaydariv lyubomyr-shaydariv Sep 1, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Marcono1234

Oh, I seem to have misterstood your toString() suggestion earlier leaving it out of scope (I mistakenly assumed it leads to a stack overflow error, but it doesn't).

Could you please suggest the NullSafeTypeAdapter.toString() pattern you'd like to see?

@lyubomyr-shaydariv lyubomyr-shaydariv Sep 2, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As suggested in #2729 (comment), do you want to take the chance and implement toString as well. I think at least some other adapters implement it, and it would then make it easier to see which adapter is wrapped by this one.

I applied the NullSafeTypeAdapter[%s] pattern you suggested in #2729 (comment).

@Override
public void write(JsonWriter out, T value) throws IOException {
if (value == null) {
out.nullValue();
} else {
TypeAdapter.this.write(out, value);
}
}

@Override
public T read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
return TypeAdapter.this.read(reader);
@Override
public T read(JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
}
};
return TypeAdapter.this.read(reader);
}

@Override
public String toString() {
return "NullSafeTypeAdapter[" + TypeAdapter.this + "]";
}
}
}
51 changes: 39 additions & 12 deletions gson/src/test/java/com/google/gson/TypeAdapterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,50 @@
public class TypeAdapterTest {
@Test
public void testNullSafe() throws IOException {
TypeAdapter<String> adapter =
new TypeAdapter<String>() {
@Override
public void write(JsonWriter out, String value) {
throw new AssertionError("unexpected call");
}

@Override
public String read(JsonReader in) {
throw new AssertionError("unexpected call");
}
}.nullSafe();
TypeAdapter<String> adapter = assertionErrorAdapter.nullSafe();

assertThat(adapter.toJson(null)).isEqualTo("null");
assertThat(adapter.fromJson("null")).isNull();
}

@Test
public void testNullSafe_ReturningSameInstanceOnceNullSafe() {
TypeAdapter<?> nullSafeAdapter = assertionErrorAdapter.nullSafe();

assertThat(nullSafeAdapter.nullSafe()).isSameInstanceAs(nullSafeAdapter);
assertThat(nullSafeAdapter.nullSafe().nullSafe()).isSameInstanceAs(nullSafeAdapter);
assertThat(nullSafeAdapter.nullSafe().nullSafe().nullSafe()).isSameInstanceAs(nullSafeAdapter);
}

@Test
public void testNullSafe_ToString() {
TypeAdapter<?> adapter = assertionErrorAdapter;

assertThat(adapter.toString()).isEqualTo("assertionErrorAdapter");
assertThat(adapter.nullSafe().toString())
.isEqualTo("NullSafeTypeAdapter[assertionErrorAdapter]");
assertThat(adapter.nullSafe().nullSafe().toString())
.isEqualTo("NullSafeTypeAdapter[assertionErrorAdapter]");
}

private static final TypeAdapter<String> assertionErrorAdapter =
new TypeAdapter<>() {
@Override
public void write(JsonWriter out, String value) {
throw new AssertionError("unexpected call");
}

@Override
public String read(JsonReader in) {
throw new AssertionError("unexpected call");
}

@Override
public String toString() {
return "assertionErrorAdapter";
}
};

/**
* Tests behavior when {@link TypeAdapter#write(JsonWriter, Object)} manually throws {@link
* IOException} which is not caused by writer usage.
Expand Down