Skip to content

Commit dcac3bc

Browse files
committed
Adding test case for nested json with depth of 999, 1000, 1001
1 parent 6d81160 commit dcac3bc

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,4 +1438,56 @@ public void testRecursiveDepthArray() {
14381438
array.add(array);
14391439
new JSONArray(array);
14401440
}
1441+
1442+
@Test
1443+
public void testRecursiveDepthAtPosition999Object() {
1444+
HashMap<String, Object> map = JSONObjectTest.buildNestedMap(999);
1445+
new JSONArray().put(0, map);
1446+
}
1447+
1448+
@Test
1449+
public void testRecursiveDepthAtPosition1000Object() {
1450+
HashMap<String, Object> map = JSONObjectTest.buildNestedMap(1000);
1451+
new JSONArray().put(0, map);
1452+
}
1453+
1454+
@Test(expected = JSONException.class)
1455+
public void testRecursiveDepthAtPosition1001Object() {
1456+
HashMap<String, Object> map = JSONObjectTest.buildNestedMap(1001);
1457+
new JSONArray().put(0, map);
1458+
}
1459+
1460+
@Test(expected = JSONException.class)
1461+
public void testRecursiveDepthArrayLimitedMaps() {
1462+
ArrayList<Object> array = new ArrayList<>();
1463+
array.add(array);
1464+
new JSONArray(array);
1465+
}
1466+
1467+
@Test
1468+
public void testRecursiveDepthArrayFor999Levels() {
1469+
ArrayList<Object> array = buildNestedArray(999);
1470+
new JSONArray(array);
1471+
}
1472+
1473+
@Test
1474+
public void testRecursiveDepthArrayFor1000Levels() {
1475+
ArrayList<Object> array = buildNestedArray(1000);
1476+
new JSONArray(array);
1477+
}
1478+
1479+
@Test(expected = JSONException.class)
1480+
public void testRecursiveDepthArrayFor1001Levels() {
1481+
ArrayList<Object> array = buildNestedArray(1001);
1482+
new JSONArray(array);
1483+
}
1484+
1485+
public static ArrayList<Object> buildNestedArray(int maxDepth) {
1486+
if (maxDepth <= 0) {
1487+
return new ArrayList<>();
1488+
}
1489+
ArrayList<Object> nestedArray = new ArrayList<>();
1490+
nestedArray.add(buildNestedArray(maxDepth - 1));
1491+
return nestedArray;
1492+
}
14411493
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3735,4 +3735,41 @@ public void testCircularReferenceMultipleLevel() {
37353735
jsonObject.put("test", inside);
37363736
new JSONObject(jsonObject);
37373737
}
3738+
3739+
@Test
3740+
public void issue743SerializationMapWith999Objects() {
3741+
HashMap<String, Object> map = buildNestedMap(999);
3742+
JSONObject object = new JSONObject(map);
3743+
String jsonString = object.toString();
3744+
}
3745+
3746+
@Test
3747+
public void issue743SerializationMapWith1000Objects() {
3748+
HashMap<String, Object> map = buildNestedMap(1000);
3749+
JSONObject object = new JSONObject(map);
3750+
String jsonString = object.toString();
3751+
}
3752+
3753+
@Test(expected = JSONException.class)
3754+
public void issue743SerializationMapWith1001Objects() {
3755+
HashMap<String, Object> map = buildNestedMap(1001);
3756+
JSONObject object = new JSONObject(map);
3757+
String jsonString = object.toString();
3758+
}
3759+
3760+
/**
3761+
* Method to build nested map of max maxDepth
3762+
*
3763+
* @param maxDepth
3764+
* @return
3765+
*/
3766+
public static HashMap<String, Object> buildNestedMap(int maxDepth) {
3767+
if (maxDepth <= 0) {
3768+
return new HashMap<>();
3769+
}
3770+
HashMap<String, Object> nestedMap = new HashMap<>();
3771+
nestedMap.put("t", buildNestedMap(maxDepth - 1));
3772+
return nestedMap;
3773+
}
3774+
37383775
}

0 commit comments

Comments
 (0)