Skip to content

Commit 90563eb

Browse files
Sean LearySean Leary
authored andcommitted
1063-option-3 parserConfig set max len
1 parent 1efb5f6 commit 90563eb

5 files changed

Lines changed: 193 additions & 59 deletions

File tree

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2667,16 +2667,33 @@ protected static boolean isDecimalNotation(final String val) {
26672667
/**
26682668
* Try to convert a string into a number, boolean, or null. If the string
26692669
* can't be converted, return the string.
2670+
* Warning! stringToValue(String) uses the default max number length. If you want to override it,
2671+
* use a suitable initialized JSONParserConfiguration and the method: stringToValue(String, JSONParserConfiguration).
26702672
*
2671-
* @param string
2672-
* A String. can not be null.
2673+
* @param str A String. can not be null.
26732674
* @return A simple JSON value.
26742675
* @throws NullPointerException
26752676
* Thrown if the string is null.
26762677
*/
26772678
// Changes to this method must be copied to the corresponding method in
26782679
// the XML class to keep full support for Android
2679-
public static Object stringToValue(String string) {
2680+
public static Object stringToValue(String str) {
2681+
return stringToValue(str, new JSONParserConfiguration());
2682+
}
2683+
2684+
/**
2685+
* Try to convert a string into a number, boolean, or null. If the string
2686+
* can't be converted, return the string.
2687+
*
2688+
* @param string A String. can not be null.
2689+
* @param jsonParserConfiguration the parser config
2690+
* @return A simple JSON value. If the string represents a number that is too large,
2691+
* a string will be returned.
2692+
* @throws NullPointerException Thrown if the string is null.
2693+
*/
2694+
// Changes to this method must be copied to the corresponding method in
2695+
// the XML class to keep full support for Android
2696+
public static Object stringToValue(String string, JSONParserConfiguration jsonParserConfiguration) {
26802697
if ("".equals(string)) {
26812698
return string;
26822699
}
@@ -2700,7 +2717,14 @@ public static Object stringToValue(String string) {
27002717
char initial = string.charAt(0);
27012718
if ((initial >= '0' && initial <= '9') || initial == '-') {
27022719
try {
2703-
if (string.length() <= 1000) {
2720+
if (jsonParserConfiguration == null) {
2721+
jsonParserConfiguration = new JSONParserConfiguration();
2722+
}
2723+
// user declines max number checking
2724+
if (jsonParserConfiguration.getMaxNumberLength() == JSONParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH) {
2725+
return stringToNumber(string);
2726+
}
2727+
if (string.length() <= jsonParserConfiguration.getMaxNumberLength()) {
27042728
return stringToNumber(string);
27052729
}
27062730
} catch (Exception ignore) {

‎src/main/java/org/json/JSONTokener.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ Object nextSimpleValue(char c) {
513513
jsonParserConfiguration.isStrictMode() && string.endsWith(".")) {
514514
throw this.syntaxError(String.format("Strict mode error: Value '%s' ends with dot", string));
515515
}
516-
Object obj = JSONObject.stringToValue(string);
516+
Object obj = JSONObject.stringToValue(string, jsonParserConfiguration);
517517
// if obj is a boolean, look at string
518518
if (jsonParserConfiguration != null &&
519519
jsonParserConfiguration.isStrictMode()) {

‎src/main/java/org/json/ParserConfiguration.java‎

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,21 @@ public class ParserConfiguration {
1313
*/
1414
public static final int UNDEFINED_MAXIMUM_NESTING_DEPTH = -1;
1515

16+
/**
17+
* Used to indicate there's no defined limit to the maximum number length
18+
*/
19+
public static final int UNDEFINED_MAXIMUM_NUMBER_LENGTH = -1;
20+
1621
/**
1722
* The default maximum nesting depth when parsing a document.
1823
*/
1924
public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512;
2025

26+
/**
27+
* The default max number length
28+
*/
29+
public static final int DEFAULT_MAX_NUMBER_LENGTH = 1000;
30+
2131
/**
2232
* Specifies if values should be kept as strings (<code>true</code>), or if
2333
* they should try to be guessed into JSON values (numeric, boolean, string).
@@ -29,23 +39,31 @@ public class ParserConfiguration {
2939
*/
3040
protected int maxNestingDepth;
3141

42+
/**
43+
* The max number of chars for any number. Exceeding this limit will cause the value to be converted to a string
44+
*/
45+
protected int maxNumberLength;
46+
3247
/**
3348
* Constructs a new ParserConfiguration with default settings.
3449
*/
3550
public ParserConfiguration() {
3651
this.keepStrings = false;
3752
this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;
53+
this.maxNumberLength = DEFAULT_MAX_NUMBER_LENGTH;
3854
}
3955

4056
/**
41-
* Constructs a new ParserConfiguration with the specified settings.
57+
* Constructs a new ParserConfiguration with the specified settings. Use the with* methods instead of calling this ctor.
4258
*
4359
* @param keepStrings A boolean indicating whether to preserve strings during parsing.
4460
* @param maxNestingDepth An integer representing the maximum allowed nesting depth.
61+
* @deprecated
4562
*/
4663
protected ParserConfiguration(final boolean keepStrings, final int maxNestingDepth) {
4764
this.keepStrings = keepStrings;
4865
this.maxNestingDepth = maxNestingDepth;
66+
this.maxNumberLength = DEFAULT_MAX_NUMBER_LENGTH;
4967
}
5068

5169
/**
@@ -58,10 +76,11 @@ protected ParserConfiguration clone() {
5876
// item, a new map instance should be created and if possible each value in the
5977
// map should be cloned as well. If the values of the map are known to also
6078
// be immutable, then a shallow clone of the map is acceptable.
61-
return new ParserConfiguration(
62-
this.keepStrings,
63-
this.maxNestingDepth
64-
);
79+
ParserConfiguration parserConfiguration = new ParserConfiguration();
80+
parserConfiguration.keepStrings = this.keepStrings;
81+
parserConfiguration.maxNestingDepth = this.maxNestingDepth;
82+
parserConfiguration.maxNumberLength = this.maxNumberLength;
83+
return parserConfiguration;
6584
}
6685

6786
/**
@@ -123,4 +142,37 @@ public <T extends ParserConfiguration> T withMaxNestingDepth(int maxNestingDepth
123142

124143
return newConfig;
125144
}
145+
146+
147+
/**
148+
* The maximum number length that the parser will allow
149+
*
150+
* @return the maximum number lengtj set for this configuration
151+
*/
152+
public int getMaxNumberLength() {
153+
return maxNumberLength;
154+
}
155+
156+
/**
157+
* Defines the maximum number length that the parser will allow
158+
* Using any negative value as a parameter is equivalent to setting no limit to the length
159+
* which means any size number is allowed
160+
*
161+
* @param maxNumberLength the maximum number length allowed
162+
* @param <T> the type of the configuration object
163+
* @return The existing configuration will not be modified. A new configuration is returned.
164+
*/
165+
@SuppressWarnings("unchecked")
166+
public <T extends ParserConfiguration> T withMaxNumberLength(int maxNumberLength) {
167+
T newConfig = (T) this.clone();
168+
169+
if (maxNumberLength > UNDEFINED_MAXIMUM_NUMBER_LENGTH) {
170+
newConfig.maxNumberLength = maxNumberLength;
171+
} else {
172+
newConfig.maxNumberLength = UNDEFINED_MAXIMUM_NUMBER_LENGTH;
173+
}
174+
175+
return newConfig;
176+
}
177+
126178
}

‎src/main/java/org/json/XML.java‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -617,16 +617,32 @@ public static Object stringToValue(String string, XMLXsiTypeConverter<?> typeCon
617617
return stringToValue(string);
618618
}
619619

620+
/**
621+
* This method is the same as {@link JSONObject#stringToValue(String)}.
622+
* Warning! stringToValue(String) uses the default max number length. If you want to override it,
623+
* use a suitable initialized XMLParserConfiguration and the method: stringToValue(String, XMLParserConfiguration).
624+
* @param str String to convert
625+
* @return JSON value of this string or the string
626+
*/
627+
// To maintain compatibility with the Android API, this method is a direct copy of
628+
// the one in JSONObject. Changes made here should be reflected there.
629+
// This method should not make calls out of the XML object.
630+
public static Object stringToValue(String str) {
631+
return stringToValue(str, new XMLParserConfiguration());
632+
}
633+
620634
/**
621635
* This method is the same as {@link JSONObject#stringToValue(String)}.
622636
*
623637
* @param string String to convert
624-
* @return JSON value of this string or the string
638+
* @param xmlParserConfiguration the XML parser config object
639+
* @return JSON value of this string or the string. If the string represents a number that is too large,
640+
* a string will be returned.
625641
*/
626642
// To maintain compatibility with the Android API, this method is a direct copy of
627643
// the one in JSONObject. Changes made here should be reflected there.
628644
// This method should not make calls out of the XML object.
629-
public static Object stringToValue(String string) {
645+
public static Object stringToValue(String string, XMLParserConfiguration xmlParserConfiguration) {
630646
if ("".equals(string)) {
631647
return string;
632648
}
@@ -650,7 +666,11 @@ public static Object stringToValue(String string) {
650666
char initial = string.charAt(0);
651667
if ((initial >= '0' && initial <= '9') || initial == '-') {
652668
try {
653-
if(string.length() <= 1000) {
669+
// user declines max number checking
670+
if (xmlParserConfiguration.getMaxNumberLength() == JSONParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH) {
671+
return stringToNumber(string);
672+
}
673+
if(string.length() <= xmlParserConfiguration.getMaxNumberLength()) {
654674
return stringToNumber(string);
655675
}
656676
} catch (Exception ignore) {

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

Lines changed: 84 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
import org.json.junit.data.CustomClassH;
6868
import org.json.junit.data.CustomClassI;
6969
import org.json.junit.data.CustomClassJ;
70-
import org.json.JSONObject;
7170
import org.junit.After;
7271
import org.junit.Ignore;
7372
import 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

Comments
 (0)