diff --git a/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java b/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java index b544740e38..44e0391377 100644 --- a/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java +++ b/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java @@ -854,7 +854,7 @@ public Object handleWeirdKey(Class keyClass, String keyValue, Object key = h.value().handleWeirdKey(this, keyClass, keyValue, msg); if (key != DeserializationProblemHandler.NOT_HANDLED) { // Sanity check for broken handlers, otherwise nasty to debug: - if ((key == null) || keyClass.isInstance(key)) { + if (isHandleSane(keyClass, key)) { return key; } throw weirdStringException(keyValue, keyClass, String.format( @@ -898,7 +898,7 @@ public Object handleWeirdStringValue(Class targetClass, String value, Object instance = h.value().handleWeirdStringValue(this, targetClass, value, msg); if (instance != DeserializationProblemHandler.NOT_HANDLED) { // Sanity check for broken handlers, otherwise nasty to debug: - if ((instance == null) || targetClass.isInstance(instance)) { + if (isHandleSane(targetClass, instance)) { return instance; } throw weirdStringException(value, targetClass, String.format( @@ -941,7 +941,7 @@ public Object handleWeirdNumberValue(Class targetClass, Number value, Object key = h.value().handleWeirdNumberValue(this, targetClass, value, msg); if (key != DeserializationProblemHandler.NOT_HANDLED) { // Sanity check for broken handlers, otherwise nasty to debug: - if ((key == null) || targetClass.isInstance(key)) { + if (isHandleSane(targetClass, key)) { return key; } throw weirdNumberException(value, targetClass, _format( @@ -964,7 +964,7 @@ public Object handleWeirdNativeValue(JavaType targetType, Object badValue, Object goodValue = h.value().handleWeirdNativeValue(this, targetType, badValue, p); if (goodValue != DeserializationProblemHandler.NOT_HANDLED) { // Sanity check for broken handlers, otherwise nasty to debug: - if ((goodValue == null) || raw.isInstance(goodValue)) { + if (isHandleSane(raw, goodValue)) { return goodValue; } throw JsonMappingException.from(p, _format( @@ -1008,7 +1008,7 @@ public Object handleMissingInstantiator(Class instClass, ValueInstantiator va instClass, valueInst, p, msg); if (instance != DeserializationProblemHandler.NOT_HANDLED) { // Sanity check for broken handlers, otherwise nasty to debug: - if ((instance == null) || instClass.isInstance(instance)) { + if (isHandleSane(instClass, instance)) { return instance; } reportBadDefinition(constructType(instClass), String.format( @@ -1058,7 +1058,7 @@ public Object handleInstantiationProblem(Class instClass, Object argument, Object instance = h.value().handleInstantiationProblem(this, instClass, argument, t); if (instance != DeserializationProblemHandler.NOT_HANDLED) { // Sanity check for broken handlers, otherwise nasty to debug: - if ((instance == null) || instClass.isInstance(instance)) { + if (isHandleSane(instClass, instance)) { return instance; } reportBadDefinition(constructType(instClass), String.format( @@ -1117,7 +1117,7 @@ public Object handleUnexpectedToken(Class instClass, JsonToken t, Object instance = h.value().handleUnexpectedToken(this, instClass, t, p, msg); if (instance != DeserializationProblemHandler.NOT_HANDLED) { - if ((instance == null) || instClass.isInstance(instance)) { + if (isHandleSane(instClass, instance)) { return instance; } reportBadDefinition(constructType(instClass), String.format( @@ -1589,4 +1589,20 @@ protected DateFormat getDateFormat() _dateFormat = df = (DateFormat) df.clone(); return df; } + + private boolean isHandleSane(Class targetClass, Object instance) { + return (instance == null) || targetClass.isInstance(instance) || isWrapper(targetClass, instance.getClass()); + } + + private boolean isWrapper(Class targetClass, Class instanceClass) { + return targetClass == Byte.TYPE && instanceClass == Byte.class + || targetClass == Short.TYPE && instanceClass == Short.class + || targetClass == Integer.TYPE && instanceClass == Integer.class + || targetClass == Long.TYPE && instanceClass == Long.class + || targetClass == Float.TYPE && instanceClass == Float.class + || targetClass == Double.TYPE && instanceClass == Double.class + || targetClass == Character.TYPE && instanceClass == Character.class + || targetClass == Boolean.TYPE && instanceClass == Boolean.class; + } + }