mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
* Prevent exceptions in cases where the value is not a string.
* Don't call toString when we know it's a string, just cast
This commit is contained in:
parent
0c7bd725a6
commit
382f62e781
1 changed files with 22 additions and 16 deletions
|
@ -980,12 +980,15 @@ public class JSONObject {
|
|||
|| val instanceof Short || val instanceof Byte){
|
||||
return new BigDecimal(((Number) val).longValue());
|
||||
}
|
||||
if (val instanceof String) {
|
||||
try {
|
||||
return new BigDecimal(val.toString());
|
||||
return new BigDecimal((String) val);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional BigInteger associated with a key, or the defaultValue if
|
||||
|
@ -1016,17 +1019,20 @@ public class JSONObject {
|
|||
|| val instanceof Short || val instanceof Byte){
|
||||
return BigInteger.valueOf(((Number) val).longValue());
|
||||
}
|
||||
if (val instanceof String) {
|
||||
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(val.toString()).toBigInteger();
|
||||
return new BigDecimal((String) val).toBigInteger();
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional double associated with a key, or NaN if there is no such
|
||||
|
@ -1147,7 +1153,7 @@ public class JSONObject {
|
|||
|
||||
if (val instanceof String) {
|
||||
try {
|
||||
return new BigDecimal(val.toString()).intValue();
|
||||
return new BigDecimal((String) val).intValue();
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -1216,7 +1222,7 @@ public class JSONObject {
|
|||
|
||||
if (val instanceof String) {
|
||||
try {
|
||||
return new BigDecimal(val.toString()).longValue();
|
||||
return new BigDecimal((String) val).longValue();
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
@ -1261,7 +1267,7 @@ public class JSONObject {
|
|||
|
||||
if (val instanceof String) {
|
||||
try {
|
||||
return new BigDecimal(val.toString());
|
||||
return new BigDecimal((String) val);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue