From d6510b8512e6df26bdd82742d3045d9b03a59717 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sat, 13 Jun 2020 11:07:54 -0700 Subject: [PATCH] refactoring --- .../deser/std/NumberDeserializers.java | 11 +++- .../databind/deser/std/StdDeserializer.java | 62 ++++++++++--------- 2 files changed, 42 insertions(+), 31 deletions(-) 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 a16893f590..d6a6589c9a 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 @@ -959,6 +959,7 @@ public final LogicalType logicalType() { @Override public BigInteger deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { + CoercionAction act; switch (p.currentTokenId()) { case JsonTokenId.ID_NUMBER_INT: switch (p.getNumberType()) { @@ -969,15 +970,19 @@ public BigInteger deserialize(JsonParser p, DeserializationContext ctxt) throws } break; case JsonTokenId.ID_NUMBER_FLOAT: - if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_FLOAT_AS_INT)) { - _failDoubleToIntCoercion(p, ctxt, "java.math.BigInteger"); + act = _checkFloatToIntCoercion(ctxt, p, _valueClass); + if (act == CoercionAction.AsNull) { + return (BigInteger) getNullValue(ctxt); + } + if (act == CoercionAction.AsEmpty) { + return (BigInteger) getEmptyValue(ctxt); } return p.getDecimalValue().toBigInteger(); case JsonTokenId.ID_START_ARRAY: return _deserializeFromArray(p, ctxt); case JsonTokenId.ID_STRING: // let's do implicit re-parse String text = p.getText(); - CoercionAction act = _checkFromStringCoercion(ctxt, text); + act = _checkFromStringCoercion(ctxt, text); if (act == CoercionAction.AsNull) { return getNullValue(ctxt); } diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java index ed72066d90..b251a8bdec 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java @@ -1183,11 +1183,40 @@ protected CoercionAction _checkCoercionActionFail(DeserializationContext ctxt, } /* - /**************************************************** - /* Helper methods for sub-classes, coercions, older (pre-2.12) - /**************************************************** + /********************************************************************** + /* Helper methods for sub-classes, coercions, older (pre-2.12), non-deprecated + /********************************************************************** */ + /** + * Helper method called in case where an integral number is encountered, but + * config settings suggest that a coercion may be needed to "upgrade" + * {@link java.lang.Number} into "bigger" type like {@link java.lang.Long} or + * {@link java.math.BigInteger} + * + * @see DeserializationFeature#USE_BIG_INTEGER_FOR_INTS + * @see DeserializationFeature#USE_LONG_FOR_INTS + * + * @since 2.6 + */ + protected Object _coerceIntegral(JsonParser p, DeserializationContext ctxt) throws IOException + { + int feats = ctxt.getDeserializationFeatures(); + if (DeserializationFeature.USE_BIG_INTEGER_FOR_INTS.enabledIn(feats)) { + return p.getBigIntegerValue(); + } + if (DeserializationFeature.USE_LONG_FOR_INTS.enabledIn(feats)) { + return p.getLongValue(); + } + return p.getNumberValue(); // should be optimal, whatever it is + } + + /* + /********************************************************************** + /* Helper methods for sub-classes, coercions, older (pre-2.12), deprecated + /********************************************************************** + */ + /** * @deprecated Since 2.12 use {@link #_checkFromStringCoercion} instead */ @@ -1225,6 +1254,7 @@ protected Object _coerceEmptyString(DeserializationContext ctxt, boolean isPrimi return null; } + @Deprecated // since 2.12 protected void _failDoubleToIntCoercion(JsonParser p, DeserializationContext ctxt, String type) throws IOException { @@ -1233,29 +1263,6 @@ protected void _failDoubleToIntCoercion(JsonParser p, DeserializationContext ctx p.getValueAsString(), type); } - /** - * Helper method called in case where an integral number is encountered, but - * config settings suggest that a coercion may be needed to "upgrade" - * {@link java.lang.Number} into "bigger" type like {@link java.lang.Long} or - * {@link java.math.BigInteger} - * - * @see DeserializationFeature#USE_BIG_INTEGER_FOR_INTS - * @see DeserializationFeature#USE_LONG_FOR_INTS - * - * @since 2.6 - */ - protected Object _coerceIntegral(JsonParser p, DeserializationContext ctxt) throws IOException - { - int feats = ctxt.getDeserializationFeatures(); - if (DeserializationFeature.USE_BIG_INTEGER_FOR_INTS.enabledIn(feats)) { - return p.getBigIntegerValue(); - } - if (DeserializationFeature.USE_LONG_FOR_INTS.enabledIn(feats)) { - return p.getLongValue(); - } - return p.getBigIntegerValue(); // 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 @@ -1323,8 +1330,7 @@ protected final void _verifyNullForPrimitiveCoercion(DeserializationContext ctxt _reportFailedNullCoerce(ctxt, enable, feat, strDesc); } - // NOTE: for non-primitive Scalars - // @since 2.9 + @Deprecated // since 2.12 protected final void _verifyNullForScalarCoercion(DeserializationContext ctxt, String str) throws JsonMappingException { if (!ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS)) {