Skip to content

Commit

Permalink
Minor re-factoring wrt #968 (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Apr 4, 2023
1 parent ef2c4cc commit ab65d67
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/main/java/com/fasterxml/jackson/core/base/ParserBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -1098,16 +1098,16 @@ protected void convertNumberToBigInteger() throws IOException
{
if ((_numTypesValid & NR_BIGDECIMAL) != 0) {
// here it'll just get truncated, no exceptions thrown
_numberBigInt = _getBigDecimal().toBigInteger();
_numberBigInt = _convertBigDecimalToBigInteger(_getBigDecimal());
} else if ((_numTypesValid & NR_LONG) != 0) {
_numberBigInt = BigInteger.valueOf(_numberLong);
} else if ((_numTypesValid & NR_INT) != 0) {
_numberBigInt = BigInteger.valueOf(_numberInt);
} else if ((_numTypesValid & NR_DOUBLE) != 0) {
if (_numberString != null) {
_numberBigInt = _getBigDecimal().toBigInteger();
_numberBigInt = _convertBigDecimalToBigInteger(_getBigDecimal());
} else {
_numberBigInt = BigDecimal.valueOf(_getNumberDouble()).toBigInteger();
_numberBigInt = _convertBigDecimalToBigInteger(BigDecimal.valueOf(_getNumberDouble()));
}
} else {
_throwInternal();
Expand Down Expand Up @@ -1214,6 +1214,12 @@ protected void convertNumberToBigDecimal() throws IOException
_numTypesValid |= NR_BIGDECIMAL;
}

// @since 2.15
protected BigInteger _convertBigDecimalToBigInteger(BigDecimal bigDec) throws IOException {
// 04-Apr-2022, tatu: wrt [core#968] Need to limit max scale magnitude
return bigDec.toBigInteger();
}

/**
* Internal accessor that needs to be used for accessing number value of type
* {@link BigInteger} which -- as of 2.14 -- is typically lazily parsed.
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/fasterxml/jackson/core/io/NumberInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public final class NumberInput
{
// numbers with more than these characters are better parsed with BigDecimalParser
// parsing numbers with many digits in Java is slower than O(n)

// 04-Apr-2023, tatu: NOTE! This is above default "longest number by chars"
// limit
private final static int LARGE_INT_SIZE = 1250;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.fasterxml.jackson.failing;

import java.math.BigInteger;

import org.junit.Assert;
import org.junit.Test;

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

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

try (JsonParser p = JSON_F.createParser(DOC)) {
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
Assert.assertNotNull(p.getBigIntegerValue());
BigInteger value = p.getBigIntegerValue();
Assert.assertNotNull(value);
}
}

@Test(timeout = 2000)
public void tinyIntegerViaBigDecimal() throws Exception {
final String DOC = "1e-25000000";

try (JsonParser p = JSON_F.createParser(DOC)) {
assertToken(JsonToken.VALUE_NUMBER_FLOAT, p.nextToken());
BigInteger value = p.getBigIntegerValue();
Assert.assertNotNull(value);
}
}

protected void assertToken(JsonToken expToken, JsonToken actToken)
{
if (actToken != expToken) {
Expand Down

0 comments on commit ab65d67

Please sign in to comment.