Skip to content

Commit ef68cdf

Browse files
authored
Merge pull request #773 from eedijs/master
Add optJSONArray method to JSONObject with a default value
2 parents 4e8231c + 284a316 commit ef68cdf

File tree

4 files changed

+63
-14
lines changed

4 files changed

+63
-14
lines changed

src/main/java/org/json/JSONArray.java

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -924,30 +924,57 @@ public BigDecimal optBigDecimal(int index, BigDecimal defaultValue) {
924924
}
925925

926926
/**
927-
* Get the optional JSONArray associated with an index.
927+
* Get the optional JSONArray associated with an index. Null is returned if
928+
* there is no value at that index or if the value is not a JSONArray.
928929
*
929930
* @param index
930-
* subscript
931-
* @return A JSONArray value, or null if the index has no value, or if the
932-
* value is not a JSONArray.
931+
* The index must be between 0 and length() - 1.
932+
* @return A JSONArray value.
933933
*/
934934
public JSONArray optJSONArray(int index) {
935-
Object o = this.opt(index);
936-
return o instanceof JSONArray ? (JSONArray) o : null;
935+
return this.optJSONArray(index, null);
936+
}
937+
938+
/**
939+
* Get the optional JSONArray associated with an index. The defaultValue is returned if
940+
* there is no value at that index or if the value is not a JSONArray.
941+
*
942+
* @param index
943+
* The index must be between 0 and length() - 1.
944+
* @param defaultValue
945+
* The default.
946+
* @return A JSONArray value.
947+
*/
948+
public JSONArray optJSONArray(int index, JSONArray defaultValue) {
949+
Object object = this.opt(index);
950+
return object instanceof JSONArray ? (JSONArray) object : defaultValue;
937951
}
938952

939953
/**
940954
* Get the optional JSONObject associated with an index. Null is returned if
941-
* the key is not found, or null if the index has no value, or if the value
942-
* is not a JSONObject.
955+
* there is no value at that index or if the value is not a JSONObject.
943956
*
944957
* @param index
945958
* The index must be between 0 and length() - 1.
946959
* @return A JSONObject value.
947960
*/
948961
public JSONObject optJSONObject(int index) {
949-
Object o = this.opt(index);
950-
return o instanceof JSONObject ? (JSONObject) o : null;
962+
return this.optJSONObject(index, null);
963+
}
964+
965+
/**
966+
* Get the optional JSONObject associated with an index. The defaultValue is returned if
967+
* there is no value at that index or if the value is not a JSONObject.
968+
*
969+
* @param index
970+
* The index must be between 0 and length() - 1.
971+
* @param defaultValue
972+
* The default.
973+
* @return A JSONObject value.
974+
*/
975+
public JSONObject optJSONObject(int index, JSONObject defaultValue) {
976+
Object object = this.opt(index);
977+
return object instanceof JSONObject ? (JSONObject) object : defaultValue;
951978
}
952979

953980
/**

src/main/java/org/json/JSONObject.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,8 +1512,22 @@ public Integer optIntegerObject(String key, Integer defaultValue) {
15121512
* @return A JSONArray which is the value.
15131513
*/
15141514
public JSONArray optJSONArray(String key) {
1515-
Object o = this.opt(key);
1516-
return o instanceof JSONArray ? (JSONArray) o : null;
1515+
return this.optJSONArray(key, null);
1516+
}
1517+
1518+
/**
1519+
* Get an optional JSONArray associated with a key, or the default if there
1520+
* is no such key, or if its value is not a JSONArray.
1521+
*
1522+
* @param key
1523+
* A key string.
1524+
* @param defaultValue
1525+
* The default.
1526+
* @return A JSONArray which is the value.
1527+
*/
1528+
public JSONArray optJSONArray(String key, JSONArray defaultValue) {
1529+
Object object = this.opt(key);
1530+
return object instanceof JSONArray ? (JSONArray) object : defaultValue;
15171531
}
15181532

15191533
/**

src/test/java/org/json/junit/JSONArrayTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,13 +595,17 @@ public void opt() {
595595

596596
JSONArray nestedJsonArray = jsonArray.optJSONArray(9);
597597
assertTrue("Array opt JSONArray", nestedJsonArray != null);
598-
assertTrue("Array opt JSONArray default",
598+
assertTrue("Array opt JSONArray null",
599599
null == jsonArray.optJSONArray(99));
600+
assertTrue("Array opt JSONArray default",
601+
"value".equals(jsonArray.optJSONArray(99, new JSONArray("[\"value\"]")).getString(0)));
600602

601603
JSONObject nestedJsonObject = jsonArray.optJSONObject(10);
602604
assertTrue("Array opt JSONObject", nestedJsonObject != null);
603-
assertTrue("Array opt JSONObject default",
605+
assertTrue("Array opt JSONObject null",
604606
null == jsonArray.optJSONObject(99));
607+
assertTrue("Array opt JSONObject default",
608+
"value".equals(jsonArray.optJSONObject(99, new JSONObject("{\"key\":\"value\"}")).getString("key")));
605609

606610
assertTrue("Array opt long",
607611
0 == jsonArray.optLong(11));

src/test/java/org/json/junit/JSONObjectTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,8 @@ public void jsonObjectOptDefault() {
25102510
MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1)));
25112511
assertTrue("optJSONArray() should return null ",
25122512
null==jsonObject.optJSONArray("myKey"));
2513+
assertTrue("optJSONArray() should return default JSONArray",
2514+
"value".equals(jsonObject.optJSONArray("myKey", new JSONArray("[\"value\"]")).getString(0)));
25132515
assertTrue("optJSONObject() should return default JSONObject ",
25142516
jsonObject.optJSONObject("myKey", new JSONObject("{\"testKey\":\"testValue\"}")).getString("testKey").equals("testValue"));
25152517
assertTrue("optLong() should return default long",
@@ -2555,6 +2557,8 @@ public void jsonObjectOptNoKey() {
25552557
Integer.valueOf(42).equals(jsonObject.optIntegerObject("myKey", 42)));
25562558
assertTrue("optEnum() should return default Enum",
25572559
MyEnum.VAL1.equals(jsonObject.optEnum(MyEnum.class, "myKey", MyEnum.VAL1)));
2560+
assertTrue("optJSONArray() should return default JSONArray",
2561+
"value".equals(jsonObject.optJSONArray("myKey", new JSONArray("[\"value\"]")).getString(0)));
25582562
assertTrue("optJSONArray() should return null ",
25592563
null==jsonObject.optJSONArray("myKey"));
25602564
assertTrue("optJSONObject() should return default JSONObject ",

0 commit comments

Comments
 (0)