Skip to content

Commit

Permalink
more refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 13, 2020
1 parent 2b0485b commit 1e99a57
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ protected Byte _parseByte(DeserializationContext ctxt, JsonParser p)
return (Byte) getEmptyValue(ctxt);
}
return p.getByteValue();
case JsonTokenId.ID_NULL:
return (Byte) _coerceNullToken(ctxt, false);
case JsonTokenId.ID_NULL: // null fine for non-primitive
return (Byte) getNullValue(ctxt);
case JsonTokenId.ID_NUMBER_INT:
return p.getByteValue();
case JsonTokenId.ID_START_ARRAY:
Expand Down Expand Up @@ -392,8 +392,8 @@ protected Short _parseShort(DeserializationContext ctxt, JsonParser p)
return (Short) getEmptyValue(ctxt);
}
return p.getShortValue();
case JsonTokenId.ID_NULL:
return (Short) _coerceNullToken(ctxt, false);
case JsonTokenId.ID_NULL: // null fine for non-primitive
return (Short) getNullValue(ctxt);
case JsonTokenId.ID_NUMBER_INT:
return p.getShortValue();
case JsonTokenId.ID_START_ARRAY:
Expand Down Expand Up @@ -455,7 +455,10 @@ public Character deserialize(JsonParser p, DeserializationContext ctxt)
}
break;
case JsonTokenId.ID_NULL:
return (Character) _coerceNullToken(ctxt, _primitive);
if (_primitive) {
_verifyNullForPrimitive(ctxt);
}
return (Character) getNullValue(ctxt);
case JsonTokenId.ID_START_ARRAY:
return _deserializeFromArray(p, ctxt);
default:
Expand Down Expand Up @@ -553,8 +556,8 @@ protected final Integer _parseInteger(DeserializationContext ctxt, JsonParser p)
return p.getValueAsInt();
case JsonTokenId.ID_NUMBER_INT: // NOTE: caller assumed to check in fast path
return p.getIntValue();
case JsonTokenId.ID_NULL:
return (Integer) _coerceNullToken(ctxt, false);
case JsonTokenId.ID_NULL: // null fine for non-primitive
return (Integer) getNullValue(ctxt);
case JsonTokenId.ID_START_ARRAY:
return (Integer) _deserializeFromArray(p, ctxt);
}
Expand Down Expand Up @@ -624,8 +627,8 @@ protected final Long _parseLong(DeserializationContext ctxt, JsonParser p)
return (Long) getEmptyValue(ctxt);
}
return p.getValueAsLong();
case JsonTokenId.ID_NULL:
return (Long) _coerceNullToken(ctxt, false);
case JsonTokenId.ID_NULL: // null fine for non-primitive
return (Long) getNullValue(ctxt);
case JsonTokenId.ID_NUMBER_INT:
return p.getLongValue();
case JsonTokenId.ID_START_ARRAY:
Expand Down Expand Up @@ -701,8 +704,8 @@ protected final Float _parseFloat(JsonParser p, DeserializationContext ctxt)
} catch (IllegalArgumentException iae) { }
return (Float) ctxt.handleWeirdStringValue(_valueClass, text,
"not a valid Float value");
case JsonTokenId.ID_NULL:
return (Float) _coerceNullToken(ctxt, _primitive);
case JsonTokenId.ID_NULL: // null fine for non-primitive
return (Float) getNullValue(ctxt);
case JsonTokenId.ID_NUMBER_FLOAT:
case JsonTokenId.ID_NUMBER_INT: // safe coercion
return p.getFloatValue();
Expand Down Expand Up @@ -792,8 +795,8 @@ protected final Double _parseDouble(JsonParser p, DeserializationContext ctxt) t
} catch (IllegalArgumentException iae) { }
return (Double) ctxt.handleWeirdStringValue(_valueClass, text,
"not a valid Double value");
case JsonTokenId.ID_NULL:
return (Double) _coerceNullToken(ctxt, _primitive);
case JsonTokenId.ID_NULL: // null fine for non-primitive
return (Double) getNullValue(ctxt);
case JsonTokenId.ID_NUMBER_FLOAT:
case JsonTokenId.ID_NUMBER_INT: // safe coercion
return p.getDoubleValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,15 +431,6 @@ protected final Boolean _parseBoolean(DeserializationContext ctxt,
throws IOException
{
switch (p.currentTokenId()) {
case JsonTokenId.ID_TRUE:
return true;
case JsonTokenId.ID_FALSE:
return false;
case JsonTokenId.ID_NULL:
return (Boolean) _coerceNullToken(ctxt, false);
case JsonTokenId.ID_NUMBER_INT:
// may accept ints too, (0 == false, otherwise true)
return _coerceBooleanFromInt(ctxt, p, targetType);
case JsonTokenId.ID_STRING:
String text = p.getText();
CoercionAction act = _checkFromStringCoercion(ctxt, text,
Expand All @@ -463,6 +454,15 @@ protected final Boolean _parseBoolean(DeserializationContext ctxt,
}
return (Boolean) ctxt.handleWeirdStringValue(_valueClass, text,
"only \"true\" or \"false\" recognized");
case JsonTokenId.ID_TRUE:
return true;
case JsonTokenId.ID_FALSE:
return false;
case JsonTokenId.ID_NULL: // null fine for non-primitive
return (Boolean) getNullValue(ctxt);
case JsonTokenId.ID_NUMBER_INT:
// may accept ints too, (0 == false, otherwise true)
return _coerceBooleanFromInt(ctxt, p, targetType);
case JsonTokenId.ID_START_ARRAY: // unwrapping / from-empty-array coercion?
return (Boolean) _deserializeFromArray(p, ctxt);
}
Expand Down Expand Up @@ -1211,20 +1211,6 @@ protected Object _coerceIntegral(JsonParser p, DeserializationContext ctxt) thro
return p.getNumberValue(); // should be optimal, whatever it is
}

/**
* Method to call when JSON `null` token is encountered. Note: only called when
* this deserializer encounters it but NOT when reached via property
*
* @since 2.9
*/
protected Object _coerceNullToken(DeserializationContext ctxt, boolean isPrimitive) throws JsonMappingException
{
if (isPrimitive) {
_verifyNullForPrimitive(ctxt);
}
return getNullValue(ctxt);
}

/**
* Method called when JSON String with value "null" is encountered.
*
Expand Down Expand Up @@ -1389,6 +1375,15 @@ protected void _verifyNumberForScalarCoercion(DeserializationContext ctxt, JsonP
}
}

@Deprecated // since 2.12
protected Object _coerceNullToken(DeserializationContext ctxt, boolean isPrimitive) throws JsonMappingException
{
if (isPrimitive) {
_verifyNullForPrimitive(ctxt);
}
return getNullValue(ctxt);
}

/*
/****************************************************
/* Helper methods for sub-classes, resolving dependencies
Expand Down

0 comments on commit 1e99a57

Please sign in to comment.