From 49463bc6b601c6468656e05d62e9714822d914ad Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 11 Sep 2024 23:44:38 +0100 Subject: [PATCH 1/9] workaround bug in FastDoubleParser --- .../fasterxml/jackson/core/io/BigDecimalParser.java | 9 +++++++-- .../jackson/core/io/BigDecimalParserTest.java | 11 +++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java b/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java index eddbb9347d..5a0edbd1c2 100644 --- a/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java +++ b/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java @@ -40,7 +40,11 @@ private BigDecimalParser() {} * @throws NumberFormatException */ public static BigDecimal parse(String valueStr) { - return parse(valueStr.toCharArray()); + if (valueStr.length() < 500) { + return new BigDecimal(valueStr); + } + // workaround https://github.com/FasterXML/jackson-databind/issues/4694 + return JavaBigDecimalParser.parseBigDecimal(valueStr); } /** @@ -58,7 +62,8 @@ public static BigDecimal parse(final char[] chars, final int off, final int len) if (len < 500) { return new BigDecimal(chars, off, len); } - return JavaBigDecimalParser.parseBigDecimal(chars, off, len); + // workaround https://github.com/FasterXML/jackson-databind/issues/4694 + return JavaBigDecimalParser.parseBigDecimal(new String(chars, off, len)); // 20-Aug-2022, tatu: Although "new BigDecimal(...)" only throws NumberFormatException // operations by "parseBigDecimal()" can throw "ArithmeticException", so handle both: diff --git a/src/test/java/com/fasterxml/jackson/core/io/BigDecimalParserTest.java b/src/test/java/com/fasterxml/jackson/core/io/BigDecimalParserTest.java index bdcdea4ba1..3427d7c94a 100644 --- a/src/test/java/com/fasterxml/jackson/core/io/BigDecimalParserTest.java +++ b/src/test/java/com/fasterxml/jackson/core/io/BigDecimalParserTest.java @@ -2,6 +2,7 @@ import java.math.BigDecimal; +import ch.randelshofer.fastdoubleparser.JavaBigDecimalParser; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; @@ -51,6 +52,16 @@ void longValidStringFastParse() { assertEquals(EXP, BigDecimalParser.parseWithFastParser(num.toCharArray(), 0, num.length())); } + @Test + void issueDatabind4694() { + final String str = "-11000.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + final BigDecimal expected = new BigDecimal(str); + assertEquals(expected, JavaBigDecimalParser.parseBigDecimal(str)); + assertEquals(expected, BigDecimalParser.parse(str)); + final char[] arr = str.toCharArray(); + assertEquals(expected, BigDecimalParser.parse(arr, 0, arr.length)); + } + static String genLongInvalidString() { final int len = 1500; final StringBuilder sb = new StringBuilder(len); From 2a60d86fffc84362509fdb5021884e7ceed71c46 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 12 Sep 2024 00:09:38 +0100 Subject: [PATCH 2/9] fix exception handling --- .../jackson/core/io/BigDecimalParser.java | 21 +++++++++++++------ .../jackson/core/io/BigDecimalParserTest.java | 2 ++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java b/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java index 5a0edbd1c2..aba0f98474 100644 --- a/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java +++ b/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java @@ -40,11 +40,18 @@ private BigDecimalParser() {} * @throws NumberFormatException */ public static BigDecimal parse(String valueStr) { - if (valueStr.length() < 500) { - return new BigDecimal(valueStr); + try { + if (valueStr.length() < 500) { + return new BigDecimal(valueStr); + } + // workaround https://github.com/FasterXML/jackson-databind/issues/4694 + return JavaBigDecimalParser.parseBigDecimal(valueStr); + + // 20-Aug-2022, tatu: Although "new BigDecimal(...)" only throws NumberFormatException + // operations by "parseBigDecimal()" can throw "ArithmeticException", so handle both: + } catch (ArithmeticException | NumberFormatException e) { + throw _parseFailure(e, valueStr); } - // workaround https://github.com/FasterXML/jackson-databind/issues/4694 - return JavaBigDecimalParser.parseBigDecimal(valueStr); } /** @@ -118,7 +125,8 @@ public static BigDecimal parseWithFastParser(final String valueStr) { */ public static BigDecimal parseWithFastParser(final char[] ch, final int off, final int len) { try { - return JavaBigDecimalParser.parseBigDecimal(ch, off, len); + // workaround https://github.com/FasterXML/jackson-databind/issues/4694 + return JavaBigDecimalParser.parseBigDecimal(new String(ch, off, len)); } catch (ArithmeticException | NumberFormatException e) { throw _parseFailure(e, ch, off, len); } @@ -167,7 +175,8 @@ private static String _getValueDesc(final char[] array, final int offset, final } private static String _generateExceptionMessage(final String valueToReport, final String desc) { - return String.format("Value %s can not be deserialized as `java.math.BigDecimal`, reason: %s" , + return String.format("Value %s cannot be deserialized as `java.math.BigDecimal`, reason: %s" , valueToReport, desc); } + } diff --git a/src/test/java/com/fasterxml/jackson/core/io/BigDecimalParserTest.java b/src/test/java/com/fasterxml/jackson/core/io/BigDecimalParserTest.java index 3427d7c94a..9ec4cc6cda 100644 --- a/src/test/java/com/fasterxml/jackson/core/io/BigDecimalParserTest.java +++ b/src/test/java/com/fasterxml/jackson/core/io/BigDecimalParserTest.java @@ -58,8 +58,10 @@ void issueDatabind4694() { final BigDecimal expected = new BigDecimal(str); assertEquals(expected, JavaBigDecimalParser.parseBigDecimal(str)); assertEquals(expected, BigDecimalParser.parse(str)); + assertEquals(expected, BigDecimalParser.parseWithFastParser(str)); final char[] arr = str.toCharArray(); assertEquals(expected, BigDecimalParser.parse(arr, 0, arr.length)); + assertEquals(expected, BigDecimalParser.parseWithFastParser(arr, 0, arr.length)); } static String genLongInvalidString() { From 8f1c037f062a8552c512d259621bbc87432778d1 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 12 Sep 2024 00:27:36 +0100 Subject: [PATCH 3/9] Update BigDecimalParser.java --- .../java/com/fasterxml/jackson/core/io/BigDecimalParser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java b/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java index aba0f98474..fe0e7ced66 100644 --- a/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java +++ b/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java @@ -175,7 +175,7 @@ private static String _getValueDesc(final char[] array, final int offset, final } private static String _generateExceptionMessage(final String valueToReport, final String desc) { - return String.format("Value %s cannot be deserialized as `java.math.BigDecimal`, reason: %s" , + return String.format("Value %s can not be deserialized as `java.math.BigDecimal`, reason: %s" , valueToReport, desc); } From ccaec3042cafbb82efa84fd27fd751281d2d3366 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 12 Sep 2024 01:15:48 +0100 Subject: [PATCH 4/9] Update NonStandardNumberParsingTest.java --- .../core/read/NonStandardNumberParsingTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/test/java/com/fasterxml/jackson/core/read/NonStandardNumberParsingTest.java b/src/test/java/com/fasterxml/jackson/core/read/NonStandardNumberParsingTest.java index 33ee7fc900..b8e45e6519 100644 --- a/src/test/java/com/fasterxml/jackson/core/read/NonStandardNumberParsingTest.java +++ b/src/test/java/com/fasterxml/jackson/core/read/NonStandardNumberParsingTest.java @@ -243,6 +243,20 @@ void leadingDotInNegativeDecimalAllowedReader() throws Exception { _testLeadingDotInNegativeDecimalAllowed(jsonFactory(), MODE_READER_THROTTLED); } + // https://github.com/FasterXML/jackson-databind/issues/4694 + @Test + void databind4694() throws Exception { + final String str = "-11000.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + final BigDecimal expected = new BigDecimal(str); + for (int mode : ALL_MODES) { + try (JsonParser p = createParser(mode, String.format(" %s ", str))) { + assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); + assertEquals(expected, p.getDecimalValue()); + assertFalse(p.isNaN()); + } + } + } + private void _testLeadingDotInDecimalAllowed(JsonFactory f, int mode) throws Exception { try (JsonParser p = createParser(f, mode, " .125 ")) { From acbece63e42dc751158678430974bbfac1a076a0 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 12 Sep 2024 09:36:43 +0100 Subject: [PATCH 5/9] remove failing tests (moved to working tests) --- .../failing/BigDecimalParser4694Test.java | 25 ---------------- .../failing/NumberParsing4694Test.java | 29 ------------------- 2 files changed, 54 deletions(-) delete mode 100644 src/test/java/com/fasterxml/jackson/failing/BigDecimalParser4694Test.java delete mode 100644 src/test/java/com/fasterxml/jackson/failing/NumberParsing4694Test.java diff --git a/src/test/java/com/fasterxml/jackson/failing/BigDecimalParser4694Test.java b/src/test/java/com/fasterxml/jackson/failing/BigDecimalParser4694Test.java deleted file mode 100644 index 5515fdd747..0000000000 --- a/src/test/java/com/fasterxml/jackson/failing/BigDecimalParser4694Test.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.fasterxml.jackson.failing; - -import ch.randelshofer.fastdoubleparser.JavaBigDecimalParser; -import com.fasterxml.jackson.core.io.BigDecimalParser; -import org.junit.jupiter.api.Test; - -import java.math.BigDecimal; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -class BigDecimalParser4694Test extends com.fasterxml.jackson.core.JUnit5TestBase -{ - // https://github.com/FasterXML/jackson-databind/issues/4694 - @Test - void issueDatabind4694() { - final String str = "-11000.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - final BigDecimal expected = new BigDecimal(str); - assertEquals(expected, JavaBigDecimalParser.parseBigDecimal(str)); - assertEquals(expected, BigDecimalParser.parse(str)); - assertEquals(expected, BigDecimalParser.parseWithFastParser(str)); - final char[] arr = str.toCharArray(); - assertEquals(expected, BigDecimalParser.parse(arr, 0, arr.length)); - assertEquals(expected, BigDecimalParser.parseWithFastParser(arr, 0, arr.length)); - } -} diff --git a/src/test/java/com/fasterxml/jackson/failing/NumberParsing4694Test.java b/src/test/java/com/fasterxml/jackson/failing/NumberParsing4694Test.java deleted file mode 100644 index 1dae2ae406..0000000000 --- a/src/test/java/com/fasterxml/jackson/failing/NumberParsing4694Test.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.fasterxml.jackson.failing; - -import com.fasterxml.jackson.core.JUnit5TestBase; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; -import org.junit.jupiter.api.Test; - -import java.math.BigDecimal; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; - -class NumberParsing4694Test - extends JUnit5TestBase -{ - // https://github.com/FasterXML/jackson-databind/issues/4694 - @Test - void databind4694() throws Exception { - final String str = "-11000.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - final BigDecimal expected = new BigDecimal(str); - for (int mode : ALL_MODES) { - try (JsonParser p = createParser(mode, String.format(" %s ", str))) { - assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); - assertEquals(expected, p.getDecimalValue()); - assertFalse(p.isNaN()); - } - } - } -} From 4f540a320b45dd9651c4e03a062f5695a7c90363 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 12 Sep 2024 15:13:25 +0100 Subject: [PATCH 6/9] add double and float tests --- .../read/NonStandardNumberParsingTest.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/fasterxml/jackson/core/read/NonStandardNumberParsingTest.java b/src/test/java/com/fasterxml/jackson/core/read/NonStandardNumberParsingTest.java index b8e45e6519..c67745b21e 100644 --- a/src/test/java/com/fasterxml/jackson/core/read/NonStandardNumberParsingTest.java +++ b/src/test/java/com/fasterxml/jackson/core/read/NonStandardNumberParsingTest.java @@ -21,6 +21,9 @@ class NonStandardNumberParsingTest .enable(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS) .enable(JsonReadFeature.ALLOW_TRAILING_DECIMAL_POINT_FOR_NUMBERS) .build(); + private final String ISSUE_4694_VALUE = + "-11000.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + protected JsonFactory jsonFactory() { return JSON_F; @@ -246,10 +249,9 @@ void leadingDotInNegativeDecimalAllowedReader() throws Exception { // https://github.com/FasterXML/jackson-databind/issues/4694 @Test void databind4694() throws Exception { - final String str = "-11000.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - final BigDecimal expected = new BigDecimal(str); + final BigDecimal expected = new BigDecimal(ISSUE_4694_VALUE); for (int mode : ALL_MODES) { - try (JsonParser p = createParser(mode, String.format(" %s ", str))) { + try (JsonParser p = createParser(mode, String.format(" %s ", ISSUE_4694_VALUE))) { assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); assertEquals(expected, p.getDecimalValue()); assertFalse(p.isNaN()); @@ -257,6 +259,28 @@ void databind4694() throws Exception { } } + void databind4694Double() throws Exception { + final Double expected = new Double(ISSUE_4694_VALUE); + for (int mode : ALL_MODES) { + try (JsonParser p = createParser(mode, String.format(" %s ", ISSUE_4694_VALUE))) { + assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); + assertEquals(expected, p.getDoubleValue()); + assertFalse(p.isNaN()); + } + } + } + + void databind4694Float() throws Exception { + final Float expected = new Float(ISSUE_4694_VALUE); + for (int mode : ALL_MODES) { + try (JsonParser p = createParser(mode, String.format(" %s ", ISSUE_4694_VALUE))) { + assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); + assertEquals(expected, p.getFloatValue()); + assertFalse(p.isNaN()); + } + } + } + private void _testLeadingDotInDecimalAllowed(JsonFactory f, int mode) throws Exception { try (JsonParser p = createParser(f, mode, " .125 ")) { From 6bb2a0ac65512375686ee90dea72e2450a8bc91e Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Thu, 12 Sep 2024 16:18:21 +0100 Subject: [PATCH 7/9] move tests --- .../read/NonStandardNumberParsingTest.java | 38 ------------------- .../jackson/core/read/NumberParsingTest.java | 38 +++++++++++++++++++ 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/src/test/java/com/fasterxml/jackson/core/read/NonStandardNumberParsingTest.java b/src/test/java/com/fasterxml/jackson/core/read/NonStandardNumberParsingTest.java index c67745b21e..33ee7fc900 100644 --- a/src/test/java/com/fasterxml/jackson/core/read/NonStandardNumberParsingTest.java +++ b/src/test/java/com/fasterxml/jackson/core/read/NonStandardNumberParsingTest.java @@ -21,9 +21,6 @@ class NonStandardNumberParsingTest .enable(JsonReadFeature.ALLOW_LEADING_DECIMAL_POINT_FOR_NUMBERS) .enable(JsonReadFeature.ALLOW_TRAILING_DECIMAL_POINT_FOR_NUMBERS) .build(); - private final String ISSUE_4694_VALUE = - "-11000.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; - protected JsonFactory jsonFactory() { return JSON_F; @@ -246,41 +243,6 @@ void leadingDotInNegativeDecimalAllowedReader() throws Exception { _testLeadingDotInNegativeDecimalAllowed(jsonFactory(), MODE_READER_THROTTLED); } - // https://github.com/FasterXML/jackson-databind/issues/4694 - @Test - void databind4694() throws Exception { - final BigDecimal expected = new BigDecimal(ISSUE_4694_VALUE); - for (int mode : ALL_MODES) { - try (JsonParser p = createParser(mode, String.format(" %s ", ISSUE_4694_VALUE))) { - assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); - assertEquals(expected, p.getDecimalValue()); - assertFalse(p.isNaN()); - } - } - } - - void databind4694Double() throws Exception { - final Double expected = new Double(ISSUE_4694_VALUE); - for (int mode : ALL_MODES) { - try (JsonParser p = createParser(mode, String.format(" %s ", ISSUE_4694_VALUE))) { - assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); - assertEquals(expected, p.getDoubleValue()); - assertFalse(p.isNaN()); - } - } - } - - void databind4694Float() throws Exception { - final Float expected = new Float(ISSUE_4694_VALUE); - for (int mode : ALL_MODES) { - try (JsonParser p = createParser(mode, String.format(" %s ", ISSUE_4694_VALUE))) { - assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); - assertEquals(expected, p.getFloatValue()); - assertFalse(p.isNaN()); - } - } - } - private void _testLeadingDotInDecimalAllowed(JsonFactory f, int mode) throws Exception { try (JsonParser p = createParser(f, mode, " .125 ")) { diff --git a/src/test/java/com/fasterxml/jackson/core/read/NumberParsingTest.java b/src/test/java/com/fasterxml/jackson/core/read/NumberParsingTest.java index 676228d89a..c64a9f732f 100644 --- a/src/test/java/com/fasterxml/jackson/core/read/NumberParsingTest.java +++ b/src/test/java/com/fasterxml/jackson/core/read/NumberParsingTest.java @@ -28,6 +28,9 @@ protected JsonFactory jsonFactory() { return sharedStreamFactory(); } + private final String ISSUE_4694_VALUE = + "-11000.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; + /* /********************************************************************** /* Tests, Boolean @@ -996,6 +999,41 @@ void negativeMaxNumberLength() { } } + // https://github.com/FasterXML/jackson-databind/issues/4694 + @Test + void databind4694() throws Exception { + final BigDecimal expected = new BigDecimal(ISSUE_4694_VALUE); + for (int mode : ALL_MODES) { + try (JsonParser p = createParser(mode, String.format(" %s ", ISSUE_4694_VALUE))) { + assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); + assertEquals(expected, p.getDecimalValue()); + assertFalse(p.isNaN()); + } + } + } + + void databind4694Double() throws Exception { + final Double expected = new Double(ISSUE_4694_VALUE); + for (int mode : ALL_MODES) { + try (JsonParser p = createParser(mode, String.format(" %s ", ISSUE_4694_VALUE))) { + assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); + assertEquals(expected, p.getDoubleValue()); + assertFalse(p.isNaN()); + } + } + } + + void databind4694Float() throws Exception { + final Float expected = new Float(ISSUE_4694_VALUE); + for (int mode : ALL_MODES) { + try (JsonParser p = createParser(mode, String.format(" %s ", ISSUE_4694_VALUE))) { + assertEquals(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken()); + assertEquals(expected, p.getFloatValue()); + assertFalse(p.isNaN()); + } + } + } + /* /********************************************************** /* Helper methods From df47cbd5a76a560f7ce8afd319e7ec2248f7b342 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 13 Sep 2024 14:23:00 +0100 Subject: [PATCH 8/9] uptake fastdoubleparser 1.0.1 and remove workaround --- pom.xml | 2 +- .../com/fasterxml/jackson/core/io/BigDecimalParser.java | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index d1b05737a1..49496c3d4b 100644 --- a/pom.xml +++ b/pom.xml @@ -299,7 +299,7 @@ com.fasterxml.jackson.core.*;version=${project.version} ch.randelshofer fastdoubleparser - 1.0.0 + 1.0.1 diff --git a/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java b/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java index fe0e7ced66..126ea1cca5 100644 --- a/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java +++ b/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java @@ -44,7 +44,6 @@ public static BigDecimal parse(String valueStr) { if (valueStr.length() < 500) { return new BigDecimal(valueStr); } - // workaround https://github.com/FasterXML/jackson-databind/issues/4694 return JavaBigDecimalParser.parseBigDecimal(valueStr); // 20-Aug-2022, tatu: Although "new BigDecimal(...)" only throws NumberFormatException @@ -69,8 +68,7 @@ public static BigDecimal parse(final char[] chars, final int off, final int len) if (len < 500) { return new BigDecimal(chars, off, len); } - // workaround https://github.com/FasterXML/jackson-databind/issues/4694 - return JavaBigDecimalParser.parseBigDecimal(new String(chars, off, len)); + return JavaBigDecimalParser.parseBigDecimal(chars, off, len); // 20-Aug-2022, tatu: Although "new BigDecimal(...)" only throws NumberFormatException // operations by "parseBigDecimal()" can throw "ArithmeticException", so handle both: @@ -125,8 +123,7 @@ public static BigDecimal parseWithFastParser(final String valueStr) { */ public static BigDecimal parseWithFastParser(final char[] ch, final int off, final int len) { try { - // workaround https://github.com/FasterXML/jackson-databind/issues/4694 - return JavaBigDecimalParser.parseBigDecimal(new String(ch, off, len)); + return JavaBigDecimalParser.parseBigDecimal(ch, off, len); } catch (ArithmeticException | NumberFormatException e) { throw _parseFailure(e, ch, off, len); } From 1a86c2b6de4dbcea32d68251baf9e4a9ebb25de8 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 13 Sep 2024 14:28:43 +0100 Subject: [PATCH 9/9] add constant --- .../java/com/fasterxml/jackson/core/io/BigDecimalParser.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java b/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java index 126ea1cca5..2be42744c8 100644 --- a/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java +++ b/src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java @@ -25,6 +25,7 @@ public final class BigDecimalParser { final static int MAX_CHARS_TO_REPORT = 1000; + private final static int SIZE_FOR_SWITCH_TO_FASTDOUBLEPARSER = 500; private BigDecimalParser() {} @@ -41,7 +42,7 @@ private BigDecimalParser() {} */ public static BigDecimal parse(String valueStr) { try { - if (valueStr.length() < 500) { + if (valueStr.length() < SIZE_FOR_SWITCH_TO_FASTDOUBLEPARSER) { return new BigDecimal(valueStr); } return JavaBigDecimalParser.parseBigDecimal(valueStr); @@ -65,7 +66,7 @@ public static BigDecimal parse(String valueStr) { */ public static BigDecimal parse(final char[] chars, final int off, final int len) { try { - if (len < 500) { + if (len < SIZE_FOR_SWITCH_TO_FASTDOUBLEPARSER) { return new BigDecimal(chars, off, len); } return JavaBigDecimalParser.parseBigDecimal(chars, off, len);