Skip to content

Commit 39fdb63

Browse files
committed
Minor changes post #3864 to avoid changing API (but trading in "sneaky" throws)
1 parent 2228d54 commit 39fdb63

File tree

7 files changed

+38
-11
lines changed

7 files changed

+38
-11
lines changed

src/main/java/com/fasterxml/jackson/databind/JsonNode.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -607,11 +607,14 @@ public byte[] binaryValue() throws IOException {
607607
* Returns integer value for this node (as {@link BigInteger}), <b>if and only if</b>
608608
* this node is numeric ({@link #isNumber} returns true). For other
609609
* types returns <code>BigInteger.ZERO</code>.
610+
*<p>
611+
* NOTE: In Jackson 2.x MAY throw {@link com.fasterxml.jackson.core.exc.StreamConstraintsException}
612+
* if the scale of the underlying {@link BigDecimal} is too large to convert (NOTE: thrown
613+
* "sneakily" in Jackson 2.x due to API compatibility restrictions)
610614
*
611615
* @return {@link BigInteger} value this node contains, if numeric node; <code>BigInteger.ZERO</code> for non-number nodes.
612-
* @throws StreamConstraintsException if the scale of the underlying {@link BigDecimal} is too large
613616
*/
614-
public BigInteger bigIntegerValue() throws StreamConstraintsException { return BigInteger.ZERO; }
617+
public BigInteger bigIntegerValue() { return BigInteger.ZERO; }
615618

616619
/*
617620
/**********************************************************

src/main/java/com/fasterxml/jackson/databind/deser/std/NumberDeserializers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ public BigInteger deserialize(JsonParser p, DeserializationContext ctxt) throws
941941
return (BigInteger) getEmptyValue(ctxt);
942942
}
943943
final BigDecimal bd = p.getDecimalValue();
944-
StreamReadConstraints.defaults().validateBigIntegerScale(bd.scale());
944+
p.streamReadConstraints().validateBigIntegerScale(bd.scale());
945945
return bd.toBigInteger();
946946
// 29-Jun-2020, tatu: New! "Scalar from Object" (mostly for XML)
947947
case JsonTokenId.ID_START_OBJECT:

src/main/java/com/fasterxml/jackson/databind/node/BaseJsonNode.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package com.fasterxml.jackson.databind.node;
22

33
import java.io.IOException;
4+
import java.math.BigDecimal;
5+
import java.math.BigInteger;
46

57
import com.fasterxml.jackson.core.*;
8+
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
69
import com.fasterxml.jackson.databind.JsonNode;
710
import com.fasterxml.jackson.databind.JsonSerializable;
811
import com.fasterxml.jackson.databind.SerializerProvider;
912
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
13+
import com.fasterxml.jackson.databind.util.ExceptionUtil;
1014

1115
/**
1216
* Abstract base class common to all standard {@link JsonNode}
@@ -269,4 +273,16 @@ protected JsonPointer _jsonPointerIfValid(String exprOrProperty) {
269273
return null;
270274
}
271275

276+
// @since 2.15
277+
protected BigInteger _bigIntFromBigDec(BigDecimal value) {
278+
try {
279+
StreamReadConstraints.defaults().validateBigIntegerScale(value.scale());
280+
} catch (StreamConstraintsException e) {
281+
// 06-Apr-2023, tatu: Since `JsonNode` does not generally expose Jackson
282+
// exceptions, we need to either wrap possible `StreamConstraintsException`
283+
// or use "sneaky throw". Let's do latter.
284+
ExceptionUtil.throwSneaky(e);
285+
}
286+
return value.toBigInteger();
287+
}
272288
}

src/main/java/com/fasterxml/jackson/databind/node/DecimalNode.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.math.BigInteger;
66

77
import com.fasterxml.jackson.core.*;
8-
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
98
import com.fasterxml.jackson.databind.*;
109

1110
/**
@@ -86,9 +85,8 @@ public boolean canConvertToExactIntegral() {
8685

8786

8887
@Override
89-
public BigInteger bigIntegerValue() throws StreamConstraintsException {
90-
StreamReadConstraints.defaults().validateBigIntegerScale(_value.scale());
91-
return _value.toBigInteger();
88+
public BigInteger bigIntegerValue() {
89+
return _bigIntFromBigDec(_value);
9290
}
9391

9492
@Override

src/main/java/com/fasterxml/jackson/databind/node/NumericNode.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import java.math.BigInteger;
55

66
import com.fasterxml.jackson.core.JsonParser;
7-
import com.fasterxml.jackson.core.exc.StreamConstraintsException;
87

98
/**
109
* Intermediate value node used for numeric nodes.
@@ -32,7 +31,7 @@ public final JsonNodeType getNodeType()
3231
@Override public abstract long longValue();
3332
@Override public abstract double doubleValue();
3433
@Override public abstract BigDecimal decimalValue();
35-
@Override public abstract BigInteger bigIntegerValue() throws StreamConstraintsException;
34+
@Override public abstract BigInteger bigIntegerValue();
3635

3736
@Override public abstract boolean canConvertToInt();
3837
@Override public abstract boolean canConvertToLong();

src/main/java/com/fasterxml/jackson/databind/util/ExceptionUtil.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fasterxml.jackson.databind.util;
22

3+
import java.io.IOException;
4+
35
/**
46
* Utility methods for dealing with exceptions/throwables
57
*
@@ -56,4 +58,14 @@ private static boolean isFatal(Throwable throwable) {
5658
|| throwable instanceof VerifyError
5759
);
5860
}
61+
62+
public static <T> T throwSneaky(IOException e) {
63+
_sneaky(e);
64+
return null; // never gets here, needed for compiler tho
65+
}
66+
67+
@SuppressWarnings("unchecked")
68+
private static <E extends Throwable> void _sneaky(Throwable e) throws E {
69+
throw (E) e;
70+
}
5971
}

src/test/java/com/fasterxml/jackson/databind/node/ObjectNodeTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.fasterxml.jackson.databind.node;
22

3-
import java.io.IOException;
43
import java.math.BigDecimal;
54
import java.math.BigInteger;
65
import java.util.*;
@@ -203,7 +202,7 @@ public void testBasicsPutSet()
203202
assertEquals("foobar", old.textValue());
204203
}
205204

206-
public void testBigNumbers() throws IOException
205+
public void testBigNumbers()
207206
{
208207
ObjectNode n = new ObjectNode(JsonNodeFactory.instance);
209208
assertStandardEquals(n);

0 commit comments

Comments
 (0)