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:
parent
1ab5260a7a
commit
a7f8ff24df
1 changed files with 15 additions and 19 deletions
|
@ -1024,15 +1024,13 @@ public class JSONObject {
|
|||
|| val instanceof Short || val instanceof Byte){
|
||||
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 {
|
||||
return new BigDecimal((String) val);
|
||||
return new BigDecimal(val.toString());
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional BigInteger associated with a key, or the defaultValue if
|
||||
|
@ -1063,20 +1061,18 @@ public class JSONObject {
|
|||
|| val instanceof Short || val instanceof Byte){
|
||||
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 {
|
||||
// the other opt functions handle implicit conversions, i.e.
|
||||
// jo.put("double",1.1d);
|
||||
// jo.optInt("double"); -- will return 1, not an error
|
||||
// this conversion to BigDecimal then to BigInteger is to maintain
|
||||
// that type cast support that may truncate the decimal.
|
||||
return new BigDecimal((String) val).toBigInteger();
|
||||
return new BigDecimal(val.toString()).toBigInteger();
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional double associated with a key, or NaN if there is no such
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue