You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When reading a JSON file, I would like to return a null value if a key is not found. At present, optInt and optFloat only return primitives - so you can't set null as the default.
It's a fairly simple change, to add in the following code to JSONObject:
/**
* Get the optional double (obj) 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.
*
* @param key
* A key string.
* @return The value.
*/
public Float optFloatObj(String key) {
return this.optFloatObj(key, Float.NaN);
}
/**
* Get the optional double (obj) 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 key
* A key string.
* @param defaultValue
* The default value.
* @return The value.
*/
public Float optFloatObj(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 integer 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
* attempt will be made to evaluate it as a number.
*
* @param key
* A key string.
* @return An object which is the value.
*/
public Integer optIntegerObj(String key) {
return this.optIntegerObj(key, 0);
}
/**
* Get an optional integer value 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 optIntegerObj(String key, Integer defaultValue) {
final Number val = this.optNumber(key, null);
if (val == null) {
return defaultValue;
}
return val.intValue();
}
Unfortunately I've forgotten all the little I learnt about forking/creating branches/submitting pull requests etc, and Github doesn't make it particularly easy unless you're already familiar with how to do it - I've made the changes over on https://github.com/davejbur/JSON-java/tree/add-object-based-versions-int-float but the diff seems to think I've deleted and redone huge chunks instead of just adding four methods!
Perhaps someone could either tell me where I've gone wrong with Github, or just add the code above in to the master anyway.
Thanks!
Dave
The text was updated successfully, but these errors were encountered:
When reading a JSON file, I would like to return a null value if a key is not found. At present, optInt and optFloat only return primitives - so you can't set null as the default.
It's a fairly simple change, to add in the following code to JSONObject:
Unfortunately I've forgotten all the little I learnt about forking/creating branches/submitting pull requests etc, and Github doesn't make it particularly easy unless you're already familiar with how to do it - I've made the changes over on https://github.com/davejbur/JSON-java/tree/add-object-based-versions-int-float but the diff seems to think I've deleted and redone huge chunks instead of just adding four methods!
Perhaps someone could either tell me where I've gone wrong with Github, or just add the code above in to the master anyway.
Thanks!
Dave
The text was updated successfully, but these errors were encountered: