Skip to content

Commit 382f62e

Browse files
author
John J. Aylward
committed
* Prevent exceptions in cases where the value is not a string.
* Don't call toString when we know it's a string, just cast
1 parent 0c7bd72 commit 382f62e

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

JSONObject.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -980,11 +980,14 @@ public BigDecimal optBigDecimal(String key, BigDecimal defaultValue) {
980980
|| val instanceof Short || val instanceof Byte){
981981
return new BigDecimal(((Number) val).longValue());
982982
}
983-
try {
984-
return new BigDecimal(val.toString());
985-
} catch (Exception e) {
986-
return defaultValue;
983+
if (val instanceof String) {
984+
try {
985+
return new BigDecimal((String) val);
986+
} catch (Exception e) {
987+
return defaultValue;
988+
}
987989
}
990+
return defaultValue;
988991
}
989992

990993
/**
@@ -1016,16 +1019,19 @@ public BigInteger optBigInteger(String key, BigInteger defaultValue) {
10161019
|| val instanceof Short || val instanceof Byte){
10171020
return BigInteger.valueOf(((Number) val).longValue());
10181021
}
1019-
try {
1020-
// the other opt functions handle implicit conversions, i.e.
1021-
// jo.put("double",1.1d);
1022-
// jo.optInt("double"); -- will return 1, not an error
1023-
// this conversion to BigDecimal then to BigInteger is to maintain
1024-
// that type cast support that may truncate the decimal.
1025-
return new BigDecimal(val.toString()).toBigInteger();
1026-
} catch (Exception e) {
1027-
return defaultValue;
1022+
if (val instanceof String) {
1023+
try {
1024+
// the other opt functions handle implicit conversions, i.e.
1025+
// jo.put("double",1.1d);
1026+
// jo.optInt("double"); -- will return 1, not an error
1027+
// this conversion to BigDecimal then to BigInteger is to maintain
1028+
// that type cast support that may truncate the decimal.
1029+
return new BigDecimal((String) val).toBigInteger();
1030+
} catch (Exception e) {
1031+
return defaultValue;
1032+
}
10281033
}
1034+
return defaultValue;
10291035
}
10301036

10311037
/**
@@ -1147,7 +1153,7 @@ public int optInt(String key, int defaultValue) {
11471153

11481154
if (val instanceof String) {
11491155
try {
1150-
return new BigDecimal(val.toString()).intValue();
1156+
return new BigDecimal((String) val).intValue();
11511157
} catch (Exception e) {
11521158
return defaultValue;
11531159
}
@@ -1216,7 +1222,7 @@ public long optLong(String key, long defaultValue) {
12161222

12171223
if (val instanceof String) {
12181224
try {
1219-
return new BigDecimal(val.toString()).longValue();
1225+
return new BigDecimal((String) val).longValue();
12201226
} catch (Exception e) {
12211227
return defaultValue;
12221228
}
@@ -1261,7 +1267,7 @@ public Number optNumber(String key, Number defaultValue) {
12611267

12621268
if (val instanceof String) {
12631269
try {
1264-
return new BigDecimal(val.toString());
1270+
return new BigDecimal((String) val);
12651271
} catch (Exception e) {
12661272
return defaultValue;
12671273
}

0 commit comments

Comments
 (0)