Skip to content

Commit

Permalink
Fix TypeAdapter.toJson throwing AssertionError for custom IOExcepti…
Browse files Browse the repository at this point in the history
…on (#2172)

* Fix TypeAdapter.toJson throwing AssertionError for custom IOException

* Add throws javadoc tag for TypeAdapter methods
  • Loading branch information
Marcono1234 committed Aug 8, 2022
1 parent d53b3ea commit 6fc1c8f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
13 changes: 7 additions & 6 deletions gson/src/main/java/com/google/gson/TypeAdapter.java
Expand Up @@ -131,8 +131,7 @@ public abstract class TypeAdapter<T> {
* Unlike Gson's similar {@link Gson#toJson(JsonElement, Appendable) toJson}
* method, this write is strict. Create a {@link
* JsonWriter#setLenient(boolean) lenient} {@code JsonWriter} and call
* {@link #write(com.google.gson.stream.JsonWriter, Object)} for lenient
* writing.
* {@link #write(JsonWriter, Object)} for lenient writing.
*
* @param value the Java object to convert. May be null.
* @since 2.2
Expand Down Expand Up @@ -205,9 +204,9 @@ public final TypeAdapter<T> nullSafe() {
* Converts {@code value} to a JSON document. Unlike Gson's similar {@link
* Gson#toJson(Object) toJson} method, this write is strict. Create a {@link
* JsonWriter#setLenient(boolean) lenient} {@code JsonWriter} and call
* {@link #write(com.google.gson.stream.JsonWriter, Object)} for lenient
* writing.
* {@link #write(JsonWriter, Object)} for lenient writing.
*
* @throws JsonIOException wrapping {@code IOException}s thrown by {@link #write(JsonWriter, Object)}
* @param value the Java object to convert. May be null.
* @since 2.2
*/
Expand All @@ -216,7 +215,7 @@ public final String toJson(T value) {
try {
toJson(stringWriter, value);
} catch (IOException e) {
throw new AssertionError(e); // No I/O writing to a StringWriter.
throw new JsonIOException(e);
}
return stringWriter.toString();
}
Expand All @@ -226,6 +225,7 @@ public final String toJson(T value) {
*
* @param value the Java object to convert. May be null.
* @return the converted JSON tree. May be {@link JsonNull}.
* @throws JsonIOException wrapping {@code IOException}s thrown by {@link #write(JsonWriter, Object)}
* @since 2.2
*/
public final JsonElement toJsonTree(T value) {
Expand All @@ -248,7 +248,7 @@ public final JsonElement toJsonTree(T value) {

/**
* Converts the JSON document in {@code in} to a Java object. Unlike Gson's
* similar {@link Gson#fromJson(java.io.Reader, Class) fromJson} method, this
* similar {@link Gson#fromJson(Reader, Class) fromJson} method, this
* read is strict. Create a {@link JsonReader#setLenient(boolean) lenient}
* {@code JsonReader} and call {@link #read(JsonReader)} for lenient reading.
*
Expand Down Expand Up @@ -284,6 +284,7 @@ public final T fromJson(String json) throws IOException {
*
* @param jsonTree the JSON element to convert. May be {@link JsonNull}.
* @return the converted Java object. May be null.
* @throws JsonIOException wrapping {@code IOException}s thrown by {@link #read(JsonReader)}
* @since 2.2
*/
public final T fromJsonTree(JsonElement jsonTree) {
Expand Down
33 changes: 33 additions & 0 deletions gson/src/test/java/com/google/gson/TypeAdapterTest.java
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
Expand All @@ -26,6 +27,38 @@ public void testNullSafe() throws IOException {
assertNull(adapter.fromJson("null"));
}

/**
* Tests behavior when {@link TypeAdapter#write(JsonWriter, Object)} manually throws
* {@link IOException} which is not caused by writer usage.
*/
@Test
public void testToJson_ThrowingIOException() {
final IOException exception = new IOException("test");
TypeAdapter<Integer> adapter = new TypeAdapter<Integer>() {
@Override public void write(JsonWriter out, Integer value) throws IOException {
throw exception;
}

@Override public Integer read(JsonReader in) throws IOException {
throw new AssertionError("not needed by this test");
}
};

try {
adapter.toJson(1);
fail();
} catch (JsonIOException e) {
assertEquals(exception, e.getCause());
}

try {
adapter.toJsonTree(1);
fail();
} catch (JsonIOException e) {
assertEquals(exception, e.getCause());
}
}

private static final TypeAdapter<String> adapter = new TypeAdapter<String>() {
@Override public void write(JsonWriter out, String value) throws IOException {
out.value(value);
Expand Down

0 comments on commit 6fc1c8f

Please sign in to comment.