From 68883b9ff822ad78d889864c48ac3141d90175d5 Mon Sep 17 00:00:00 2001 From: "John J. Aylward" Date: Thu, 19 Nov 2020 19:10:08 -0500 Subject: [PATCH] update number handling to use new helper method for consistency. --- src/main/java/org/json/JSONObject.java | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 72ecee522..b838b8ef3 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -1161,8 +1161,7 @@ static BigDecimal objectToBigDecimal(Object val, BigDecimal defaultValue) { return new BigDecimal((BigInteger) val); } if (val instanceof Double || val instanceof Float){ - final double d = ((Number) val).doubleValue(); - if(Double.isNaN(d)) { + if (!numberIsFinite((Number)val)) { return defaultValue; } return new BigDecimal(((Number) val).doubleValue()); @@ -1212,11 +1211,10 @@ static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) { return ((BigDecimal) val).toBigInteger(); } if (val instanceof Double || val instanceof Float){ - final double d = ((Number) val).doubleValue(); - if(Double.isNaN(d)) { + if (!numberIsFinite((Number)val)) { return defaultValue; } - return new BigDecimal(d).toBigInteger(); + return new BigDecimal(((Number) val).doubleValue()).toBigInteger(); } if (val instanceof Long || val instanceof Integer || val instanceof Short || val instanceof Byte){ @@ -2267,18 +2265,8 @@ public static Object stringToValue(String string) { * If o is a non-finite number. */ public static void testValidity(Object o) throws JSONException { - if (o != null) { - if (o instanceof Double) { - if (((Double) o).isInfinite() || ((Double) o).isNaN()) { - throw new JSONException( - "JSON does not allow non-finite numbers."); - } - } else if (o instanceof Float) { - if (((Float) o).isInfinite() || ((Float) o).isNaN()) { - throw new JSONException( - "JSON does not allow non-finite numbers."); - } - } + if (o instanceof Number && !numberIsFinite((Number) o)) { + throw new JSONException("JSON does not allow non-finite numbers."); } }