Skip to content

Commit

Permalink
Merge branch '2.18'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 13, 2024
2 parents 6d8a09f + 5c76113 commit 7a28627
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 60 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ tools.jackson.core.*;version=${project.version}
<dependency>
<groupId>ch.randelshofer</groupId>
<artifactId>fastdoubleparser</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
</dependency>

<!-- Test dependencies -->
Expand Down
5 changes: 3 additions & 2 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ a pure JSON library.
#1305: Make helper methods of `WriterBasedJsonGenerator` non-final to allow overriding
(contributed by @zhangOranges)
#1310: Add new `StreamReadConstraints` (`maxTokenCount`) to limit maximum number
of Tokens allowed per document
(implemented by @pjfanning)
of Tokens allowed per document#
#1331: Update to FastDoubleParser v1.0.1 to fix `BigDecimal` decoding proble
(fixed by @pjfanning)

2.17.2 (05-Jul-2024)

Expand Down
16 changes: 14 additions & 2 deletions src/main/java/tools/jackson/core/io/BigDecimalParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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() {}

Expand All @@ -39,7 +40,17 @@ private BigDecimalParser() {}
* @throws NumberFormatException for decoding failures
*/
public static BigDecimal parse(String valueStr) {
return parse(valueStr.toCharArray());
try {
if (valueStr.length() < SIZE_FOR_SWITCH_TO_FASTDOUBLEPARSER) {
return new BigDecimal(valueStr);
}
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);
}
}

/**
Expand All @@ -59,7 +70,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);
Expand Down Expand Up @@ -172,4 +183,5 @@ private static String _generateExceptionMessage(final String valueToReport, fina
return String.format("Value %s can not be deserialized as `java.math.BigDecimal`, reason: %s" ,
valueToReport, desc);
}

}
13 changes: 13 additions & 0 deletions src/test/java/tools/jackson/core/io/BigDecimalParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.math.BigDecimal;

import ch.randelshofer.fastdoubleparser.JavaBigDecimalParser;
import org.junit.jupiter.api.Test;

import tools.jackson.core.JUnit5TestBase;
Expand Down Expand Up @@ -53,6 +54,18 @@ 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));
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() {
final int len = 1500;
final StringBuilder sb = new StringBuilder(len);
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/tools/jackson/core/read/NumberParsingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ protected JsonFactory jsonFactory() {

}

private final String ISSUE_4694_VALUE =
"-11000.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";

/*
/**********************************************************************
/* Tests, Boolean
Expand Down Expand Up @@ -1006,6 +1009,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
Expand Down
25 changes: 0 additions & 25 deletions src/test/java/tools/jackson/failing/BigDecimalParser4694Test.java

This file was deleted.

30 changes: 0 additions & 30 deletions src/test/java/tools/jackson/failing/NumberParsing4694Test.java

This file was deleted.

0 comments on commit 7a28627

Please sign in to comment.