Renamed object methods from ...Obj to ...Object.

Added object method for optDoubleObject (returns Double vice double).
Added similar methods in JSONArray.
Added test methods.
This commit is contained in:
dburbrid
2023-06-29 09:39:34 +01:00
parent 8ce0019a5d
commit 4951ec48c8
5 changed files with 467 additions and 2 deletions

View File

@@ -599,6 +599,38 @@ public class JSONArray implements Iterable<Object> {
}
}
/**
* Get the optional Boolean object associated with an index. It returns false
* if there is no value at that index, or if the value is not Boolean.TRUE
* or the String "true".
*
* @param index
* The index must be between 0 and length() - 1.
* @return The truth.
*/
public Boolean optBooleanObject(int index) {
return this.optBooleanObject(index, false);
}
/**
* Get the optional Boolean object associated with an index. It returns the
* defaultValue if there is no value at that index or if it is not a Boolean
* or the String "true" or "false" (case insensitive).
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* A boolean default.
* @return The truth.
*/
public Boolean optBooleanObject(int index, Boolean defaultValue) {
try {
return this.getBoolean(index);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Get the optional double value associated with an index. NaN is returned
* if there is no value for the index, or if the value is not a number and
@@ -635,6 +667,42 @@ public class JSONArray implements Iterable<Object> {
return doubleValue;
}
/**
* Get the optional Double object associated with an index. NaN is returned
* if there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The object.
*/
public Double optDoubleObject(int index) {
return this.optDoubleObject(index, Double.NaN);
}
/**
* Get the optional double value associated with an index. The defaultValue
* is returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* subscript
* @param defaultValue
* The default object.
* @return The object.
*/
public Double optDoubleObject(int index, Double defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
final Double doubleValue = val.doubleValue();
// if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) {
// return defaultValue;
// }
return doubleValue;
}
/**
* Get the optional float value associated with an index. NaN is returned
* if there is no value for the index, or if the value is not a number and
@@ -671,6 +739,42 @@ public class JSONArray implements Iterable<Object> {
return floatValue;
}
/**
* Get the optional Float object associated with an index. NaN is returned
* if there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The object.
*/
public Float optFloatObject(int index) {
return this.optFloatObject(index, Float.NaN);
}
/**
* Get the optional Float object associated with an index. The defaultValue
* is returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* subscript
* @param defaultValue
* The default object.
* @return The object.
*/
public Float optFloatObject(int index, Float defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
final Float floatValue = val.floatValue();
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
// return floatValue;
// }
return floatValue;
}
/**
* Get the optional int value associated with an index. Zero is returned if
* there is no value for the index, or if the value is not a number and
@@ -703,6 +807,38 @@ public class JSONArray implements Iterable<Object> {
return val.intValue();
}
/**
* Get the optional Integer object associated with an index. Zero is returned if
* there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The object.
*/
public Integer optIntegerObject(int index) {
return this.optIntegerObject(index, 0);
}
/**
* Get the optional Integer object associated with an index. The defaultValue is
* returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default object.
* @return The object.
*/
public Integer optIntegerObject(int index, Integer defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
return val.intValue();
}
/**
* Get the enum value associated with a key.
*
@@ -846,6 +982,38 @@ public class JSONArray implements Iterable<Object> {
return val.longValue();
}
/**
* Get the optional Long object associated with an index. Zero is returned if
* there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @return The object.
*/
public Long optLongObject(int index) {
return this.optLongObject(index, 0L);
}
/**
* Get the optional Long object associated with an index. The defaultValue is
* returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param index
* The index must be between 0 and length() - 1.
* @param defaultValue
* The default object.
* @return The object.
*/
public Long optLongObject(int index, Long defaultValue) {
final Number val = this.optNumber(index, null);
if (val == null) {
return defaultValue;
}
return val.longValue();
}
/**
* Get an optional {@link Number} value associated with a key, or <code>null</code>
* if there is no such key or if the value is not a number. If the value is a string,

View File

@@ -1131,6 +1131,45 @@ public class JSONObject {
}
}
/**
* Get an optional boolean object associated with a key. It returns false if there
* is no such key, or if the value is not Boolean.TRUE or the String "true".
*
* @param key
* A key string.
* @return The truth.
*/
public Boolean optBooleanObject(String key) {
return this.optBooleanObject(key, false);
}
/**
* Get an optional boolean object associated with a key. It returns the
* defaultValue if there is no such key, or if it is not a Boolean or the
* String "true" or "false" (case insensitive).
*
* @param key
* A key string.
* @param defaultValue
* The default.
* @return The truth.
*/
public Boolean optBooleanObject(String key, Boolean defaultValue) {
Object val = this.opt(key);
if (NULL.equals(val)) {
return defaultValue;
}
if (val instanceof Boolean){
return ((Boolean) val).booleanValue();
}
try {
// we'll use the get anyway because it does string conversion.
return this.getBoolean(key);
} catch (Exception e) {
return defaultValue;
}
}
/**
* Get an optional BigDecimal associated with a key, or the defaultValue if
* there is no such key or if its value is not a number. If the value is a
@@ -1294,7 +1333,39 @@ public class JSONObject {
}
/**
* Get the optional double value associated with an index. NaN is returned
* Get an optional Double object associated with a key, or NaN if there is no such
* key or if its value is not a number. If the value is a string, an attempt
* will be made to evaluate it as a number.
*
* @param key
* A string which is the key.
* @return An object which is the value.
*/
public Double optDoubleObject(String key) {
return this.optDoubleObject(key, Double.NaN);
}
/**
* Get an optional Double object associated with a key, or the defaultValue if
* there is no such key or if its value is not a number. If the value is a
* string, an attempt will be made to evaluate it as a number.
*
* @param key
* A key string.
* @param defaultValue
* The default.
* @return An object which is the value.
*/
public Double optDoubleObject(String key, Double defaultValue) {
Number val = this.optNumber(key);
if (val == null) {
return defaultValue;
}
return val.doubleValue();
}
/**
* Get the optional float value associated with an index. NaN is returned
* if there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
@@ -1307,7 +1378,7 @@ public class JSONObject {
}
/**
* Get the optional double value associated with an index. The defaultValue
* Get the optional float value associated with an index. The defaultValue
* is returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
@@ -1329,6 +1400,42 @@ public class JSONObject {
return floatValue;
}
/**
* Get the optional Float object associated with an index. NaN is returned
* if there is no value for the index, or if the value is not a number and
* cannot be converted to a number.
*
* @param key
* A key string.
* @return The object.
*/
public Float optFloatObject(String key) {
return this.optFloatObject(key, Float.NaN);
}
/**
* Get the optional Float object associated with an index. The defaultValue
* is returned if there is no value for the index, or if the value is not a
* number and cannot be converted to a number.
*
* @param key
* A key string.
* @param defaultValue
* The default object.
* @return The object.
*/
public Float optFloatObject(String key, Float defaultValue) {
Number val = this.optNumber(key);
if (val == null) {
return defaultValue;
}
final Float floatValue = val.floatValue();
// if (Float.isNaN(floatValue) || Float.isInfinite(floatValue)) {
// return defaultValue;
// }
return floatValue;
}
/**
* Get an optional int value associated with a key, or zero if there is no
* such key or if the value is not a number. If the value is a string, an
@@ -1361,6 +1468,38 @@ public class JSONObject {
return val.intValue();
}
/**
* Get an optional Integer object associated with a key, or zero if there is no
* such key or if the value is not a number. If the value is a string, an
* attempt will be made to evaluate it as a number.
*
* @param key
* A key string.
* @return An object which is the value.
*/
public Integer optIntegerObject(String key) {
return this.optIntegerObject(key, 0);
}
/**
* Get an optional Integer object associated with a key, or the default if there
* is no such key or if the value is not a number. If the value is a string,
* an attempt will be made to evaluate it as a number.
*
* @param key
* A key string.
* @param defaultValue
* The default.
* @return An object which is the value.
*/
public Integer optIntegerObject(String key, Integer defaultValue) {
final Number val = this.optNumber(key, null);
if (val == null) {
return defaultValue;
}
return val.intValue();
}
/**
* Get an optional JSONArray associated with a key. It returns null if there
* is no such key, or if its value is not a JSONArray.
@@ -1432,6 +1571,39 @@ public class JSONObject {
return val.longValue();
}
/**
* Get an optional Long object associated with a key, or zero if there is no
* such key or if the value is not a number. If the value is a string, an
* attempt will be made to evaluate it as a number.
*
* @param key
* A key string.
* @return An object which is the value.
*/
public Long optLongObject(String key) {
return this.optLongObject(key, 0L);
}
/**
* Get an optional Long object associated with a key, or the default if there
* is no such key or if the value is not a number. If the value is a string,
* an attempt will be made to evaluate it as a number.
*
* @param key
* A key string.
* @param defaultValue
* The default.
* @return An object which is the value.
*/
public Long optLongObject(String key, Long defaultValue) {
final Number val = this.optNumber(key, null);
if (val == null) {
return defaultValue;
}
return val.longValue();
}
/**
* Get an optional {@link Number} value associated with a key, or <code>null</code>
* if there is no such key or if the value is not a number. If the value is a string,