|
19 | 19 |
|
20 | 20 | import java.io.StringReader; |
21 | 21 | import java.lang.reflect.Type; |
| 22 | +import java.math.BigDecimal; |
| 23 | +import java.math.BigInteger; |
22 | 24 | import java.util.Map; |
23 | 25 | import java.util.function.BiFunction; |
24 | 26 | import java.util.function.Function; |
@@ -80,7 +82,51 @@ public BiFunction<JsonInput, PropertySetting, T> apply(Type ignored) { |
80 | 82 | default: |
81 | 83 | throw new JsonException("Unable to coerce to a number: " + jsonInput.peek()); |
82 | 84 | } |
| 85 | + validateIntegralRange(number, stereotype); |
83 | 86 | return mapper.apply(number); |
84 | 87 | }; |
85 | 88 | } |
| 89 | + |
| 90 | + private static void validateIntegralRange(Number number, Class<?> stereotype) { |
| 91 | + // Only for integral targets |
| 92 | + if (!(stereotype == Byte.class |
| 93 | + || stereotype == Short.class |
| 94 | + || stereotype == Integer.class |
| 95 | + || stereotype == Long.class)) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + final BigInteger value; |
| 100 | + try { |
| 101 | + // Parses "2147483648", "1e3", "1.0" safely; rejects "1.2" |
| 102 | + BigDecimal bd = |
| 103 | + (number instanceof BigDecimal) ? (BigDecimal) number : new BigDecimal(number.toString()); |
| 104 | + value = bd.toBigIntegerExact(); |
| 105 | + } catch (RuntimeException e) { |
| 106 | + throw new JsonException( |
| 107 | + "Expected an integer value for " + stereotype.getSimpleName() + ": " + number, e); |
| 108 | + } |
| 109 | + |
| 110 | + BigInteger min; |
| 111 | + BigInteger max; |
| 112 | + |
| 113 | + if (stereotype == Byte.class) { |
| 114 | + min = BigInteger.valueOf(Byte.MIN_VALUE); |
| 115 | + max = BigInteger.valueOf(Byte.MAX_VALUE); |
| 116 | + } else if (stereotype == Short.class) { |
| 117 | + min = BigInteger.valueOf(Short.MIN_VALUE); |
| 118 | + max = BigInteger.valueOf(Short.MAX_VALUE); |
| 119 | + } else if (stereotype == Integer.class) { |
| 120 | + min = BigInteger.valueOf(Integer.MIN_VALUE); |
| 121 | + max = BigInteger.valueOf(Integer.MAX_VALUE); |
| 122 | + } else { // Long.class |
| 123 | + min = BigInteger.valueOf(Long.MIN_VALUE); |
| 124 | + max = BigInteger.valueOf(Long.MAX_VALUE); |
| 125 | + } |
| 126 | + |
| 127 | + if (value.compareTo(min) < 0 || value.compareTo(max) > 0) { |
| 128 | + throw new JsonException( |
| 129 | + "Numeric value out of range for " + stereotype.getSimpleName() + ": " + value); |
| 130 | + } |
| 131 | + } |
86 | 132 | } |
0 commit comments