Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow DeserializationProblemHandler to respond to primitive types #1767

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}

}