Implement [JACKSON-758]
diff --git a/release-notes/VERSION b/release-notes/VERSION
index d23b20e..404bc7d 100644
--- a/release-notes/VERSION
+++ b/release-notes/VERSION
@@ -13,6 +13,9 @@
Improvements:
+* [JACKSON-758]: Remove 'IOException' from throws clauses of "writeValueAsString"
+ and "writeValueAsBytes" of ObjectMapper/ObjectWriter
+ (suggested by G-T Chen)
* [JACKSON-839]: Allow "upgrade" of integer number types for
UntypedObjectDeserializer, even with default typing enabled.
* [JACKSON-850]: Allow use of zero-arg factory methods as "default creator"
diff --git a/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java b/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java
index 5050b21..8f935cb 100644
--- a/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java
+++ b/src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java
@@ -1,5 +1,6 @@
package com.fasterxml.jackson.databind;
+import java.io.IOException;
import java.io.Serializable;
import java.util.*;
@@ -170,6 +171,19 @@
}
/**
+ * Factory method used when "upgrading" an {@link IOException} into
+ * {@link JsonMappingException}: usually only needed to comply with
+ * a signature.
+ *
+ * @since 2.1
+ */
+ public static JsonMappingException fromUnexpectedIOE(IOException src)
+ {
+ return new JsonMappingException("Unexpected IOException (of type "
+ +src.getClass().getName()+"): "+src.getMessage(), (JsonLocation)null, src);
+ }
+
+ /**
* Method that can be called to either create a new JsonMappingException
* (if underlying exception is not a JsonMappingException), or augment
* given exception with given path/reference information.
@@ -220,7 +234,7 @@
jme.prependPath(ref);
return jme;
}
-
+
/*
/**********************************************************
/* Accessors/mutators
diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
index 08dadcb..5d5dfd8 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
@@ -2004,13 +2004,21 @@
* a String. Functionally equivalent to calling
* {@link #writeValue(Writer,Object)} with {@link java.io.StringWriter}
* and constructing String, but more efficient.
+ *<p>
+ * Note: prior to version 2.1, throws clause included {@link IOException}; 2.1 removed it.
*/
public String writeValueAsString(Object value)
- throws IOException, JsonGenerationException, JsonMappingException
+ throws JsonProcessingException
{
// alas, we have to pull the recycler directly here...
SegmentedStringWriter sw = new SegmentedStringWriter(_jsonFactory._getBufferRecycler());
- _configAndWriteValue(_jsonFactory.createJsonGenerator(sw), value);
+ try {
+ _configAndWriteValue(_jsonFactory.createJsonGenerator(sw), value);
+ } catch (JsonProcessingException e) { // to support [JACKSON-758]
+ throw e;
+ } catch (IOException e) { // shouldn't really happen, but is declared as possibility so:
+ throw JsonMappingException.fromUnexpectedIOE(e);
+ }
return sw.getAndClear();
}
@@ -2020,12 +2028,20 @@
* {@link #writeValue(Writer,Object)} with {@link java.io.ByteArrayOutputStream}
* and getting bytes, but more efficient.
* Encoding used will be UTF-8.
+ *<p>
+ * Note: prior to version 2.1, throws clause included {@link IOException}; 2.1 removed it.
*/
public byte[] writeValueAsBytes(Object value)
- throws IOException, JsonGenerationException, JsonMappingException
- {
+ throws JsonProcessingException
+ {
ByteArrayBuilder bb = new ByteArrayBuilder(_jsonFactory._getBufferRecycler());
- _configAndWriteValue(_jsonFactory.createJsonGenerator(bb, JsonEncoding.UTF8), value);
+ try {
+ _configAndWriteValue(_jsonFactory.createJsonGenerator(bb, JsonEncoding.UTF8), value);
+ } catch (JsonProcessingException e) { // to support [JACKSON-758]
+ throw e;
+ } catch (IOException e) { // shouldn't really happen, but is declared as possibility so:
+ throw JsonMappingException.fromUnexpectedIOE(e);
+ }
byte[] result = bb.toByteArray();
bb.release();
return result;
diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java b/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java
index 2e01809..307683a 100644
--- a/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java
+++ b/src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java
@@ -500,13 +500,21 @@
* a String. Functionally equivalent to calling
* {@link #writeValue(Writer,Object)} with {@link java.io.StringWriter}
* and constructing String, but more efficient.
+ *<p>
+ * Note: prior to version 2.1, throws clause included {@link IOException}; 2.1 removed it.
*/
public String writeValueAsString(Object value)
- throws IOException, JsonGenerationException, JsonMappingException
+ throws JsonProcessingException
{
// alas, we have to pull the recycler directly here...
SegmentedStringWriter sw = new SegmentedStringWriter(_jsonFactory._getBufferRecycler());
- _configAndWriteValue(_jsonFactory.createJsonGenerator(sw), value);
+ try {
+ _configAndWriteValue(_jsonFactory.createJsonGenerator(sw), value);
+ } catch (JsonProcessingException e) { // to support [JACKSON-758]
+ throw e;
+ } catch (IOException e) { // shouldn't really happen, but is declared as possibility so:
+ throw JsonMappingException.fromUnexpectedIOE(e);
+ }
return sw.getAndClear();
}
@@ -516,12 +524,20 @@
* {@link #writeValue(Writer,Object)} with {@link java.io.ByteArrayOutputStream}
* and getting bytes, but more efficient.
* Encoding used will be UTF-8.
+ *<p>
+ * Note: prior to version 2.1, throws clause included {@link IOException}; 2.1 removed it.
*/
public byte[] writeValueAsBytes(Object value)
- throws IOException, JsonGenerationException, JsonMappingException
- {
+ throws JsonProcessingException
+ {
ByteArrayBuilder bb = new ByteArrayBuilder(_jsonFactory._getBufferRecycler());
- _configAndWriteValue(_jsonFactory.createJsonGenerator(bb, JsonEncoding.UTF8), value);
+ try {
+ _configAndWriteValue(_jsonFactory.createJsonGenerator(bb, JsonEncoding.UTF8), value);
+ } catch (JsonProcessingException e) { // to support [JACKSON-758]
+ throw e;
+ } catch (IOException e) { // shouldn't really happen, but is declared as possibility so:
+ throw JsonMappingException.fromUnexpectedIOE(e);
+ }
byte[] result = bb.toByteArray();
bb.release();
return result;