mirror of
https://github.com/processing/processing4.git
synced 2026-02-12 18:10:43 +01:00
fix JSON handling of null and optional items
This commit is contained in:
@@ -573,6 +573,20 @@ public class JSONObject {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an optional string associated with a key.
|
||||
* It returns the defaultValue if there is no such key.
|
||||
*
|
||||
* @param key A key string.
|
||||
* @param defaultValue The default.
|
||||
* @return A string which is the value.
|
||||
*/
|
||||
public String getString(String key, String defaultValue) {
|
||||
Object object = this.opt(key);
|
||||
return NULL.equals(object) ? defaultValue : object.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Gets the int value associated with a key
|
||||
*
|
||||
@@ -598,6 +612,25 @@ public class JSONObject {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an optional int 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 int getInt(String key, int defaultValue) {
|
||||
try {
|
||||
return this.getInt(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the long value associated with a key.
|
||||
*
|
||||
@@ -617,6 +650,26 @@ public class JSONObject {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an optional long 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 long getLong(String key, long defaultValue) {
|
||||
try {
|
||||
return this.getLong(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @webref jsonobject:method
|
||||
* @brief Gets the float value associated with a key
|
||||
@@ -630,6 +683,15 @@ public class JSONObject {
|
||||
}
|
||||
|
||||
|
||||
public float getFloat(String key, float defaultValue) {
|
||||
try {
|
||||
return getFloat(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the double value associated with a key.
|
||||
* @param key A key string.
|
||||
@@ -649,6 +711,25 @@ public class JSONObject {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an optional double 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 getDouble(String key, double defaultValue) {
|
||||
try {
|
||||
return this.getDouble(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the boolean value associated with a key.
|
||||
*
|
||||
@@ -676,6 +757,24 @@ public class JSONObject {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get an optional boolean 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 getBoolean(String key, boolean defaultValue) {
|
||||
try {
|
||||
return this.getBoolean(key);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the JSONArray value associated with a key.
|
||||
*
|
||||
@@ -807,7 +906,7 @@ public class JSONObject {
|
||||
* @return true if there is no value associated with the key or if
|
||||
* the value is the JSONObject.NULL object.
|
||||
*/
|
||||
protected boolean isNull(String key) {
|
||||
public boolean isNull(String key) {
|
||||
return JSONObject.NULL.equals(this.opt(key));
|
||||
}
|
||||
|
||||
@@ -910,24 +1009,6 @@ public class JSONObject {
|
||||
// }
|
||||
|
||||
|
||||
// /**
|
||||
// * Get an optional boolean 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.
|
||||
// */
|
||||
// private boolean optBoolean(String key, boolean defaultValue) {
|
||||
// try {
|
||||
// return this.getBoolean(key);
|
||||
// } catch (Exception e) {
|
||||
// return defaultValue;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// /**
|
||||
// * Get an optional double associated with a key,
|
||||
// * or NaN if there is no such key or if its value is not a number.
|
||||
@@ -942,25 +1023,6 @@ public class JSONObject {
|
||||
// }
|
||||
|
||||
|
||||
// /**
|
||||
// * Get an optional double 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.
|
||||
// */
|
||||
// private double optDouble(String key, double defaultValue) {
|
||||
// try {
|
||||
// return this.getDouble(key);
|
||||
// } catch (Exception e) {
|
||||
// return defaultValue;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// /**
|
||||
// * 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.
|
||||
@@ -975,25 +1037,6 @@ public class JSONObject {
|
||||
// }
|
||||
|
||||
|
||||
// /**
|
||||
// * Get an optional int 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.
|
||||
// */
|
||||
// private int optInt(String key, int defaultValue) {
|
||||
// try {
|
||||
// return this.getInt(key);
|
||||
// } catch (Exception e) {
|
||||
// return defaultValue;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// /**
|
||||
// * Get an optional JSONArray associated with a key.
|
||||
// * It returns null if there is no such key, or if its value is not a
|
||||
@@ -1036,25 +1079,6 @@ public class JSONObject {
|
||||
// }
|
||||
|
||||
|
||||
// /**
|
||||
// * Get an optional long 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 long optLong(String key, long defaultValue) {
|
||||
// try {
|
||||
// return this.getLong(key);
|
||||
// } catch (Exception e) {
|
||||
// return defaultValue;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
// /**
|
||||
// * Get an optional string associated with a key.
|
||||
// * It returns an empty string if there is no such key. If the value is not
|
||||
@@ -1068,20 +1092,6 @@ public class JSONObject {
|
||||
// }
|
||||
|
||||
|
||||
// /**
|
||||
// * Get an optional string associated with a key.
|
||||
// * It returns the defaultValue if there is no such key.
|
||||
// *
|
||||
// * @param key A key string.
|
||||
// * @param defaultValue The default.
|
||||
// * @return A string which is the value.
|
||||
// */
|
||||
// public String optString(String key, String defaultValue) {
|
||||
// Object object = this.opt(key);
|
||||
// return NULL.equals(object) ? defaultValue : object.toString();
|
||||
// }
|
||||
|
||||
|
||||
private void populateMap(Object bean) {
|
||||
Class klass = bean.getClass();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user