1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

correct string check for JSONObject optBigDecimal and optBigInteger

This commit is contained in:
John J. Aylward 2017-05-18 14:41:42 -04:00
parent 1ab5260a7a
commit a7f8ff24df

View file

@ -1024,14 +1024,12 @@ public class JSONObject {
|| val instanceof Short || val instanceof Byte){ || val instanceof Short || val instanceof Byte){
return new BigDecimal(((Number) val).longValue()); return new BigDecimal(((Number) val).longValue());
} }
if (val instanceof String) { // don't check if it's a string in case of unchecked Number subclasses
try { try {
return new BigDecimal((String) val); return new BigDecimal(val.toString());
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
}
} }
return defaultValue;
} }
/** /**
@ -1063,19 +1061,17 @@ public class JSONObject {
|| val instanceof Short || val instanceof Byte){ || val instanceof Short || val instanceof Byte){
return BigInteger.valueOf(((Number) val).longValue()); return BigInteger.valueOf(((Number) val).longValue());
} }
if (val instanceof String) { // don't check if it's a string in case of unchecked Number subclasses
try { try {
// the other opt functions handle implicit conversions, i.e. // the other opt functions handle implicit conversions, i.e.
// jo.put("double",1.1d); // jo.put("double",1.1d);
// jo.optInt("double"); -- will return 1, not an error // jo.optInt("double"); -- will return 1, not an error
// this conversion to BigDecimal then to BigInteger is to maintain // this conversion to BigDecimal then to BigInteger is to maintain
// that type cast support that may truncate the decimal. // that type cast support that may truncate the decimal.
return new BigDecimal((String) val).toBigInteger(); return new BigDecimal(val.toString()).toBigInteger();
} catch (Exception e) { } catch (Exception e) {
return defaultValue; return defaultValue;
}
} }
return defaultValue;
} }
/** /**