6767import org .json .junit .data .CustomClassH ;
6868import org .json .junit .data .CustomClassI ;
6969import org .json .junit .data .CustomClassJ ;
70- import org .json .JSONObject ;
7170import org .junit .After ;
7271import org .junit .Ignore ;
7372import org .junit .Test ;
@@ -3801,33 +3800,84 @@ public void testMaxNumberLength() {
38013800 sb .append ("9999999999" );
38023801 }
38033802
3804- // edge case: JSONObject with number just under the max length limit
3805- String s1Object = "{ \" a\" : " + sb + "}" ;
3806- JSONObject jsonObject1 = new JSONObject (s1Object );
3807- Object obj1Object = jsonObject1 .get ("a" );
3808- assertTrue (obj1Object instanceof Number );
3809- assertEquals (1000 , obj1Object .toString ().length ());
3810-
3811- // edge case: JSONArray with number just under the max length limit
3812- String s1Array = "[" + sb + "]" ;
3813- JSONArray jsonArray1 = new JSONArray (s1Array );
3814- Object obj1Array = jsonArray1 .get (0 );
3815- assertTrue (obj1Array instanceof Number );
3816- assertEquals (1000 , obj1Array .toString ().length ());
3817-
3818- // edge case: JSONObject with number just over the max length limit
3819- String s2Object = "{ \" a\" : " + sb + "9}" ;
3820- JSONObject jsonObject2 = new JSONObject (s2Object );
3821- Object obj2Object = jsonObject2 .get ("a" );
3822- assertTrue (obj2Object instanceof String );
3823- assertEquals (1001 , ((String ) obj2Object ).length ());
3824-
3825- // edge case: JSONArray with number just over the max length limit
3826- String s2Array = "[" + sb + "9]" ;
3827- JSONArray jsonArray2 = new JSONArray (s2Array );
3828- Object obj2Array = jsonArray2 .get (0 );
3829- assertTrue (obj2Array instanceof String );
3830- assertEquals (1001 , ((String ) obj2Array ).length ());
3803+ // JSONObject with number just under the max length limit
3804+ checkJSONObjectMaxLen (sb .toString (), true , null );
3805+
3806+ // JSONArray with number just under the max length limit
3807+ checkJSONArrayMaxLen (sb .toString (), true , null );
3808+
3809+ // JSONObject with number just over the max length limit
3810+ checkJSONObjectMaxLen (sb + "9" , false , null );
3811+
3812+ // JSONArray with number just over the max length limit
3813+ checkJSONArrayMaxLen (sb + "9" , false , null );
3814+
3815+ // JSONObject with number at config max length limit
3816+ checkJSONObjectMaxLen (sb .toString () + sb .toString (), true , new JSONParserConfiguration ().withMaxNumberLength (2000 ));
3817+
3818+ // JSONArray with number at config max length limit
3819+ checkJSONArrayMaxLen ((sb .toString () + sb ), true , new JSONParserConfiguration ().withMaxNumberLength (2000 ));
3820+
3821+ // JSONObject with number just over config max length limit
3822+ checkJSONObjectMaxLen (sb .toString () + sb .toString () + "9" , false , new JSONParserConfiguration ().withMaxNumberLength (2000 ));
3823+
3824+ // JSONArray with number just over config max length limit
3825+ checkJSONArrayMaxLen ((sb .toString () + sb + "9" ), false , new JSONParserConfiguration ().withMaxNumberLength (2000 ));
3826+
3827+ // JSONObject with large number, no checks
3828+ checkJSONObjectMaxLen (sb .toString () + sb .toString () + "9" , true ,
3829+ new JSONParserConfiguration ().withMaxNumberLength (JSONParserConfiguration .UNDEFINED_MAXIMUM_NUMBER_LENGTH ));
3830+
3831+ // JSONArray with number just over config max length limit
3832+ checkJSONArrayMaxLen ((sb .toString () + sb + "9" ), true ,
3833+ new JSONParserConfiguration ().withMaxNumberLength (JSONParserConfiguration .UNDEFINED_MAXIMUM_NUMBER_LENGTH ));
3834+ }
3835+
3836+ /**
3837+ * Convenience method to check JSONObject for a long number
3838+ * @param s string containing the number digits to check
3839+ * @param isValid true if number is valid, otherwise false
3840+ * @param jsonParserConfiguration the config object
3841+ */
3842+ private static void checkJSONObjectMaxLen (String s , boolean isValid , JSONParserConfiguration jsonParserConfiguration ) {
3843+ String str = "{ \" a\" : " + s + "}" ;
3844+ JSONObject jsonObject ;
3845+ if (jsonParserConfiguration == null ) {
3846+ jsonObject = new JSONObject (str );
3847+ } else {
3848+ jsonObject = new JSONObject (str , jsonParserConfiguration );
3849+ }
3850+ Object obj = jsonObject .get ("a" );
3851+ if (isValid ) {
3852+ assertTrue (obj instanceof Number );
3853+ } else {
3854+ assertTrue (obj instanceof String );
3855+ }
3856+ // may not work for scientific notation and BigDecimal
3857+ // assertEquals(s.length(), obj.toString().length());
3858+ }
3859+
3860+ /**
3861+ * Convenience method to check JSONArray for a long number
3862+ * @param s string containing the number digits to check
3863+ * @param isValid true if number is valid, otherwise false
3864+ */
3865+ private static void checkJSONArrayMaxLen (String s , boolean isValid , JSONParserConfiguration jsonParserConfiguration ) {
3866+ String str = "[" + s + "]" ;
3867+ JSONArray jsonArray ;
3868+ if (jsonParserConfiguration == null ) {
3869+ jsonArray = new JSONArray (str );
3870+ } else {
3871+ jsonArray = new JSONArray (str , jsonParserConfiguration );
3872+ }
3873+ Object obj = jsonArray .get (0 );
3874+ if (isValid ) {
3875+ assertTrue (obj instanceof Number );
3876+ } else {
3877+ assertTrue (obj instanceof String );
3878+ }
3879+ // may not work for scientific notation and BigDecimal
3880+ // assertEquals(s.length(), obj.toString().length());
38313881 }
38323882
38333883 /**
@@ -3843,16 +3893,12 @@ public void testMaxNumberLengthNegativeInteger() {
38433893 assertEquals (1000 , sb .length ());
38443894
38453895 // at max length: parsed as number
3846- JSONObject jo = new JSONObject ("{ \" a\" : " + sb + "}" );
3847- Object val = jo .get ("a" );
3848- assertTrue ("Expected Number but got " + val .getClass (), val instanceof Number );
3896+ checkJSONObjectMaxLen (sb .toString (), true , null );
38493897
38503898 // over max length: returned as string
38513899 sb .append ("1" );
38523900 assertEquals (1001 , sb .length ());
3853- JSONObject jo2 = new JSONObject ("{ \" a\" : " + sb + "}" );
3854- Object val2 = jo2 .get ("a" );
3855- assertTrue ("Expected String but got " + val2 .getClass (), val2 instanceof String );
3901+ checkJSONObjectMaxLen (sb .toString (), false , null );
38563902 }
38573903
38583904 /**
@@ -3873,16 +3919,12 @@ public void testMaxNumberLengthDecimal() {
38733919 assertEquals (1000 , sb .length ());
38743920
38753921 // at max length: parsed as number (BigDecimal)
3876- JSONObject jo = new JSONObject ("{ \" a\" : " + sb + "}" );
3877- Object val = jo .get ("a" );
3878- assertTrue ("Expected Number but got " + val .getClass (), val instanceof Number );
3922+ checkJSONObjectMaxLen (sb .toString (), true , null );
38793923
38803924 // over max length: returned as string
38813925 sb .append ("3" );
38823926 assertEquals (1001 , sb .length ());
3883- JSONObject jo2 = new JSONObject ("{ \" a\" : " + sb + "}" );
3884- Object val2 = jo2 .get ("a" );
3885- assertTrue ("Expected String but got " + val2 .getClass (), val2 instanceof String );
3927+ checkJSONObjectMaxLen (sb .toString (), false , null );
38863928 }
38873929
38883930 /**
@@ -3899,9 +3941,7 @@ public void testMaxNumberLengthScientificNotation() {
38993941 assertEquals (1000 , sb .length ());
39003942
39013943 // at max length: parsed as number
3902- JSONObject jo = new JSONObject ("{ \" a\" : " + sb + "}" );
3903- Object val = jo .get ("a" );
3904- assertTrue ("Expected Number but got " + val .getClass (), val instanceof Number );
3944+ checkJSONObjectMaxLen (sb .toString (), true , null );
39053945
39063946 // over max length: returned as string
39073947 sb = new StringBuilder ("1." );
@@ -3910,9 +3950,7 @@ public void testMaxNumberLengthScientificNotation() {
39103950 }
39113951 sb .append ("e100" );
39123952 assertEquals (1001 , sb .length ());
3913- JSONObject jo2 = new JSONObject ("{ \" a\" : " + sb + "}" );
3914- Object val2 = jo2 .get ("a" );
3915- assertTrue ("Expected String but got " + val2 .getClass (), val2 instanceof String );
3953+ checkJSONObjectMaxLen (sb .toString (), false , null );
39163954 }
39173955
39183956 /**
0 commit comments