Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to FastDoubleParser v1.0.1 to fix BigDecimal decoding problem #1331

Merged
merged 9 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,18 @@ private BigDecimalParser() {}
* @throws NumberFormatException
*/
public static BigDecimal parse(String valueStr) {
return parse(valueStr.toCharArray());
try {
if (valueStr.length() < 500) {
return new BigDecimal(valueStr);
}
// workaround https://github.com/FasterXML/jackson-databind/issues/4694
return JavaBigDecimalParser.parseBigDecimal(valueStr);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

effectively, the same code as the parse(char[]) method but slight optimisation is that we avoid converting to a char array when we call the JavaBigDecimalParser method


// 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 @@ -58,7 +69,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:
Expand Down Expand Up @@ -113,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);
}
Expand Down Expand Up @@ -165,4 +178,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);
}

}
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 static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -51,6 +52,18 @@ void longValidStringFastParse() {
assertEquals(EXP, BigDecimalParser.parseWithFastParser(num.toCharArray(), 0, num.length()));
}

@Test
pjfanning marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
pjfanning marked this conversation as resolved.
Show resolved Hide resolved
"-11000.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";


protected JsonFactory jsonFactory() {
return JSON_F;
Expand Down Expand Up @@ -243,6 +246,41 @@ void leadingDotInNegativeDecimalAllowedReader() throws Exception {
_testLeadingDotInNegativeDecimalAllowed(jsonFactory(), MODE_READER_THROTTLED);
}

// https://github.com/FasterXML/jackson-databind/issues/4694
cowtowncoder marked this conversation as resolved.
Show resolved Hide resolved
@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 ")) {
Expand Down

This file was deleted.

This file was deleted.