Skip to content

Commit 48437a7

Browse files
committed
More rerouting of exceptions through DeserializationContext.reportXxx() instead of direct throws
1 parent 4b9bfd0 commit 48437a7

16 files changed

+117
-60
lines changed

src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -852,18 +852,6 @@ public void reportUnknownProperty(Object instanceOrClass, String fieldName,
852852
instanceOrClass, fieldName, propIds);
853853
}
854854

855-
/**
856-
* @since 2.8
857-
*/
858-
public void reportMappingException(String msg, Object... msgArgs)
859-
throws JsonMappingException
860-
{
861-
if (msgArgs.length > 0) {
862-
msg = String.format(msg, msgArgs);
863-
}
864-
throw mappingException(msg);
865-
}
866-
867855
/**
868856
* @since 2.8
869857
*/
@@ -954,6 +942,32 @@ public void reportEndOfInputException(Class<?> instClass) throws JsonMappingExce
954942
throw endOfInputException(instClass);
955943
}
956944

945+
/**
946+
* @since 2.8
947+
*/
948+
public void reportMappingException(String msg, Object... msgArgs)
949+
throws JsonMappingException
950+
{
951+
if (msgArgs.length > 0) {
952+
msg = String.format(msg, msgArgs);
953+
}
954+
throw mappingException(msg);
955+
}
956+
957+
/**
958+
* @since 2.8
959+
*/
960+
public void reportMappingException(Class<?> targetClass, JsonToken t) throws JsonMappingException {
961+
throw mappingException(targetClass, t);
962+
}
963+
964+
/**
965+
* @since 2.8
966+
*/
967+
public void reportMappingException(Class<?> targetClass) throws JsonMappingException {
968+
throw mappingException(targetClass);
969+
}
970+
957971
/*
958972
/**********************************************************
959973
/* Methods for constructing exceptions, "untyped"
@@ -967,16 +981,20 @@ public JsonMappingException mappingException(Class<?> targetClass) {
967981
return mappingException(targetClass, _parser.getCurrentToken());
968982
}
969983

984+
/**
985+
* @deprecated Since 2.8 use {@link #reportMappingException(String, Object...)} instead
986+
*/
987+
@Deprecated
970988
public JsonMappingException mappingException(Class<?> targetClass, JsonToken token) {
971989
return JsonMappingException.from(_parser,
972990
String.format("Can not deserialize instance of %s out of %s token",
973991
_calcName(targetClass), token));
974992
}
975-
993+
976994
/**
977-
* Helper method for constructing generic mapping exception with specified
978-
* message and current location information
995+
* @deprecated Since 2.8 use {@link #reportMappingException(String, Object...)} instead
979996
*/
997+
@Deprecated
980998
public JsonMappingException mappingException(String message) {
981999
return JsonMappingException.from(getParser(), message);
9821000
}

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializer.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ protected final Object _deserializeOther(JsonParser p, DeserializationContext ct
168168
return deserializeFromObject(p, ctxt);
169169
default:
170170
}
171-
throw ctxt.mappingException(handledType());
171+
ctxt.reportMappingException(handledType());
172+
return null;
172173
}
173174

174175
protected Object _missingToken(JsonParser p, DeserializationContext ctxt) throws IOException {
@@ -691,7 +692,8 @@ protected Object deserializeUsingPropertyBasedWithUnwrapped(JsonParser p, Deseri
691692
// !!! 08-Jul-2011, tatu: Could probably support; but for now
692693
// it's too complicated, so bail out
693694
tokens.close();
694-
throw ctxt.mappingException("Can not create polymorphic instances with unwrapped values");
695+
ctxt.reportMappingException("Can not create polymorphic instances with unwrapped values");
696+
return null;
695697
}
696698
return _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
697699
}
@@ -856,7 +858,8 @@ protected Object deserializeUsingPropertyBasedWithExternalTypeId(JsonParser p, D
856858
if (bean.getClass() != _beanType.getRawClass()) {
857859
// !!! 08-Jul-2011, tatu: Could theoretically support; but for now
858860
// it's too complicated, so bail out
859-
throw ctxt.mappingException("Can not create polymorphic instances with unwrapped values");
861+
ctxt.reportMappingException("Can not create polymorphic instances with unwrapped values");
862+
return null;
860863
}
861864
return ext.complete(p, ctxt, bean);
862865
}

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializerBase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1299,7 +1299,8 @@ public Object deserializeFromArray(JsonParser p, DeserializationContext ctxt) th
12991299
if (t == JsonToken.END_ARRAY) {
13001300
return null;
13011301
}
1302-
throw ctxt.mappingException(handledType(), JsonToken.START_ARRAY);
1302+
ctxt.reportMappingException(handledType(), JsonToken.START_ARRAY);
1303+
return null;
13031304
}
13041305
throw ctxt.mappingException(handledType());
13051306
}

src/main/java/com/fasterxml/jackson/databind/deser/BuilderBasedDeserializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,8 @@ protected Object deserializeUsingPropertyBasedWithUnwrapped(JsonParser p,
590590
if (bean.getClass() != _beanType.getRawClass()) {
591591
// !!! 08-Jul-2011, tatu: Could probably support; but for now
592592
// it's too complicated, so bail out
593-
throw ctxt.mappingException("Can not create polymorphic instances with unwrapped values");
593+
ctxt.reportMappingException("Can not create polymorphic instances with unwrapped values");
594+
return null;
594595
}
595596
return _unwrappedPropertyHandler.processUnwrapped(p, ctxt, bean, tokens);
596597
}

src/main/java/com/fasterxml/jackson/databind/deser/impl/FailingDeserializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public FailingDeserializer(String m) {
2424

2525
@Override
2626
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws JsonMappingException{
27-
throw ctxt.mappingException(_message);
27+
ctxt.reportMappingException(_message);
28+
return null;
2829
}
2930
}

src/main/java/com/fasterxml/jackson/databind/deser/std/JsonNodeDeserializer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ protected final ObjectNode deserializeObject(JsonParser p, DeserializationContex
212212
return node;
213213
}
214214
if (t != JsonToken.FIELD_NAME) {
215-
throw ctxt.mappingException(handledType(), p.getCurrentToken());
215+
ctxt.reportMappingException(handledType(), p.getCurrentToken());
216+
return null;
216217
}
217218
key = p.getCurrentName();
218219
}
@@ -263,7 +264,8 @@ protected final ArrayNode deserializeArray(JsonParser p, DeserializationContext
263264
while (true) {
264265
JsonToken t = p.nextToken();
265266
if (t == null) {
266-
throw ctxt.mappingException("Unexpected end-of-input when binding data into ArrayNode");
267+
ctxt.reportEndOfInputException(ArrayNode.class);
268+
return node;
267269
}
268270
switch (t.id()) {
269271
case JsonTokenId.ID_START_OBJECT:

src/main/java/com/fasterxml/jackson/databind/deser/std/MapDeserializer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ protected final void _readAndBind(JsonParser p, DeserializationContext ctxt,
426426
return;
427427
}
428428
if (t != JsonToken.FIELD_NAME) {
429-
throw ctxt.mappingException(_mapType.getRawClass(), p.getCurrentToken());
429+
ctxt.reportMappingException(_mapType.getRawClass(), p.getCurrentToken());
430+
return;
430431
}
431432
keyStr = p.getCurrentName();
432433
}
@@ -487,7 +488,8 @@ protected final void _readAndBindStringMap(JsonParser p, DeserializationContext
487488
return;
488489
}
489490
if (t != JsonToken.FIELD_NAME) {
490-
throw ctxt.mappingException(_mapType.getRawClass(), p.getCurrentToken());
491+
ctxt.reportMappingException(_mapType.getRawClass(), p.getCurrentToken());
492+
return;
491493
}
492494
key = p.getCurrentName();
493495
}

src/main/java/com/fasterxml/jackson/databind/deser/std/MapEntryDeserializer.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public Map.Entry<Object,Object> deserialize(JsonParser jp, DeserializationContex
173173
// Ok: must point to START_OBJECT, FIELD_NAME or END_OBJECT
174174
JsonToken t = jp.getCurrentToken();
175175
if (t != JsonToken.START_OBJECT && t != JsonToken.FIELD_NAME && t != JsonToken.END_OBJECT) {
176-
// [JACKSON-620] (empty) String may be ok however:
176+
// String may be ok however:
177177
// slightly redundant (since String was passed above), but
178178
return _deserializeFromEmpty(jp, ctxt);
179179
}
@@ -182,9 +182,11 @@ public Map.Entry<Object,Object> deserialize(JsonParser jp, DeserializationContex
182182
}
183183
if (t != JsonToken.FIELD_NAME) {
184184
if (t == JsonToken.END_OBJECT) {
185-
throw ctxt.mappingException("Can not deserialize a Map.Entry out of empty JSON Object");
185+
ctxt.reportMappingException("Can not deserialize a Map.Entry out of empty JSON Object");
186+
return null;
186187
}
187-
throw ctxt.mappingException(handledType(), t);
188+
ctxt.reportMappingException(handledType(), t);
189+
return null;
188190
}
189191

190192
final KeyDeserializer keyDes = _keyDeserializer;
@@ -213,10 +215,12 @@ public Map.Entry<Object,Object> deserialize(JsonParser jp, DeserializationContex
213215
t = jp.nextToken();
214216
if (t != JsonToken.END_OBJECT) {
215217
if (t == JsonToken.FIELD_NAME) { // most likely
216-
throw ctxt.mappingException("Problem binding JSON into Map.Entry: more than one entry in JSON (second field: '"+jp.getCurrentName()+"')");
218+
ctxt.reportMappingException("Problem binding JSON into Map.Entry: more than one entry in JSON (second field: '"+jp.getCurrentName()+"')");
219+
} else {
220+
// how would this occur?
221+
ctxt.reportMappingException("Problem binding JSON into Map.Entry: unexpected content after JSON Object entry: "+t);
217222
}
218-
// how would this occur?
219-
throw ctxt.mappingException("Problem binding JSON into Map.Entry: unexpected content after JSON Object entry: "+t);
223+
return null;
220224
}
221225
return new AbstractMap.SimpleEntry<Object,Object>(key, value);
222226
}

src/main/java/com/fasterxml/jackson/databind/deser/std/NumberDeserializers.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ public Character deserialize(JsonParser p, DeserializationContext ctxt)
284284
return C;
285285
}
286286
}
287-
throw ctxt.mappingException(_valueClass, p.getCurrentToken());
287+
ctxt.reportMappingException(_valueClass, p.getCurrentToken());
288+
return null;
288289
}
289290
}
290291

@@ -490,7 +491,8 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx
490491
break;
491492
}
492493
// Otherwise, no can do:
493-
throw ctxt.mappingException(_valueClass, p.getCurrentToken());
494+
ctxt.reportMappingException(_valueClass, p.getCurrentToken());
495+
return null;
494496
}
495497

496498
/**
@@ -576,7 +578,8 @@ public BigInteger deserialize(JsonParser p, DeserializationContext ctxt) throws
576578
}
577579
}
578580
// String is ok too, can easily convert; otherwise, no can do:
579-
throw ctxt.mappingException(_valueClass, p.getCurrentToken());
581+
ctxt.reportMappingException(_valueClass, p.getCurrentToken());
582+
return null;
580583
}
581584
}
582585

@@ -620,7 +623,8 @@ public BigDecimal deserialize(JsonParser p, DeserializationContext ctxt)
620623
break;
621624
}
622625
// Otherwise, no can do:
623-
throw ctxt.mappingException(_valueClass, p.getCurrentToken());
626+
ctxt.reportMappingException(_valueClass, p.getCurrentToken());
627+
return null;
624628
}
625629
}
626630
}

src/main/java/com/fasterxml/jackson/databind/deser/std/StackTraceElementDeserializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public StackTraceElement deserialize(JsonParser p, DeserializationContext ctxt)
5454
}
5555
return value;
5656
}
57-
throw ctxt.mappingException(_valueClass, t);
57+
ctxt.reportMappingException(_valueClass, t);
58+
return null;
5859
}
5960
}

0 commit comments

Comments
 (0)