From 38a61e069d82d0935a1245bf0005e6b4e55fbee9 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Mon, 4 Feb 2019 15:04:32 -0800 Subject: [PATCH] Fix #1675 --- release-notes/VERSION-2.x | 3 ++ .../jackson/databind/ObjectMapper.java | 48 +++++++++---------- .../jackson/databind/ObjectReader.java | 23 ++++++--- 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index e4daee4780..8ebbeb8854 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -6,6 +6,9 @@ Project: jackson-databind 2.10.0 (not yet released) +#1675: Remove "impossible" `IOException` in `readTree()` and `readValue()` `ObjectMapper` + methods which accept Strings + (requested by matthew-pwnieexpress@github) #2059: Remove `final` modifier for `TypeFactory` (requested by Thibaut R) #2115: Support naive deserialization of `Serializable` values as "untyped", same diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java index 1a48ed4bed..259789e3f7 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java +++ b/src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java @@ -2531,8 +2531,14 @@ public JsonNode readTree(Reader r) throws IOException { * Same as {@link #readTree(InputStream)} except content read from * passed-in {@link String} */ - public JsonNode readTree(String content) throws IOException { - return _readTreeAndClose(_jsonFactory.createParser(content)); + public JsonNode readTree(String content) throws JsonProcessingException, JsonMappingException { + try { // since 2.10 remove "impossible" IOException as per [databind#1675] + return _readTreeAndClose(_jsonFactory.createParser(content)); + } catch (JsonProcessingException e) { + throw e; + } catch (IOException e) { // shouldn't really happen but being declared need to + throw JsonMappingException.fromUnexpectedIOE(e); + } } /** @@ -2936,49 +2942,35 @@ public T readValue(URL src, JavaType valueType) /** * Method to deserialize JSON content from given JSON content String. - * - * @throws IOException if a low-level I/O problem (unexpected end-of-input, - * network error) occurs (passed through as-is without additional wrapping -- note - * that this is one case where {@link DeserializationFeature#WRAP_EXCEPTIONS} - * does NOT result in wrapping of exception even if enabled) + * * @throws JsonParseException if underlying input contains invalid content * of type {@link JsonParser} supports (JSON for default case) * @throws JsonMappingException if the input JSON structure does not match structure * expected for result type (or has other mismatch issues) */ - @SuppressWarnings("unchecked") public T readValue(String content, Class valueType) - throws IOException, JsonParseException, JsonMappingException + throws JsonProcessingException, JsonMappingException { - return (T) _readMapAndClose(_jsonFactory.createParser(content), _typeFactory.constructType(valueType)); + return readValue(content, _typeFactory.constructType(valueType)); } /** * Method to deserialize JSON content from given JSON content String. - * - * @throws IOException if a low-level I/O problem (unexpected end-of-input, - * network error) occurs (passed through as-is without additional wrapping -- note - * that this is one case where {@link DeserializationFeature#WRAP_EXCEPTIONS} - * does NOT result in wrapping of exception even if enabled) + * * @throws JsonParseException if underlying input contains invalid content * of type {@link JsonParser} supports (JSON for default case) * @throws JsonMappingException if the input JSON structure does not match structure * expected for result type (or has other mismatch issues) */ - @SuppressWarnings({ "unchecked" }) public T readValue(String content, TypeReference valueTypeRef) - throws IOException, JsonParseException, JsonMappingException + throws JsonProcessingException, JsonMappingException { - return (T) _readMapAndClose(_jsonFactory.createParser(content), _typeFactory.constructType(valueTypeRef)); + return readValue(content, _typeFactory.constructType(valueTypeRef)); } /** * Method to deserialize JSON content from given JSON content String. - * - * @throws IOException if a low-level I/O problem (unexpected end-of-input, - * network error) occurs (passed through as-is without additional wrapping -- note - * that this is one case where {@link DeserializationFeature#WRAP_EXCEPTIONS} - * does NOT result in wrapping of exception even if enabled) + * * @throws JsonParseException if underlying input contains invalid content * of type {@link JsonParser} supports (JSON for default case) * @throws JsonMappingException if the input JSON structure does not match structure @@ -2986,9 +2978,15 @@ public T readValue(String content, TypeReference valueTypeRef) */ @SuppressWarnings("unchecked") public T readValue(String content, JavaType valueType) - throws IOException, JsonParseException, JsonMappingException + throws JsonProcessingException, JsonMappingException { - return (T) _readMapAndClose(_jsonFactory.createParser(content), valueType); + try { // since 2.10 remove "impossible" IOException as per [databind#1675] + return (T) _readMapAndClose(_jsonFactory.createParser(content), valueType); + } catch (JsonProcessingException e) { + throw e; + } catch (IOException e) { // shouldn't really happen but being declared need to + throw JsonMappingException.fromUnexpectedIOE(e); + } } @SuppressWarnings("unchecked") diff --git a/src/main/java/com/fasterxml/jackson/databind/ObjectReader.java b/src/main/java/com/fasterxml/jackson/databind/ObjectReader.java index 9434203ed0..7e41056413 100644 --- a/src/main/java/com/fasterxml/jackson/databind/ObjectReader.java +++ b/src/main/java/com/fasterxml/jackson/databind/ObjectReader.java @@ -1217,13 +1217,18 @@ public T readValue(Reader src) throws IOException * was specified with {@link #withValueToUpdate(Object)}. */ @SuppressWarnings("unchecked") - public T readValue(String src) throws IOException + public T readValue(String src) throws JsonProcessingException, JsonMappingException { if (_dataFormatReaders != null) { _reportUndetectableSource(src); } - - return (T) _bindAndClose(_considerFilter(_parserFactory.createParser(src), false)); + try { // since 2.10 remove "impossible" IOException as per [databind#1675] + return (T) _bindAndClose(_considerFilter(_parserFactory.createParser(src), false)); + } catch (JsonProcessingException e) { + throw e; + } catch (IOException e) { // shouldn't really happen but being declared need to + throw JsonMappingException.fromUnexpectedIOE(e); + } } /** @@ -1361,12 +1366,18 @@ public JsonNode readTree(Reader r) throws IOException * Same as {@link #readTree(InputStream)} except content read from * passed-in {@link String} */ - public JsonNode readTree(String json) throws IOException + public JsonNode readTree(String json) throws JsonProcessingException, JsonMappingException { if (_dataFormatReaders != null) { _reportUndetectableSource(json); } - return _bindAndCloseAsTree(_considerFilter(_parserFactory.createParser(json), false)); + try { // since 2.10 remove "impossible" IOException as per [databind#1675] + return _bindAndCloseAsTree(_considerFilter(_parserFactory.createParser(json), false)); + } catch (JsonProcessingException e) { + throw e; + } catch (IOException e) { // shouldn't really happen but being declared need to + throw JsonMappingException.fromUnexpectedIOE(e); + } } /** @@ -1943,7 +1954,7 @@ protected InputStream _inputStream(File f) throws IOException { return new FileInputStream(f); } - protected void _reportUndetectableSource(Object src) throws JsonProcessingException + protected void _reportUndetectableSource(Object src) throws JsonParseException { // 17-Aug-2015, tatu: Unfortunately, no parser/generator available so: throw new JsonParseException(null, "Cannot use source of type "