@@ -3760,6 +3760,48 @@ public void issue743SerializationMapWith1001Objects() {
37603760 String jsonString = object .toString ();
37613761 }
37623762
3763+ @ Test (expected = JSONException .class )
3764+ public void testCircleReferenceFirstLevel () {
3765+ Map <Object , Object > jsonObject = new HashMap <>();
3766+
3767+ jsonObject .put ("test" , jsonObject );
3768+
3769+ new JSONObject (jsonObject , new JSONParserConfiguration ());
3770+ }
3771+
3772+ @ Test (expected = StackOverflowError .class )
3773+ public void testCircleReferenceMultiplyLevel_notConfigured_expectedStackOverflow () {
3774+ Map <Object , Object > inside = new HashMap <>();
3775+
3776+ Map <Object , Object > jsonObject = new HashMap <>();
3777+ inside .put ("test" , jsonObject );
3778+ jsonObject .put ("test" , inside );
3779+
3780+ new JSONObject (jsonObject , new JSONParserConfiguration ().withMaxNestingDepth (99999 ));
3781+ }
3782+
3783+ @ Test (expected = JSONException .class )
3784+ public void testCircleReferenceMultiplyLevel_configured_expectedJSONException () {
3785+ Map <Object , Object > inside = new HashMap <>();
3786+
3787+ Map <Object , Object > jsonObject = new HashMap <>();
3788+ inside .put ("test" , jsonObject );
3789+ jsonObject .put ("test" , inside );
3790+
3791+ new JSONObject (jsonObject , new JSONParserConfiguration ());
3792+ }
3793+
3794+ @ Test
3795+ public void testDifferentKeySameInstanceNotACircleReference () {
3796+ Map <Object , Object > map1 = new HashMap <>();
3797+ Map <Object , Object > map2 = new HashMap <>();
3798+
3799+ map1 .put ("test1" , map2 );
3800+ map1 .put ("test2" , map2 );
3801+
3802+ new JSONObject (map1 );
3803+ }
3804+
37633805 /**
37643806 * Method to build nested map of max maxDepth
37653807 *
0 commit comments