Skip to content

Commit 2666a7f

Browse files
committed
Fixed #2828 (although follow-up clean up needed, so first part)
1 parent 3766578 commit 2666a7f

File tree

10 files changed

+101
-68
lines changed

10 files changed

+101
-68
lines changed

Diff for: release-notes/VERSION-2.x

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Project: jackson-databind
66

77
2.13.0 (not yet released)
88

9-
No changes since 2.12
9+
#2828: Add `DatabindException` as intermediate subtype of `JsonMappingException`
1010

1111
2.12.1 (08-Jan-2021)
1212

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.fasterxml.jackson.databind;
2+
3+
import com.fasterxml.jackson.core.JsonLocation;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
6+
/**
7+
* Intermediate base class for all databind level processing problems, as
8+
* distinct from stream-level problems or I/O issues below.
9+
*<p>
10+
* Added in 2.13 to eventually replace {@link com.fasterxml.jackson.databind.JsonMappingException};
11+
* for 2.x will allow limited use as target (as catching it will also catch mapping exception)
12+
* but will not be constructed or thrown directly.
13+
*
14+
* @since 2.13
15+
*/
16+
public abstract class DatabindException
17+
extends JsonProcessingException
18+
{
19+
private static final long serialVersionUID = 3L;
20+
21+
protected DatabindException(String msg, JsonLocation loc, Throwable rootCause) {
22+
super(msg, loc, rootCause);
23+
}
24+
25+
protected DatabindException(String msg) {
26+
super(msg);
27+
}
28+
29+
protected DatabindException(String msg, JsonLocation loc) {
30+
this(msg, loc, null);
31+
}
32+
33+
protected DatabindException(String msg, Throwable rootCause) {
34+
this(msg, null, rootCause);
35+
}
36+
}

Diff for: src/main/java/com/fasterxml/jackson/databind/JsonMappingException.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
* troubleshooting.
2222
*/
2323
public class JsonMappingException
24-
extends JsonProcessingException
24+
extends DatabindException // @since 2.13
2525
{
26-
private static final long serialVersionUID = 1L;
26+
private static final long serialVersionUID = 3L;
2727

2828
/**
2929
* Let's limit length of reference chain, to limit damage in cases

Diff for: src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java

+39-39
Large diffs are not rendered by default.

Diff for: src/main/java/com/fasterxml/jackson/databind/ObjectReader.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2276,7 +2276,7 @@ protected void _reportUndetectableSource(Object src) throws StreamReadException
22762276
* Method called to locate deserializer for the passed root-level value.
22772277
*/
22782278
protected JsonDeserializer<Object> _findRootDeserializer(DeserializationContext ctxt)
2279-
throws JsonMappingException
2279+
throws DatabindException
22802280
{
22812281
if (_rootDeserializer != null) {
22822282
return _rootDeserializer;
@@ -2306,7 +2306,7 @@ protected JsonDeserializer<Object> _findRootDeserializer(DeserializationContext
23062306
* @since 2.6
23072307
*/
23082308
protected JsonDeserializer<Object> _findTreeDeserializer(DeserializationContext ctxt)
2309-
throws JsonMappingException
2309+
throws DatabindException
23102310
{
23112311
final JavaType nodeType = _jsonNodeType();
23122312
JsonDeserializer<Object> deser = _rootDeserializers.get(nodeType);

Diff for: src/main/java/com/fasterxml/jackson/databind/ObjectWriter.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ public void writeValue(JsonGenerator g, Object value) throws IOException
10221022
* JSON output, written to File provided.
10231023
*/
10241024
public void writeValue(File resultFile, Object value)
1025-
throws IOException, StreamWriteException, JsonMappingException
1025+
throws IOException, StreamWriteException, DatabindException
10261026
{
10271027
_writeValueAndClose(createGenerator(resultFile, JsonEncoding.UTF8), value);
10281028
}
@@ -1039,7 +1039,7 @@ public void writeValue(File resultFile, Object value)
10391039
* is closed).
10401040
*/
10411041
public void writeValue(OutputStream out, Object value)
1042-
throws IOException, StreamWriteException, JsonMappingException
1042+
throws IOException, StreamWriteException, DatabindException
10431043
{
10441044
_writeValueAndClose(createGenerator(out, JsonEncoding.UTF8), value);
10451045
}
@@ -1055,7 +1055,7 @@ public void writeValue(OutputStream out, Object value)
10551055
* is closed).
10561056
*/
10571057
public void writeValue(Writer w, Object value)
1058-
throws IOException, StreamWriteException, JsonMappingException
1058+
throws IOException, StreamWriteException, DatabindException
10591059
{
10601060
_writeValueAndClose(createGenerator(w), value);
10611061
}
@@ -1064,7 +1064,7 @@ public void writeValue(Writer w, Object value)
10641064
* @since 2.8
10651065
*/
10661066
public void writeValue(DataOutput out, Object value)
1067-
throws IOException, StreamWriteException, JsonMappingException
1067+
throws IOException, StreamWriteException, DatabindException
10681068
{
10691069
_writeValueAndClose(createGenerator(out), value);
10701070
}
@@ -1135,7 +1135,8 @@ public byte[] writeValueAsBytes(Object value)
11351135
*
11361136
* @since 2.2
11371137
*/
1138-
public void acceptJsonFormatVisitor(JavaType type, JsonFormatVisitorWrapper visitor) throws JsonMappingException
1138+
public void acceptJsonFormatVisitor(JavaType type, JsonFormatVisitorWrapper visitor)
1139+
throws JsonMappingException
11391140
{
11401141
_assertNotNull("type", type);
11411142
_assertNotNull("visitor", visitor);
@@ -1145,7 +1146,9 @@ public void acceptJsonFormatVisitor(JavaType type, JsonFormatVisitorWrapper visi
11451146
/**
11461147
* Since 2.6
11471148
*/
1148-
public void acceptJsonFormatVisitor(Class<?> type, JsonFormatVisitorWrapper visitor) throws JsonMappingException {
1149+
public void acceptJsonFormatVisitor(Class<?> type, JsonFormatVisitorWrapper visitor)
1150+
throws JsonMappingException
1151+
{
11491152
_assertNotNull("type", type);
11501153
_assertNotNull("visitor", visitor);
11511154
acceptJsonFormatVisitor(_config.constructType(type), visitor);
@@ -1482,7 +1485,7 @@ public Prefetch forRootType(ObjectWriter parent, JavaType newType) {
14821485
((TypeWrappedSerializer) ser).typeSerializer());
14831486
}
14841487
return new Prefetch(newType, ser, null);
1485-
} catch (JsonMappingException e) {
1488+
} catch (DatabindException e) {
14861489
// need to swallow?
14871490
;
14881491
}

Diff for: src/test/java/com/fasterxml/jackson/databind/deser/jdk/CollectionDeserTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -276,18 +276,18 @@ public void testWrapExceptions() throws Exception
276276

277277
try {
278278
mapper.readValue("[{}]", new TypeReference<List<SomeObject>>() {});
279-
} catch (JsonMappingException exc) {
279+
} catch (DatabindException exc) {
280280
assertEquals("I want to catch this exception", exc.getOriginalMessage());
281281
} catch (RuntimeException exc) {
282-
fail("The RuntimeException should have been wrapped with a JsonMappingException.");
282+
fail("The RuntimeException should have been wrapped with a DatabindException.");
283283
}
284284

285285
ObjectMapper mapperNoWrap = new ObjectMapper();
286286
mapperNoWrap.disable(DeserializationFeature.WRAP_EXCEPTIONS);
287287

288288
try {
289289
mapperNoWrap.readValue("[{}]", new TypeReference<List<SomeObject>>() {});
290-
} catch (JsonMappingException exc) {
290+
} catch (DatabindException exc) {
291291
fail("It should not have wrapped the RuntimeException.");
292292
} catch (RuntimeException exc) {
293293
assertEquals("I want to catch this exception", exc.getMessage());

Diff for: src/test/java/com/fasterxml/jackson/databind/seq/ReadRecoveryTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void testRootBeans() throws Exception
3434
try {
3535
bean = it.nextValue();
3636
fail("Should not have succeeded");
37-
} catch (JsonMappingException e) {
37+
} catch (DatabindException e) {
3838
verifyException(e, "Unrecognized field \"x\"");
3939
}
4040
// 21-May-2015, tatu: With [databind#734], recovery, we now know there's no more data!
@@ -59,7 +59,7 @@ public void testSimpleRootRecovery() throws Exception
5959
// second one problematic
6060
try {
6161
it.nextValue();
62-
} catch (JsonMappingException e) {
62+
} catch (DatabindException e) {
6363
verifyException(e, "Unrecognized field \"foo\"");
6464
}
6565

@@ -88,7 +88,7 @@ public void testSimpleArrayRecovery() throws Exception
8888
// second one problematic
8989
try {
9090
it.nextValue();
91-
} catch (JsonMappingException e) {
91+
} catch (DatabindException e) {
9292
verifyException(e, "Unrecognized field \"foo\"");
9393
}
9494

Diff for: src/test/java/com/fasterxml/jackson/databind/struct/TestUnwrappedWithTypeInfo.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
package com.fasterxml.jackson.databind.struct;
22

3-
import com.fasterxml.jackson.annotation.JsonProperty;
4-
import com.fasterxml.jackson.annotation.JsonTypeInfo;
5-
import com.fasterxml.jackson.annotation.JsonTypeName;
6-
import com.fasterxml.jackson.annotation.JsonUnwrapped;
7-
import com.fasterxml.jackson.databind.BaseMapTest;
8-
import com.fasterxml.jackson.databind.JsonMappingException;
9-
import com.fasterxml.jackson.databind.MapperFeature;
10-
import com.fasterxml.jackson.databind.ObjectMapper;
11-
import com.fasterxml.jackson.databind.SerializationFeature;
3+
import com.fasterxml.jackson.annotation.*;
4+
5+
import com.fasterxml.jackson.databind.*;
126

137
// Tests for [#81]
148
public class TestUnwrappedWithTypeInfo extends BaseMapTest
@@ -63,7 +57,7 @@ public void testDefaultUnwrappedWithTypeInfo() throws Exception
6357
try {
6458
mapper.writeValueAsString(outer);
6559
fail("Expected exception to be thrown.");
66-
} catch (JsonMappingException ex) {
60+
} catch (DatabindException ex) {
6761
verifyException(ex, "requires use of type information");
6862
}
6963
}

Diff for: src/test/java/com/fasterxml/jackson/failing/TestUnwrappedWithUnknown650.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public void testFailOnUnknownPropertyUnwrapped() throws Exception
2424
try {
2525
MAPPER.readValue(aposToQuotes(JSON), A.class);
2626
fail("Exception was not thrown on unkown property");
27-
} catch (JsonMappingException e) {
27+
} catch (DatabindException e) {
2828
verifyException(e, "Unrecognized field");
2929
}
3030
}

0 commit comments

Comments
 (0)