Skip to content

Commit f359c0b

Browse files
authored
Use NumberInput helper methods for parsing (#237)
1 parent 7919531 commit f359c0b

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/DecimalUtils.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.fasterxml.jackson.datatype.jsr310;
1818

19+
import com.fasterxml.jackson.core.io.NumberInput;
20+
1921
import java.math.BigDecimal;
2022
import java.time.Instant;
2123
import java.util.function.BiFunction;
@@ -92,7 +94,7 @@ public static BigDecimal toBigDecimal(long seconds, int nanoseconds)
9294
}
9395
return BigDecimal.valueOf(seconds).setScale(9);
9496
}
95-
return new BigDecimal(toDecimal(seconds, nanoseconds));
97+
return NumberInput.parseBigDecimal(toDecimal(seconds, nanoseconds));
9698
}
9799

98100
/**
@@ -104,7 +106,7 @@ public static int extractNanosecondDecimal(BigDecimal value, long integer)
104106
// !!! 14-Mar-2016, tatu: Somewhat inefficient; should replace with functionally
105107
// equivalent code that just subtracts integral part? (or, measure and show
106108
// there's no difference and do nothing... )
107-
return value.subtract(new BigDecimal(integer)).multiply(ONE_BILLION).intValue();
109+
return value.subtract(BigDecimal.valueOf(integer)).multiply(ONE_BILLION).intValue();
108110
}
109111

110112
/**
@@ -136,7 +138,7 @@ else if (seconds.scale() < -63) {
136138
else {
137139
// Now we know that seconds has reasonable scale, we can safely chop it apart.
138140
secondsOnly = seconds.longValue();
139-
nanosOnly = nanoseconds.subtract(new BigDecimal(secondsOnly).scaleByPowerOfTen(9)).intValue();
141+
nanosOnly = nanoseconds.subtract(BigDecimal.valueOf(secondsOnly).scaleByPowerOfTen(9)).intValue();
140142

141143
if (secondsOnly < 0 && secondsOnly > Instant.MIN.getEpochSecond()) {
142144
// Issue #69 and Issue #120: avoid sending a negative adjustment to the Instant constructor, we want this as the actual nanos

datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/deser/InstantDeserializer.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.fasterxml.jackson.core.JsonParser;
2121
import com.fasterxml.jackson.core.JsonToken;
2222
import com.fasterxml.jackson.core.JsonTokenId;
23+
import com.fasterxml.jackson.core.io.NumberInput;
2324
import com.fasterxml.jackson.databind.BeanProperty;
2425
import com.fasterxml.jackson.databind.DeserializationContext;
2526
import com.fasterxml.jackson.databind.DeserializationFeature;
@@ -272,10 +273,10 @@ protected T _fromString(JsonParser p, DeserializationContext ctxt,
272273
if (dots >= 0) { // negative if not simple number
273274
try {
274275
if (dots == 0) {
275-
return _fromLong(ctxt, Long.parseLong(string));
276+
return _fromLong(ctxt, NumberInput.parseLong(string));
276277
}
277278
if (dots == 1) {
278-
return _fromDecimal(ctxt, new BigDecimal(string));
279+
return _fromDecimal(ctxt, NumberInput.parseBigDecimal(string));
279280
}
280281
} catch (NumberFormatException e) {
281282
// fall through to default handling, to get error there

0 commit comments

Comments
 (0)