Skip to content

Commit 68883b9

Browse files
author
John J. Aylward
committed
update number handling to use new helper method for consistency.
1 parent 11e6b1a commit 68883b9

1 file changed

Lines changed: 5 additions & 17 deletions

File tree

src/main/java/org/json/JSONObject.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,8 +1161,7 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue) {
11611161
return new BigDecimal((BigInteger) val);
11621162
}
11631163
if (val instanceof Double || val instanceof Float){
1164-
final double d = ((Number) val).doubleValue();
1165-
if(Double.isNaN(d)) {
1164+
if (!numberIsFinite((Number)val)) {
11661165
return defaultValue;
11671166
}
11681167
return new BigDecimal(((Number) val).doubleValue());
@@ -1212,11 +1211,10 @@ static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) {
12121211
return ((BigDecimal) val).toBigInteger();
12131212
}
12141213
if (val instanceof Double || val instanceof Float){
1215-
final double d = ((Number) val).doubleValue();
1216-
if(Double.isNaN(d)) {
1214+
if (!numberIsFinite((Number)val)) {
12171215
return defaultValue;
12181216
}
1219-
return new BigDecimal(d).toBigInteger();
1217+
return new BigDecimal(((Number) val).doubleValue()).toBigInteger();
12201218
}
12211219
if (val instanceof Long || val instanceof Integer
12221220
|| val instanceof Short || val instanceof Byte){
@@ -2267,18 +2265,8 @@ public static Object stringToValue(String string) {
22672265
* If o is a non-finite number.
22682266
*/
22692267
public static void testValidity(Object o) throws JSONException {
2270-
if (o != null) {
2271-
if (o instanceof Double) {
2272-
if (((Double) o).isInfinite() || ((Double) o).isNaN()) {
2273-
throw new JSONException(
2274-
"JSON does not allow non-finite numbers.");
2275-
}
2276-
} else if (o instanceof Float) {
2277-
if (((Float) o).isInfinite() || ((Float) o).isNaN()) {
2278-
throw new JSONException(
2279-
"JSON does not allow non-finite numbers.");
2280-
}
2281-
}
2268+
if (o instanceof Number && !numberIsFinite((Number) o)) {
2269+
throw new JSONException("JSON does not allow non-finite numbers.");
22822270
}
22832271
}
22842272

0 commit comments

Comments
 (0)