Skip to content

Commit aed294f

Browse files
committed
Minor re-factoring wrt #968
1 parent ef2c4cc commit aed294f

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

src/main/java/com/fasterxml/jackson/core/base/ParserBase.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,16 +1098,16 @@ protected void convertNumberToBigInteger() throws IOException
10981098
{
10991099
if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
11001100
// here it'll just get truncated, no exceptions thrown
1101-
_numberBigInt = _getBigDecimal().toBigInteger();
1101+
_numberBigInt = _convertBigDecimalToBigInteger(_getBigDecimal());
11021102
} else if ((_numTypesValid & NR_LONG) != 0) {
11031103
_numberBigInt = BigInteger.valueOf(_numberLong);
11041104
} else if ((_numTypesValid & NR_INT) != 0) {
11051105
_numberBigInt = BigInteger.valueOf(_numberInt);
11061106
} else if ((_numTypesValid & NR_DOUBLE) != 0) {
11071107
if (_numberString != null) {
1108-
_numberBigInt = _getBigDecimal().toBigInteger();
1108+
_numberBigInt = _convertBigDecimalToBigInteger(_getBigDecimal());
11091109
} else {
1110-
_numberBigInt = BigDecimal.valueOf(_getNumberDouble()).toBigInteger();
1110+
_numberBigInt = _convertBigDecimalToBigInteger(BigDecimal.valueOf(_getNumberDouble()));
11111111
}
11121112
} else {
11131113
_throwInternal();
@@ -1214,6 +1214,12 @@ protected void convertNumberToBigDecimal() throws IOException
12141214
_numTypesValid |= NR_BIGDECIMAL;
12151215
}
12161216

1217+
// @since 2.15
1218+
protected BigInteger _convertBigDecimalToBigInteger(BigDecimal bigDec) throws IOException {
1219+
// 04-Apr-2022, tatu: wrt [core#968] Need to limit max scale magnitude
1220+
return bigDec.toBigInteger();
1221+
}
1222+
12171223
/**
12181224
* Internal accessor that needs to be used for accessing number value of type
12191225
* {@link BigInteger} which -- as of 2.14 -- is typically lazily parsed.

src/main/java/com/fasterxml/jackson/core/io/NumberInput.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public final class NumberInput
1616
{
1717
// numbers with more than these characters are better parsed with BigDecimalParser
1818
// parsing numbers with many digits in Java is slower than O(n)
19+
20+
// 04-Apr-2023, tatu: NOTE! This is above default "longest number by chars"
21+
// limit
1922
private final static int LARGE_INT_SIZE = 1250;
2023

2124
/**

src/test/java/com/fasterxml/jackson/failing/PerfBigDecimalToInteger968.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fasterxml.jackson.failing;
22

3+
import java.math.BigInteger;
4+
35
import org.junit.Assert;
46
import org.junit.Test;
57

@@ -11,16 +13,28 @@ public class PerfBigDecimalToInteger968
1113
private final JsonFactory JSON_F = new JsonFactory();
1214

1315
// For [core#968]: shouldn't take multiple seconds
14-
@Test(timeout = 3000)
16+
@Test(timeout = 2000)
1517
public void bigIntegerViaBigDecimal() throws Exception {
16-
final String DOC = "1e20000000";
18+
final String DOC = "1e25000000";
1719

1820
try (JsonParser p = JSON_F.createParser(DOC)) {
1921
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
20-
Assert.assertNotNull(p.getBigIntegerValue());
22+
BigInteger value = p.getBigIntegerValue();
23+
Assert.assertNotNull(value);
2124
}
2225
}
2326

27+
@Test(timeout = 2000)
28+
public void tinyIntegerViaBigDecimal() throws Exception {
29+
final String DOC = "1e-25000000";
30+
31+
try (JsonParser p = JSON_F.createParser(DOC)) {
32+
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
33+
BigInteger value = p.getBigIntegerValue();
34+
Assert.assertNotNull(value);
35+
}
36+
}
37+
2438
protected void assertToken(JsonToken expToken, JsonToken actToken)
2539
{
2640
if (actToken != expToken) {

0 commit comments

Comments
 (0)