From d006c3a6a9320bc3a7c277253436bbc11647139f Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 31 Jan 2017 20:46:52 -0500 Subject: [PATCH] change some null handling with JSON --- core/src/processing/data/JSONObject.java | 38 +++++++++++++++++------- core/todo.txt | 7 +++++ todo.txt | 1 - 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/core/src/processing/data/JSONObject.java b/core/src/processing/data/JSONObject.java index 1c15b3aa1..768fdd7f8 100644 --- a/core/src/processing/data/JSONObject.java +++ b/core/src/processing/data/JSONObject.java @@ -539,11 +539,15 @@ public class JSONObject { */ public Object get(String key) { if (key == null) { - throw new RuntimeException("Null key."); + throw new RuntimeException("JSONObject.get(null) called"); } Object object = this.opt(key); if (object == null) { - throw new RuntimeException("JSONObject[" + quote(key) + "] not found."); + // Adding for rev 0257 in line with other p5 api + return null; + } + if (object == null) { + throw new RuntimeException("JSONObject[" + quote(key) + "] not found"); } return object; } @@ -563,10 +567,14 @@ public class JSONObject { */ public String getString(String key) { Object object = this.get(key); + if (object == null) { + // Adding for rev 0257 in line with other p5 api + return null; + } if (object instanceof String) { return (String)object; } - throw new RuntimeException("JSONObject[" + quote(key) + "] not a string."); + throw new RuntimeException("JSONObject[" + quote(key) + "] is not a string"); } @@ -599,10 +607,12 @@ public class JSONObject { */ public int getInt(String key) { Object object = this.get(key); + if (object == null) { + throw new RuntimeException("JSONObject[" + quote(key) + "] not found"); + } try { - return object instanceof Number - ? ((Number)object).intValue() - : Integer.parseInt((String)object); + return object instanceof Number ? + ((Number)object).intValue() : Integer.parseInt((String)object); } catch (Exception e) { throw new RuntimeException("JSONObject[" + quote(key) + "] is not an int."); } @@ -778,14 +788,17 @@ public class JSONObject { * @webref jsonobject:method * @brief Gets the JSONArray value associated with a key * @param key a key string - * @return A JSONArray which is the value. - * @throws RuntimeException if the key is not found or if the value is not a JSONArray. + * @return A JSONArray which is the value, or null if not present + * @throws RuntimeException if the value is not a JSONArray. * @see JSONObject#getJSONObject(String) * @see JSONObject#setJSONObject(String, JSONObject) * @see JSONObject#setJSONArray(String, JSONArray) */ public JSONArray getJSONArray(String key) { Object object = this.get(key); + if (object == null) { + return null; + } if (object instanceof JSONArray) { return (JSONArray)object; } @@ -799,14 +812,17 @@ public class JSONObject { * @webref jsonobject:method * @brief Gets the JSONObject value associated with a key * @param key a key string - * @return A JSONObject which is the value. - * @throws RuntimeException if the key is not found or if the value is not a JSONObject. + * @return A JSONObject which is the value or null if not available. + * @throws RuntimeException if the value is not a JSONObject. * @see JSONObject#getJSONArray(String) * @see JSONObject#setJSONObject(String, JSONObject) * @see JSONObject#setJSONArray(String, JSONArray) */ public JSONObject getJSONObject(String key) { Object object = this.get(key); + if (object == null) { + return null; + } if (object instanceof JSONObject) { return (JSONObject)object; } @@ -864,7 +880,7 @@ public class JSONObject { * @return true if the key exists in the JSONObject. */ public boolean hasKey(String key) { - return this.map.containsKey(key); + return map.containsKey(key); } diff --git a/core/todo.txt b/core/todo.txt index e0f598d8b..4b370f4e4 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -12,6 +12,13 @@ X consume Unicode BOM (0xFEFF) in createReader() and Table parser o no prompt shows with selectInput() on 10.11 and 10.12 X https://github.com/processing/processing/issues/4758 X can't fix, seems embedded in the Java implementation +X return null for getString(), getJSONObject(), and getJSONArray() +X when key is not present, more in line w/ other p5 api + +contrib +X Fix a number of memory leaks (jdf) +X https://github.com/processing/processing/pull/4862 + _ TRIANGLE_STRIP not working correctly with createShape() and default renderer _ https://github.com/processing/processing/issues/4678 diff --git a/todo.txt b/todo.txt index 2b46ba1e8..a5c57e307 100755 --- a/todo.txt +++ b/todo.txt @@ -2,7 +2,6 @@ X check for already-exported folders before trying to remove them X was spewing 'file not found' errors into the console - cleaning X Contribution Manager does not show all libraries until filter cleared X https://github.com/processing/processing/issues/4840