diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/NumberDeserializers.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/NumberDeserializers.java index a3a1cf98e0..f43c27bbc5 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/std/NumberDeserializers.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/NumberDeserializers.java @@ -244,8 +244,7 @@ protected final Boolean _parseBoolean(JsonParser p, DeserializationContext ctxt) } // And finally, let's allow Strings to be converted too if (t == JsonToken.VALUE_STRING) { - final String text = p.getText(); - + String text = p.getText(); CoercionAction act = _checkFromStringCoercion(ctxt, text); if (act == CoercionAction.AsNull) { return (Boolean) getNullValue(ctxt); @@ -253,6 +252,7 @@ protected final Boolean _parseBoolean(JsonParser p, DeserializationContext ctxt) if (act == CoercionAction.AsEmpty) { return (Boolean) getEmptyValue(ctxt); } + text = text.trim(); // [databind#422]: Allow aliases if ("true".equals(text) || "True".equals(text)) { return Boolean.TRUE; @@ -303,7 +303,7 @@ protected Byte _parseByte(JsonParser p, DeserializationContext ctxt) throws IOEx { JsonToken t = p.currentToken(); if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse - final String text = p.getText(); + String text = p.getText(); CoercionAction act = _checkFromStringCoercion(ctxt, text); if (act == CoercionAction.AsNull) { return (Byte) getNullValue(ctxt); @@ -311,6 +311,7 @@ protected Byte _parseByte(JsonParser p, DeserializationContext ctxt) throws IOEx if (act == CoercionAction.AsEmpty) { return (Byte) getEmptyValue(ctxt); } + text = text.trim(); if (_hasTextualNull(text)) { return (Byte) _coerceTextualNull(ctxt, _primitive); } @@ -376,7 +377,7 @@ protected Short _parseShort(JsonParser p, DeserializationContext ctxt) throws IO return p.getShortValue(); } if (t == JsonToken.VALUE_STRING) { // let's do implicit re-parse - final String text = p.getText(); + String text = p.getText(); CoercionAction act = _checkFromStringCoercion(ctxt, text); if (act == CoercionAction.AsNull) { return (Short) getNullValue(ctxt); @@ -384,6 +385,7 @@ protected Short _parseShort(JsonParser p, DeserializationContext ctxt) throws IO if (act == CoercionAction.AsEmpty) { return (Short) getEmptyValue(ctxt); } + text = text.trim(); if (_hasTextualNull(text)) { return (Short) _coerceTextualNull(ctxt, _primitive); } @@ -445,7 +447,7 @@ public Character deserialize(JsonParser p, DeserializationContext ctxt) break; case JsonTokenId.ID_STRING: // this is the usual type - final String text = p.getText(); + String text = p.getText(); CoercionAction act = _checkFromStringCoercion(ctxt, text); if (act == CoercionAction.AsNull) { return (Character) getNullValue(ctxt); @@ -453,6 +455,7 @@ public Character deserialize(JsonParser p, DeserializationContext ctxt) if (act == CoercionAction.AsEmpty) { return (Character) getEmptyValue(ctxt); } + text = text.trim(); if (_hasTextualNull(text)) { return (Character) _coerceTextualNull(ctxt, _primitive); } @@ -518,7 +521,7 @@ protected final Integer _parseInteger(JsonParser p, DeserializationContext ctxt) } return Integer.valueOf(p.getValueAsInt()); case JsonTokenId.ID_STRING: // let's do implicit re-parse - final String text = p.getText(); + String text = p.getText(); CoercionAction act = _checkFromStringCoercion(ctxt, text); if (act == CoercionAction.AsNull) { return (Integer) getNullValue(ctxt); @@ -526,6 +529,7 @@ protected final Integer _parseInteger(JsonParser p, DeserializationContext ctxt) if (act == CoercionAction.AsEmpty) { return (Integer) getEmptyValue(ctxt); } + text = text.trim(); if (_hasTextualNull(text)) { return (Integer) _coerceTextualNull(ctxt, _primitive); } @@ -590,7 +594,7 @@ protected final Long _parseLong(JsonParser p, DeserializationContext ctxt) throw } return p.getValueAsLong(); case JsonTokenId.ID_STRING: - final String text = p.getText(); + String text = p.getText(); CoercionAction act = _checkFromStringCoercion(ctxt, text); if (act == CoercionAction.AsNull) { return (Long) getNullValue(ctxt); @@ -598,6 +602,7 @@ protected final Long _parseLong(JsonParser p, DeserializationContext ctxt) throw if (act == CoercionAction.AsEmpty) { return (Long) getEmptyValue(ctxt); } + text = text.trim(); if (_hasTextualNull(text)) { return (Long) _coerceTextualNull(ctxt, _primitive); } @@ -645,7 +650,7 @@ protected final Float _parseFloat(JsonParser p, DeserializationContext ctxt) } // And finally, let's allow Strings to be converted too if (t == JsonToken.VALUE_STRING) { - final String text = p.getText(); + String text = p.getText(); CoercionAction act = _checkFromStringCoercion(ctxt, text); if (act == CoercionAction.AsNull) { return (Float) getNullValue(ctxt); @@ -653,6 +658,7 @@ protected final Float _parseFloat(JsonParser p, DeserializationContext ctxt) if (act == CoercionAction.AsEmpty) { return (Float) getEmptyValue(ctxt); } + text = text.trim(); if (_hasTextualNull(text)) { return (Float) _coerceTextualNull(ctxt, _primitive); } @@ -722,7 +728,7 @@ protected final Double _parseDouble(JsonParser p, DeserializationContext ctxt) t return p.getDoubleValue(); } if (t == JsonToken.VALUE_STRING) { - final String text = p.getText(); + String text = p.getText(); CoercionAction act = _checkFromStringCoercion(ctxt, text); if (act == CoercionAction.AsNull) { return (Double) getNullValue(ctxt); @@ -730,6 +736,7 @@ protected final Double _parseDouble(JsonParser p, DeserializationContext ctxt) t if (act == CoercionAction.AsEmpty) { return (Double) getEmptyValue(ctxt); } + text = text.trim(); if (_hasTextualNull(text)) { return (Double) _coerceTextualNull(ctxt, _primitive); } @@ -813,14 +820,18 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx return p.getNumberValue(); case JsonTokenId.ID_STRING: - /* Textual values are more difficult... not parsing itself, but figuring - * out 'minimal' type to use - */ - String text = p.getText().trim(); - if ((text.length() == 0)) { - // note: no need to call `coerce` as this is never primitive + // Textual values are more difficult... not parsing itself, but figuring + // out 'minimal' type to use + + String text = p.getText(); + CoercionAction act = _checkFromStringCoercion(ctxt, text); + if (act == CoercionAction.AsNull) { return getNullValue(ctxt); } + if (act == CoercionAction.AsEmpty) { + return getEmptyValue(ctxt); + } + text = text.trim(); if (_hasTextualNull(text)) { // note: no need to call `coerce` as this is never primitive return getNullValue(ctxt); @@ -834,7 +845,6 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx if (_isNaN(text)) { return Double.NaN; } - _verifyStringForScalarCoercion(ctxt, text); try { if (!_isIntNumber(text)) { if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) { @@ -887,8 +897,7 @@ public Object deserializeWithType(JsonParser p, DeserializationContext ctxt, /* /********************************************************** - /* And then bit more complicated (but non-structured) number - /* types + /* And then bit more complicated (but non-structured) number types /********************************************************** */ @@ -935,13 +944,19 @@ public BigInteger deserialize(JsonParser p, DeserializationContext ctxt) throws case JsonTokenId.ID_START_ARRAY: return _deserializeFromArray(p, ctxt); case JsonTokenId.ID_STRING: // let's do implicit re-parse - String text = p.getText().trim(); - // note: no need to call `coerce` as this is never primitive - if (_isEmptyOrTextualNull(text)) { - _verifyNullForScalarCoercion(ctxt, text); + String text = p.getText(); + CoercionAction act = _checkFromStringCoercion(ctxt, text); + if (act == CoercionAction.AsNull) { + return (BigInteger) getNullValue(ctxt); + } + if (act == CoercionAction.AsEmpty) { + return (BigInteger) getEmptyValue(ctxt); + } + text = text.trim(); + if (_hasTextualNull(text)) { + // note: no need to call `coerce` as this is never primitive return (BigInteger) getNullValue(ctxt); } - _verifyStringForScalarCoercion(ctxt, text); try { return new BigInteger(text); } catch (IllegalArgumentException iae) { } @@ -980,13 +995,19 @@ public BigDecimal deserialize(JsonParser p, DeserializationContext ctxt) case JsonTokenId.ID_NUMBER_FLOAT: return p.getDecimalValue(); case JsonTokenId.ID_STRING: - String text = p.getText().trim(); - // note: no need to call `coerce` as this is never primitive - if (_isEmptyOrTextualNull(text)) { - _verifyNullForScalarCoercion(ctxt, text); + String text = p.getText(); + CoercionAction act = _checkFromStringCoercion(ctxt, text); + if (act == CoercionAction.AsNull) { + return (BigDecimal) getNullValue(ctxt); + } + if (act == CoercionAction.AsEmpty) { + return (BigDecimal) getEmptyValue(ctxt); + } + text = text.trim(); + if (_hasTextualNull(text)) { + // note: no need to call `coerce` as this is never primitive return (BigDecimal) getNullValue(ctxt); } - _verifyStringForScalarCoercion(ctxt, text); try { return new BigDecimal(text); } catch (IllegalArgumentException iae) { }