Skip to content

Commit

Permalink
Add optJSONArray and optJSONObject methods to JSONArray with a defaul…
Browse files Browse the repository at this point in the history
…t value
  • Loading branch information
eedijs committed Sep 27, 2023
1 parent db0fde2 commit 284a316
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
47 changes: 37 additions & 10 deletions src/main/java/org/json/JSONArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -924,30 +924,57 @@ public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
}

/**
* 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;
}

/**
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/org/json/junit/JSONArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -595,13 +595,17 @@ public void opt() {

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));
Expand Down

0 comments on commit 284a316

Please sign in to comment.