From db0fde2a566f5333a3ad2e70e6d21fc5680422f1 Mon Sep 17 00:00:00 2001 From: Edijs Date: Mon, 25 Sep 2023 20:18:33 +0300 Subject: [PATCH 1/2] Add optJSONArray method to JSONObject with a default value --- src/main/java/org/json/JSONObject.java | 18 ++++++++++++++++-- .../java/org/json/junit/JSONObjectTest.java | 4 ++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/json/JSONObject.java b/src/main/java/org/json/JSONObject.java index 5e00eb9..c6ac47a 100644 --- a/src/main/java/org/json/JSONObject.java +++ b/src/main/java/org/json/JSONObject.java @@ -1512,8 +1512,22 @@ public class JSONObject { * @return A JSONArray which is the value. */ public JSONArray optJSONArray(String key) { - Object o = this.opt(key); - return o instanceof JSONArray ? (JSONArray) o : null; + return this.optJSONArray(key, null); + } + + /** + * Get an optional JSONArray associated with a key, or the default if there + * is no such key, or if its value is not a JSONArray. + * + * @param key + * A key string. + * @param defaultValue + * The default. + * @return A JSONArray which is the value. + */ + public JSONArray optJSONArray(String key, JSONArray defaultValue) { + Object object = this.opt(key); + return object instanceof JSONArray ? (JSONArray) object : defaultValue; } /** diff --git a/src/test/java/org/json/junit/JSONObjectTest.java b/src/test/java/org/json/junit/JSONObjectTest.java index 2de8f81..07eb38b 100644 --- a/src/test/java/org/json/junit/JSONObjectTest.java +++ b/src/test/java/org/json/junit/JSONObjectTest.java @@ -2510,6 +2510,8 @@ public class JSONObjectTest { MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); assertTrue("optJSONArray() should return null ", null==jsonObject.optJSONArray("myKey")); + assertTrue("optJSONArray() should return default JSONArray", + "value".equals(jsonObject.optJSONArray("myKey", new JSONArray("[\"value\"]")).getString(0))); assertTrue("optJSONObject() should return default JSONObject ", jsonObject.optJSONObject("myKey", new JSONObject("{\"testKey\":\"testValue\"}")).getString("testKey").equals("testValue")); assertTrue("optLong() should return default long", @@ -2555,6 +2557,8 @@ public class JSONObjectTest { Integer.valueOf(42).equals(jsonObject.optIntegerObject("myKey", 42))); assertTrue("optEnum() should return default Enum", MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1))); + assertTrue("optJSONArray() should return default JSONArray", + "value".equals(jsonObject.optJSONArray("myKey", new JSONArray("[\"value\"]")).getString(0))); assertTrue("optJSONArray() should return null ", null==jsonObject.optJSONArray("myKey")); assertTrue("optJSONObject() should return default JSONObject ", From 284a31683898111b64ee5c92fa909b604bd9051d Mon Sep 17 00:00:00 2001 From: Edijs Date: Wed, 27 Sep 2023 19:30:45 +0300 Subject: [PATCH 2/2] Add optJSONArray and optJSONObject methods to JSONArray with a default value --- src/main/java/org/json/JSONArray.java | 47 +++++++++++++++---- .../java/org/json/junit/JSONArrayTest.java | 8 +++- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/json/JSONArray.java b/src/main/java/org/json/JSONArray.java index 375d038..cc9531e 100644 --- a/src/main/java/org/json/JSONArray.java +++ b/src/main/java/org/json/JSONArray.java @@ -924,30 +924,57 @@ public class JSONArray implements Iterable { } /** - * Get the optional JSONArray associated with an index. + * Get the optional JSONArray associated with an index. Null is returned if + * there is no value at that index or if the value is not a JSONArray. * * @param index - * subscript - * @return A JSONArray value, or null if the index has no value, or if the - * value is not a JSONArray. + * The index must be between 0 and length() - 1. + * @return A JSONArray value. */ public JSONArray optJSONArray(int index) { - Object o = this.opt(index); - return o instanceof JSONArray ? (JSONArray) o : null; + return this.optJSONArray(index, null); + } + + /** + * Get the optional JSONArray associated with an index. The defaultValue is returned if + * there is no value at that index or if the value is not a JSONArray. + * + * @param index + * The index must be between 0 and length() - 1. + * @param defaultValue + * The default. + * @return A JSONArray value. + */ + public JSONArray optJSONArray(int index, JSONArray defaultValue) { + Object object = this.opt(index); + return object instanceof JSONArray ? (JSONArray) object : defaultValue; } /** * 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. + * there is no value at that index or if the value is not a JSONObject. * * @param index * The index must be between 0 and length() - 1. * @return A JSONObject value. */ public JSONObject optJSONObject(int index) { - Object o = this.opt(index); - return o instanceof JSONObject ? (JSONObject) o : null; + return this.optJSONObject(index, null); + } + + /** + * Get the optional JSONObject associated with an index. The defaultValue is returned if + * there is no value at that index or if the value is not a JSONObject. + * + * @param index + * The index must be between 0 and length() - 1. + * @param defaultValue + * The default. + * @return A JSONObject value. + */ + public JSONObject optJSONObject(int index, JSONObject defaultValue) { + Object object = this.opt(index); + return object instanceof JSONObject ? (JSONObject) object : defaultValue; } /** diff --git a/src/test/java/org/json/junit/JSONArrayTest.java b/src/test/java/org/json/junit/JSONArrayTest.java index ad938cf..d735bf5 100644 --- a/src/test/java/org/json/junit/JSONArrayTest.java +++ b/src/test/java/org/json/junit/JSONArrayTest.java @@ -595,13 +595,17 @@ public class JSONArrayTest { JSONArray nestedJsonArray = jsonArray.optJSONArray(9); assertTrue("Array opt JSONArray", nestedJsonArray != null); - assertTrue("Array opt JSONArray default", + assertTrue("Array opt JSONArray null", null == jsonArray.optJSONArray(99)); + assertTrue("Array opt JSONArray default", + "value".equals(jsonArray.optJSONArray(99, new JSONArray("[\"value\"]")).getString(0))); JSONObject nestedJsonObject = jsonArray.optJSONObject(10); assertTrue("Array opt JSONObject", nestedJsonObject != null); - assertTrue("Array opt JSONObject default", + assertTrue("Array opt JSONObject null", null == jsonArray.optJSONObject(99)); + assertTrue("Array opt JSONObject default", + "value".equals(jsonArray.optJSONObject(99, new JSONObject("{\"key\":\"value\"}")).getString("key"))); assertTrue("Array opt long", 0 == jsonArray.optLong(11));