From 399c52430031ad171c9f1dda5d905a88f449c616 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Sat, 6 Aug 2022 15:13:56 +0200 Subject: [PATCH 1/2] Fix TypeAdapter.toJson throwing AssertionError for custom IOException --- .../java/com/google/gson/TypeAdapter.java | 2 +- .../java/com/google/gson/TypeAdapterTest.java | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/gson/src/main/java/com/google/gson/TypeAdapter.java b/gson/src/main/java/com/google/gson/TypeAdapter.java index ba798537be..72a3b1a21d 100644 --- a/gson/src/main/java/com/google/gson/TypeAdapter.java +++ b/gson/src/main/java/com/google/gson/TypeAdapter.java @@ -216,7 +216,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(); } diff --git a/gson/src/test/java/com/google/gson/TypeAdapterTest.java b/gson/src/test/java/com/google/gson/TypeAdapterTest.java index ab44637393..725ecdae93 100644 --- a/gson/src/test/java/com/google/gson/TypeAdapterTest.java +++ b/gson/src/test/java/com/google/gson/TypeAdapterTest.java @@ -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; @@ -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 adapter = new TypeAdapter() { + @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 adapter = new TypeAdapter() { @Override public void write(JsonWriter out, String value) throws IOException { out.value(value); From c42703d651e20095c64d4717cd859fae3a9a17e1 Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Mon, 8 Aug 2022 01:37:10 +0200 Subject: [PATCH 2/2] Add throws javadoc tag for TypeAdapter methods --- gson/src/main/java/com/google/gson/TypeAdapter.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/gson/src/main/java/com/google/gson/TypeAdapter.java b/gson/src/main/java/com/google/gson/TypeAdapter.java index 72a3b1a21d..37f22b8e22 100644 --- a/gson/src/main/java/com/google/gson/TypeAdapter.java +++ b/gson/src/main/java/com/google/gson/TypeAdapter.java @@ -131,8 +131,7 @@ public abstract class TypeAdapter { * 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 @@ -205,9 +204,9 @@ public final TypeAdapter 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 */ @@ -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) { @@ -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. * @@ -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) {