Skip to content

Commit 5129bca

Browse files
committed
Adding nullable functions
1 parent 612e860 commit 5129bca

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

src/com/github/tsohr/JSONObject.java

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,33 @@ public boolean optBoolean(String key, boolean defaultValue) {
11321132
return defaultValue;
11331133
}
11341134
}
1135+
1136+
/**
1137+
* Get an optional boolean associated with a key. It returns the
1138+
* defaultValue if there is no such key, or if it is not a Boolean or the
1139+
* String "true" or "false" (case insensitive).
1140+
*
1141+
* @param key
1142+
* A key string.
1143+
* @param defaultValue
1144+
* The default.
1145+
* @return The truth. including null
1146+
*/
1147+
public Boolean optBooleanNullable(String key, Boolean defaultValue) {
1148+
Object val = this.opt(key);
1149+
if (NULL.equals(val)) {
1150+
return defaultValue;
1151+
}
1152+
if (val instanceof Boolean){
1153+
return ((Boolean) val).booleanValue();
1154+
}
1155+
try {
1156+
// we'll use the get anyway because it does string conversion.
1157+
return this.getBoolean(key);
1158+
} catch (Exception e) {
1159+
return defaultValue;
1160+
}
1161+
}
11351162

11361163
/**
11371164
* Get an optional BigDecimal associated with a key, or the defaultValue if
@@ -1280,6 +1307,29 @@ public double optDouble(String key, double defaultValue) {
12801307
// }
12811308
return doubleValue;
12821309
}
1310+
1311+
/**
1312+
* Get an optional double associated with a key, or the defaultValue if
1313+
* there is no such key or if its value is not a number. If the value is a
1314+
* string, an attempt will be made to evaluate it as a number.
1315+
*
1316+
* @param key
1317+
* A key string.
1318+
* @param defaultValue
1319+
* The default.
1320+
* @return An object which is the value. including null
1321+
*/
1322+
public Double optDoubleNullable(String key, Double defaultValue) {
1323+
Number val = this.optNumber(key);
1324+
if (val == null) {
1325+
return defaultValue;
1326+
}
1327+
final double doubleValue = val.doubleValue();
1328+
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
1329+
// return defaultValue;
1330+
// }
1331+
return doubleValue;
1332+
}
12831333

12841334
/**
12851335
* Get the optional double value associated with an index. NaN is returned
@@ -1317,6 +1367,29 @@ public float optFloat(String key, float defaultValue) {
13171367
return floatValue;
13181368
}
13191369

1370+
/**
1371+
* Get the optional double value associated with an index. The defaultValue
1372+
* is returned if there is no value for the index, or if the value is not a
1373+
* number and cannot be converted to a number.
1374+
*
1375+
* @param key
1376+
* A key string.
1377+
* @param defaultValue
1378+
* The default value.
1379+
* @return The value. including null
1380+
*/
1381+
public Float optFloatNullable(String key, Float defaultValue) {
1382+
Number val = this.optNumber(key);
1383+
if (val == null) {
1384+
return defaultValue;
1385+
}
1386+
final float floatValue = val.floatValue();
1387+
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
1388+
// return defaultValue;
1389+
// }
1390+
return floatValue;
1391+
}
1392+
13201393
/**
13211394
* Get an optional int value associated with a key, or zero if there is no
13221395
* such key or if the value is not a number. If the value is a string, an
@@ -1348,6 +1421,25 @@ public int optInt(String key, int defaultValue) {
13481421
}
13491422
return val.intValue();
13501423
}
1424+
1425+
/**
1426+
* Get an optional int value associated with a key, or the default if there
1427+
* is no such key or if the value is not a number. If the value is a string,
1428+
* an attempt will be made to evaluate it as a number.
1429+
*
1430+
* @param key
1431+
* A key string.
1432+
* @param defaultValue
1433+
* The default.
1434+
* @return An object which is the value. including null
1435+
*/
1436+
public Integer optIntNullable(String key, Integer defaultValue) {
1437+
final Number val = this.optNumber(key, null);
1438+
if (val == null) {
1439+
return defaultValue;
1440+
}
1441+
return val.intValue();
1442+
}
13511443

13521444
/**
13531445
* Get an optional JSONArray associated with a key. It returns null if there
@@ -1407,6 +1499,26 @@ public long optLong(String key, long defaultValue) {
14071499

14081500
return val.longValue();
14091501
}
1502+
1503+
/**
1504+
* Get an optional long value associated with a key, or the default if there
1505+
* is no such key or if the value is not a number. If the value is a string,
1506+
* an attempt will be made to evaluate it as a number.
1507+
*
1508+
* @param key
1509+
* A key string.
1510+
* @param defaultValue
1511+
* The default.
1512+
* @return An object which is the value. including null
1513+
*/
1514+
public Long optLongNullable(String key, Long defaultValue) {
1515+
final Number val = this.optNumber(key, null);
1516+
if (val == null) {
1517+
return defaultValue;
1518+
}
1519+
1520+
return val.longValue();
1521+
}
14101522

14111523
/**
14121524
* Get an optional {@link Number} value associated with a key, or <code>null</code>

0 commit comments

Comments
 (0)