Skip to content

Commit

Permalink
Upgrade OptionalXxx deserializers with CoercionConfigs goodness too
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 10, 2020
1 parent 7eddc64 commit e273e92
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -828,29 +828,6 @@ protected CoercionAction _checkFromStringCoercion(DeserializationContext ctxt, S
/* Helper methods for sub-classes, coercions, older (pre-2.12)
/****************************************************
*/
/**
* Method called when JSON String with value "" (that is, zero length) is encountered.
*
* @deprecated Since 2.12
*/
@Deprecated
protected Object _coerceEmptyString(DeserializationContext ctxt, boolean isPrimitive) throws JsonMappingException
{
Enum<?> feat;
boolean enable;

if (!ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS)) {
feat = MapperFeature.ALLOW_COERCION_OF_SCALARS;
enable = true;
} else if (isPrimitive && ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) {
feat = DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES;
enable = false;
} else {
return getNullValue(ctxt);
}
_reportFailedNullCoerce(ctxt, enable, feat, "empty String (\"\")");
return null;
}

protected void _failDoubleToIntCoercion(JsonParser p, DeserializationContext ctxt,
String type) throws IOException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.core.JsonTokenId;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.type.LogicalType;

public class OptionalDoubleDeserializer extends BaseScalarOptionalDeserializer<OptionalDouble>
Expand All @@ -29,16 +30,22 @@ public OptionalDouble deserialize(JsonParser p, DeserializationContext ctxt) thr
}
switch (p.currentTokenId()) {
case JsonTokenId.ID_STRING:
String text = p.getText().trim();
if ((text.length() == 0)) {
_coerceEmptyString(ctxt, false);
return _empty;
{
String text = p.getText();
CoercionAction act = _checkFromStringCoercion(ctxt, text);
if (act == CoercionAction.AsNull) {
return (OptionalDouble) getNullValue(ctxt);
}
if (act == CoercionAction.AsEmpty) {
return (OptionalDouble) getEmptyValue(ctxt);
}
text = text.trim();
if (_hasTextualNull(text)) {
_coerceTextualNull(ctxt, false);
return _empty;
}
return OptionalDouble.of(_parseDoublePrimitive(ctxt, text));
}
if (_hasTextualNull(text)) {
_coerceTextualNull(ctxt, false);
return _empty;
}
return OptionalDouble.of(_parseDoublePrimitive(ctxt, text));
case JsonTokenId.ID_NUMBER_INT: // coercion here should be fine
return OptionalDouble.of(p.getDoubleValue());
case JsonTokenId.ID_NULL:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.core.JsonTokenId;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.type.LogicalType;

public class OptionalIntDeserializer extends BaseScalarOptionalDeserializer<OptionalInt>
Expand All @@ -29,16 +30,22 @@ public OptionalInt deserialize(JsonParser p, DeserializationContext ctxt) throws
}
switch (p.currentTokenId()) {
case JsonTokenId.ID_STRING:
String text = p.getText().trim();
if ((text.length() == 0)) {
_coerceEmptyString(ctxt, false);
return _empty;
{
String text = p.getText();
CoercionAction act = _checkFromStringCoercion(ctxt, text);
if (act == CoercionAction.AsNull) {
return (OptionalInt) getNullValue(ctxt);
}
if (act == CoercionAction.AsEmpty) {
return (OptionalInt) getEmptyValue(ctxt);
}
text = text.trim();
if (_hasTextualNull(text)) {
_coerceTextualNull(ctxt, false);
return _empty;
}
return OptionalInt.of(_parseIntPrimitive(ctxt, text));
}
if (_hasTextualNull(text)) {
_coerceTextualNull(ctxt, false);
return _empty;
}
return OptionalInt.of(_parseIntPrimitive(ctxt, text));
case JsonTokenId.ID_NUMBER_FLOAT:
if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
_failDoubleToIntCoercion(p, ctxt, "int");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.fasterxml.jackson.core.JsonTokenId;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.cfg.CoercionAction;
import com.fasterxml.jackson.databind.type.LogicalType;

public class OptionalLongDeserializer extends BaseScalarOptionalDeserializer<OptionalLong>
Expand All @@ -30,16 +31,22 @@ public OptionalLong deserialize(JsonParser p, DeserializationContext ctxt) throw
}
switch (p.currentTokenId()) {
case JsonTokenId.ID_STRING:
String text = p.getText().trim();
if ((text.length() == 0)) {
_coerceEmptyString(ctxt, false);
return _empty;
{
String text = p.getText();
CoercionAction act = _checkFromStringCoercion(ctxt, text);
if (act == CoercionAction.AsNull) {
return (OptionalLong) getNullValue(ctxt);
}
if (act == CoercionAction.AsEmpty) {
return (OptionalLong) getEmptyValue(ctxt);
}
text = text.trim();
if (_hasTextualNull(text)) {
_coerceTextualNull(ctxt, false);
return _empty;
}
return OptionalLong.of(_parseLongPrimitive(ctxt, text));
}
if (_hasTextualNull(text)) {
_coerceTextualNull(ctxt, false);
return _empty;
}
return OptionalLong.of(_parseLongPrimitive(ctxt, text));
case JsonTokenId.ID_NUMBER_FLOAT:
if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) {
_failDoubleToIntCoercion(p, ctxt, "long");
Expand Down

0 comments on commit e273e92

Please sign in to comment.