mirror of
https://github.com/processing/processing4.git
synced 2026-05-03 17:35:00 +02:00
fix JSON handling of null and optional items
This commit is contained in:
@@ -294,6 +294,20 @@ public class JSONArray {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the optional string associated with an index.
|
||||
* The defaultValue is returned if the key is not found.
|
||||
*
|
||||
* @param index The index must be between 0 and length() - 1.
|
||||
* @param defaultValue The default value.
|
||||
* @return A String value.
|
||||
*/
|
||||
public String getString(int index, String defaultValue) {
|
||||
Object object = this.opt(index);
|
||||
return JSONObject.NULL.equals(object) ? defaultValue : object.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the int value associated with an index.
|
||||
*
|
||||
@@ -318,6 +332,23 @@ public class JSONArray {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the optional int 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 The index must be between 0 and length() - 1.
|
||||
* @param defaultValue The default value.
|
||||
* @return The value.
|
||||
*/
|
||||
public int getInt(int index, int defaultValue) {
|
||||
try {
|
||||
return getInt(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the long value associated with an index.
|
||||
*
|
||||
@@ -338,10 +369,27 @@ public class JSONArray {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the optional long 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 The index must be between 0 and length() - 1.
|
||||
* @param defaultValue The default value.
|
||||
* @return The value.
|
||||
*/
|
||||
public long getLong(int index, long defaultValue) {
|
||||
try {
|
||||
return this.getLong(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a value from an index as a float. JSON uses 'double' values
|
||||
* internally, so this is simply getDouble() cast to a float.
|
||||
*
|
||||
*
|
||||
* @webref jsonarray:method
|
||||
* @brief Gets the float value associated with an index
|
||||
* @param index must be between 0 and length() - 1
|
||||
@@ -354,6 +402,15 @@ public class JSONArray {
|
||||
}
|
||||
|
||||
|
||||
public float getFloat(int index, float defaultValue) {
|
||||
try {
|
||||
return getFloat(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the double value associated with an index.
|
||||
*
|
||||
@@ -374,6 +431,24 @@ public class JSONArray {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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 value.
|
||||
* @return The value.
|
||||
*/
|
||||
public double getDouble(int index, double defaultValue) {
|
||||
try {
|
||||
return this.getDouble(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the boolean value associated with an index.
|
||||
* The string values "true" and "false" are converted to boolean.
|
||||
@@ -403,9 +478,27 @@ public class JSONArray {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the optional boolean value 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 getBoolean(int index, boolean defaultValue) {
|
||||
try {
|
||||
return getBoolean(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the JSONArray associated with an index.
|
||||
*
|
||||
*
|
||||
* @webref jsonobject:method
|
||||
* @brief Gets the JSONArray associated with an index value
|
||||
* @param index must be between 0 and length() - 1
|
||||
@@ -425,9 +518,18 @@ public class JSONArray {
|
||||
}
|
||||
|
||||
|
||||
public JSONArray getJSONArray(int index, JSONArray defaultValue) {
|
||||
try {
|
||||
return getJSONArray(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the JSONObject associated with an index.
|
||||
*
|
||||
*
|
||||
* @webref jsonobject:method
|
||||
* @brief Gets the JSONObject associated with an index value
|
||||
* @param index the index value of the object to get
|
||||
@@ -447,9 +549,18 @@ public class JSONArray {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get this entire array as a String array.
|
||||
*
|
||||
public JSONObject getJSONObject(int index, JSONObject defaultValue) {
|
||||
try {
|
||||
return getJSONObject(index);
|
||||
} catch (Exception e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get this entire array as a String array.
|
||||
*
|
||||
* @webref jsonarray:method
|
||||
* @brief Gets the entire array as an array of Strings
|
||||
* @see JSONArray#getIntArray()
|
||||
@@ -463,9 +574,9 @@ public class JSONArray {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get this entire array as an int array. Everything must be an int.
|
||||
*
|
||||
/**
|
||||
* Get this entire array as an int array. Everything must be an int.
|
||||
*
|
||||
* @webref jsonarray:method
|
||||
* @brief Gets the entire array as array of ints
|
||||
* @see JSONArray#getStringArray()
|
||||
@@ -533,24 +644,6 @@ public class JSONArray {
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * Get the optional boolean value 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 optBoolean(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 cannot be converted to a number.
|
||||
@@ -564,24 +657,6 @@ public class JSONArray {
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 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 value.
|
||||
// * @return The value.
|
||||
// */
|
||||
// public double optDouble(int index, double defaultValue) {
|
||||
// try {
|
||||
// return this.getDouble(index);
|
||||
// } catch (Exception e) {
|
||||
// return defaultValue;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * 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 cannot be converted to a number.
|
||||
@@ -595,49 +670,6 @@ public class JSONArray {
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * Get the optional int 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 The index must be between 0 and length() - 1.
|
||||
// * @param defaultValue The default value.
|
||||
// * @return The value.
|
||||
// */
|
||||
// public int optInt(int index, int defaultValue) {
|
||||
// try {
|
||||
// return this.getInt(index);
|
||||
// } catch (Exception e) {
|
||||
// return defaultValue;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * Get the optional JSONArray associated with an index.
|
||||
// * @param index subscript
|
||||
// * @return A JSONArray value, or null if the index has no value,
|
||||
// * or if the value is not a JSONArray.
|
||||
// */
|
||||
// public JSONArray optJSONArray(int index) {
|
||||
// Object o = this.opt(index);
|
||||
// return o instanceof JSONArray ? (JSONArray)o : null;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * Get the optional JSONObject associated with an index.
|
||||
// * Null is returned if the key is not found, or null if the index has
|
||||
// * no value, or if the value is not a JSONObject.
|
||||
// *
|
||||
// * @param index The index must be between 0 and length() - 1.
|
||||
// * @return A JSONObject value.
|
||||
// */
|
||||
// public JSON optJSONObject(int index) {
|
||||
// Object o = this.opt(index);
|
||||
// return o instanceof JSON ? (JSON)o : null;
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * Get the optional long 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 cannot be converted to a number.
|
||||
@@ -651,23 +683,6 @@ public class JSONArray {
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * Get the optional long 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 The index must be between 0 and length() - 1.
|
||||
// * @param defaultValue The default value.
|
||||
// * @return The value.
|
||||
// */
|
||||
// public long optLong(int index, long defaultValue) {
|
||||
// try {
|
||||
// return this.getLong(index);
|
||||
// } catch (Exception e) {
|
||||
// return defaultValue;
|
||||
// }
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * Get the optional string value associated with an index. It returns an
|
||||
// * empty string if there is no value at that index. If the value
|
||||
// * is not a string and is not null, then it is coverted to a string.
|
||||
@@ -677,22 +692,6 @@ public class JSONArray {
|
||||
// */
|
||||
// public String optString(int index) {
|
||||
// return this.optString(index, "");
|
||||
// }
|
||||
//
|
||||
//
|
||||
// /**
|
||||
// * Get the optional string associated with an index.
|
||||
// * The defaultValue is returned if the key is not found.
|
||||
// *
|
||||
// * @param index The index must be between 0 and length() - 1.
|
||||
// * @param defaultValue The default value.
|
||||
// * @return A String value.
|
||||
// */
|
||||
// public String optString(int index, String defaultValue) {
|
||||
// Object object = this.opt(index);
|
||||
// return JSON.NULL.equals(object)
|
||||
// ? defaultValue
|
||||
// : object.toString();
|
||||
// }
|
||||
|
||||
|
||||
@@ -1052,15 +1051,14 @@ public class JSONArray {
|
||||
* @param index must be between 0 and length() - 1
|
||||
* @return true if the value at the index is null, or if there is no value.
|
||||
*/
|
||||
// TODO not sure on this one
|
||||
protected boolean isNull(int index) {
|
||||
public boolean isNull(int index) {
|
||||
return JSONObject.NULL.equals(this.opt(index));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove an index and close the hole.
|
||||
*
|
||||
*
|
||||
* @webref jsonarray:method
|
||||
* @brief Removes an element
|
||||
* @param index the index value of the element to be removed
|
||||
@@ -1145,6 +1143,7 @@ public class JSONArray {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write the contents of the JSONArray as JSON text to a writer. For
|
||||
* compactness, no whitespace is added.
|
||||
@@ -1157,6 +1156,7 @@ public class JSONArray {
|
||||
return this.write(writer, -1, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Write the contents of the JSONArray as JSON text to a writer. For
|
||||
* compactness, no whitespace is added.
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -30,6 +30,10 @@ X automatically resize the list if necessary
|
||||
X (this is more in keeping with increment() in the Dict classes)
|
||||
X add join() method to Int/Float/StringList
|
||||
X add getContent(defaultValue) to XML
|
||||
X add isNull() (returns boolean) to JSONObject/Array
|
||||
X https://github.com/processing/processing/issues/2009
|
||||
X add getXxxx(xxx, defaultValue) methods to JSONObject/Array
|
||||
X https://github.com/processing/processing/issues/2007
|
||||
|
||||
cleaning
|
||||
X load/save methods.. is it save("blah.svg") or saveSVG("blah.svg")
|
||||
|
||||
2
todo.txt
2
todo.txt
@@ -445,8 +445,10 @@ _ add auto-save to the editor
|
||||
_ http://code.google.com/p/processing/issues/detail?id=92
|
||||
_ add mnemonics for menus (alt-f to open 'file')
|
||||
_ http://code.google.com/p/processing/issues/detail?id=12
|
||||
_ https://github.com/processing/processing/issues/51
|
||||
_ option to just print all code in project
|
||||
_ http://code.google.com/p/processing/issues/detail?id=11
|
||||
_ https://github.com/processing/processing/issues/50
|
||||
_ or option to export all the code as colored html?
|
||||
_ dim edit menus as appropriate during selection/no selection/etc
|
||||
_ http://code.google.com/p/processing/issues/detail?id=14
|
||||
|
||||
Reference in New Issue
Block a user