diff --git a/gson/src/main/java/com/google/gson/TypeAdapter.java b/gson/src/main/java/com/google/gson/TypeAdapter.java index ba798537be..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 */ @@ -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(); } @@ -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) { 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);