1063-option-3 parserConfig set max len
This commit is contained in:
@@ -2667,16 +2667,33 @@ public class JSONObject {
|
||||
/**
|
||||
* Try to convert a string into a number, boolean, or null. If the string
|
||||
* can't be converted, return the string.
|
||||
* Warning! stringToValue(String) uses the default max number length. If you want to override it,
|
||||
* use a suitable initialized JSONParserConfiguration and the method: stringToValue(String, JSONParserConfiguration).
|
||||
*
|
||||
* @param string
|
||||
* A String. can not be null.
|
||||
* @param str A String. can not be null.
|
||||
* @return A simple JSON value.
|
||||
* @throws NullPointerException
|
||||
* Thrown if the string is null.
|
||||
*/
|
||||
// Changes to this method must be copied to the corresponding method in
|
||||
// the XML class to keep full support for Android
|
||||
public static Object stringToValue(String string) {
|
||||
public static Object stringToValue(String str) {
|
||||
return stringToValue(str, new JSONParserConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to convert a string into a number, boolean, or null. If the string
|
||||
* can't be converted, return the string.
|
||||
*
|
||||
* @param string A String. can not be null.
|
||||
* @param jsonParserConfiguration the parser config
|
||||
* @return A simple JSON value. If the string represents a number that is too large,
|
||||
* a string will be returned.
|
||||
* @throws NullPointerException Thrown if the string is null.
|
||||
*/
|
||||
// Changes to this method must be copied to the corresponding method in
|
||||
// the XML class to keep full support for Android
|
||||
public static Object stringToValue(String string, JSONParserConfiguration jsonParserConfiguration) {
|
||||
if ("".equals(string)) {
|
||||
return string;
|
||||
}
|
||||
@@ -2700,7 +2717,14 @@ public class JSONObject {
|
||||
char initial = string.charAt(0);
|
||||
if ((initial >= '0' && initial <= '9') || initial == '-') {
|
||||
try {
|
||||
if (string.length() <= 1000) {
|
||||
if (jsonParserConfiguration == null) {
|
||||
jsonParserConfiguration = new JSONParserConfiguration();
|
||||
}
|
||||
// user declines max number checking
|
||||
if (jsonParserConfiguration.getMaxNumberLength() == JSONParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH) {
|
||||
return stringToNumber(string);
|
||||
}
|
||||
if (string.length() <= jsonParserConfiguration.getMaxNumberLength()) {
|
||||
return stringToNumber(string);
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
|
||||
@@ -513,7 +513,7 @@ public class JSONTokener {
|
||||
jsonParserConfiguration.isStrictMode() && string.endsWith(".")) {
|
||||
throw this.syntaxError(String.format("Strict mode error: Value '%s' ends with dot", string));
|
||||
}
|
||||
Object obj = JSONObject.stringToValue(string);
|
||||
Object obj = JSONObject.stringToValue(string, jsonParserConfiguration);
|
||||
// if obj is a boolean, look at string
|
||||
if (jsonParserConfiguration != null &&
|
||||
jsonParserConfiguration.isStrictMode()) {
|
||||
|
||||
@@ -13,11 +13,21 @@ public class ParserConfiguration {
|
||||
*/
|
||||
public static final int UNDEFINED_MAXIMUM_NESTING_DEPTH = -1;
|
||||
|
||||
/**
|
||||
* Used to indicate there's no defined limit to the maximum number length
|
||||
*/
|
||||
public static final int UNDEFINED_MAXIMUM_NUMBER_LENGTH = -1;
|
||||
|
||||
/**
|
||||
* The default maximum nesting depth when parsing a document.
|
||||
*/
|
||||
public static final int DEFAULT_MAXIMUM_NESTING_DEPTH = 512;
|
||||
|
||||
/**
|
||||
* The default max number length
|
||||
*/
|
||||
public static final int DEFAULT_MAX_NUMBER_LENGTH = 1000;
|
||||
|
||||
/**
|
||||
* Specifies if values should be kept as strings (<code>true</code>), or if
|
||||
* they should try to be guessed into JSON values (numeric, boolean, string).
|
||||
@@ -29,23 +39,31 @@ public class ParserConfiguration {
|
||||
*/
|
||||
protected int maxNestingDepth;
|
||||
|
||||
/**
|
||||
* The max number of chars for any number. Exceeding this limit will cause the value to be converted to a string
|
||||
*/
|
||||
protected int maxNumberLength;
|
||||
|
||||
/**
|
||||
* Constructs a new ParserConfiguration with default settings.
|
||||
*/
|
||||
public ParserConfiguration() {
|
||||
this.keepStrings = false;
|
||||
this.maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;
|
||||
this.maxNumberLength = DEFAULT_MAX_NUMBER_LENGTH;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new ParserConfiguration with the specified settings.
|
||||
* Constructs a new ParserConfiguration with the specified settings. Use the with* methods instead of calling this ctor.
|
||||
*
|
||||
* @param keepStrings A boolean indicating whether to preserve strings during parsing.
|
||||
* @param maxNestingDepth An integer representing the maximum allowed nesting depth.
|
||||
* @deprecated
|
||||
*/
|
||||
protected ParserConfiguration(final boolean keepStrings, final int maxNestingDepth) {
|
||||
this.keepStrings = keepStrings;
|
||||
this.maxNestingDepth = maxNestingDepth;
|
||||
this.maxNumberLength = DEFAULT_MAX_NUMBER_LENGTH;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,10 +76,11 @@ public class ParserConfiguration {
|
||||
// item, a new map instance should be created and if possible each value in the
|
||||
// map should be cloned as well. If the values of the map are known to also
|
||||
// be immutable, then a shallow clone of the map is acceptable.
|
||||
return new ParserConfiguration(
|
||||
this.keepStrings,
|
||||
this.maxNestingDepth
|
||||
);
|
||||
ParserConfiguration parserConfiguration = new ParserConfiguration();
|
||||
parserConfiguration.keepStrings = this.keepStrings;
|
||||
parserConfiguration.maxNestingDepth = this.maxNestingDepth;
|
||||
parserConfiguration.maxNumberLength = this.maxNumberLength;
|
||||
return parserConfiguration;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,4 +142,37 @@ public class ParserConfiguration {
|
||||
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The maximum number length that the parser will allow
|
||||
*
|
||||
* @return the maximum number lengtj set for this configuration
|
||||
*/
|
||||
public int getMaxNumberLength() {
|
||||
return maxNumberLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* Defines the maximum number length that the parser will allow
|
||||
* Using any negative value as a parameter is equivalent to setting no limit to the length
|
||||
* which means any size number is allowed
|
||||
*
|
||||
* @param maxNumberLength the maximum number length allowed
|
||||
* @param <T> the type of the configuration object
|
||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends ParserConfiguration> T withMaxNumberLength(int maxNumberLength) {
|
||||
T newConfig = (T) this.clone();
|
||||
|
||||
if (maxNumberLength > UNDEFINED_MAXIMUM_NUMBER_LENGTH) {
|
||||
newConfig.maxNumberLength = maxNumberLength;
|
||||
} else {
|
||||
newConfig.maxNumberLength = UNDEFINED_MAXIMUM_NUMBER_LENGTH;
|
||||
}
|
||||
|
||||
return newConfig;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -619,14 +619,30 @@ public class XML {
|
||||
|
||||
/**
|
||||
* This method is the same as {@link JSONObject#stringToValue(String)}.
|
||||
*
|
||||
* @param string String to convert
|
||||
* Warning! stringToValue(String) uses the default max number length. If you want to override it,
|
||||
* use a suitable initialized XMLParserConfiguration and the method: stringToValue(String, XMLParserConfiguration).
|
||||
* @param str String to convert
|
||||
* @return JSON value of this string or the string
|
||||
*/
|
||||
// To maintain compatibility with the Android API, this method is a direct copy of
|
||||
// the one in JSONObject. Changes made here should be reflected there.
|
||||
// This method should not make calls out of the XML object.
|
||||
public static Object stringToValue(String string) {
|
||||
public static Object stringToValue(String str) {
|
||||
return stringToValue(str, new XMLParserConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is the same as {@link JSONObject#stringToValue(String)}.
|
||||
*
|
||||
* @param string String to convert
|
||||
* @param xmlParserConfiguration the XML parser config object
|
||||
* @return JSON value of this string or the string. If the string represents a number that is too large,
|
||||
* a string will be returned.
|
||||
*/
|
||||
// To maintain compatibility with the Android API, this method is a direct copy of
|
||||
// the one in JSONObject. Changes made here should be reflected there.
|
||||
// This method should not make calls out of the XML object.
|
||||
public static Object stringToValue(String string, XMLParserConfiguration xmlParserConfiguration) {
|
||||
if ("".equals(string)) {
|
||||
return string;
|
||||
}
|
||||
@@ -650,7 +666,11 @@ public class XML {
|
||||
char initial = string.charAt(0);
|
||||
if ((initial >= '0' && initial <= '9') || initial == '-') {
|
||||
try {
|
||||
if(string.length() <= 1000) {
|
||||
// user declines max number checking
|
||||
if (xmlParserConfiguration.getMaxNumberLength() == JSONParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH) {
|
||||
return stringToNumber(string);
|
||||
}
|
||||
if(string.length() <= xmlParserConfiguration.getMaxNumberLength()) {
|
||||
return stringToNumber(string);
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
|
||||
@@ -67,7 +67,6 @@ import org.json.junit.data.CustomClassG;
|
||||
import org.json.junit.data.CustomClassH;
|
||||
import org.json.junit.data.CustomClassI;
|
||||
import org.json.junit.data.CustomClassJ;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.After;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
@@ -3801,33 +3800,84 @@ public class JSONObjectTest {
|
||||
sb.append("9999999999");
|
||||
}
|
||||
|
||||
// edge case: JSONObject with number just under the max length limit
|
||||
String s1Object = "{ \"a\": " + sb + "}";
|
||||
JSONObject jsonObject1 = new JSONObject(s1Object);
|
||||
Object obj1Object = jsonObject1.get("a");
|
||||
assertTrue(obj1Object instanceof Number);
|
||||
assertEquals(1000, obj1Object.toString().length());
|
||||
// JSONObject with number just under the max length limit
|
||||
checkJSONObjectMaxLen(sb.toString(), true, null);
|
||||
|
||||
// edge case: JSONArray with number just under the max length limit
|
||||
String s1Array = "[" + sb + "]";
|
||||
JSONArray jsonArray1 = new JSONArray(s1Array);
|
||||
Object obj1Array = jsonArray1.get(0);
|
||||
assertTrue(obj1Array instanceof Number);
|
||||
assertEquals(1000, obj1Array.toString().length());
|
||||
// JSONArray with number just under the max length limit
|
||||
checkJSONArrayMaxLen(sb.toString(), true, null);
|
||||
|
||||
// edge case: JSONObject with number just over the max length limit
|
||||
String s2Object = "{ \"a\": " + sb + "9}";
|
||||
JSONObject jsonObject2 = new JSONObject(s2Object);
|
||||
Object obj2Object = jsonObject2.get("a");
|
||||
assertTrue(obj2Object instanceof String);
|
||||
assertEquals(1001, ((String) obj2Object).length());
|
||||
// JSONObject with number just over the max length limit
|
||||
checkJSONObjectMaxLen(sb + "9", false, null);
|
||||
|
||||
// edge case: JSONArray with number just over the max length limit
|
||||
String s2Array = "[" + sb + "9]";
|
||||
JSONArray jsonArray2 = new JSONArray(s2Array);
|
||||
Object obj2Array = jsonArray2.get(0);
|
||||
assertTrue(obj2Array instanceof String);
|
||||
assertEquals(1001, ((String) obj2Array).length());
|
||||
// JSONArray with number just over the max length limit
|
||||
checkJSONArrayMaxLen(sb + "9", false, null);
|
||||
|
||||
// JSONObject with number at config max length limit
|
||||
checkJSONObjectMaxLen(sb.toString() + sb.toString(), true, new JSONParserConfiguration().withMaxNumberLength(2000));
|
||||
|
||||
// JSONArray with number at config max length limit
|
||||
checkJSONArrayMaxLen((sb.toString() + sb), true, new JSONParserConfiguration().withMaxNumberLength(2000));
|
||||
|
||||
// JSONObject with number just over config max length limit
|
||||
checkJSONObjectMaxLen(sb.toString() + sb.toString() + "9", false, new JSONParserConfiguration().withMaxNumberLength(2000));
|
||||
|
||||
// JSONArray with number just over config max length limit
|
||||
checkJSONArrayMaxLen((sb.toString() + sb + "9"), false, new JSONParserConfiguration().withMaxNumberLength(2000));
|
||||
|
||||
// JSONObject with large number, no checks
|
||||
checkJSONObjectMaxLen(sb.toString() + sb.toString() + "9", true,
|
||||
new JSONParserConfiguration().withMaxNumberLength(JSONParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH));
|
||||
|
||||
// JSONArray with number just over config max length limit
|
||||
checkJSONArrayMaxLen((sb.toString() + sb + "9"), true,
|
||||
new JSONParserConfiguration().withMaxNumberLength(JSONParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to check JSONObject for a long number
|
||||
* @param s string containing the number digits to check
|
||||
* @param isValid true if number is valid, otherwise false
|
||||
* @param jsonParserConfiguration the config object
|
||||
*/
|
||||
private static void checkJSONObjectMaxLen(String s, boolean isValid, JSONParserConfiguration jsonParserConfiguration) {
|
||||
String str = "{ \"a\": " + s + "}";
|
||||
JSONObject jsonObject;
|
||||
if (jsonParserConfiguration == null) {
|
||||
jsonObject = new JSONObject(str);
|
||||
} else {
|
||||
jsonObject = new JSONObject(str, jsonParserConfiguration);
|
||||
}
|
||||
Object obj = jsonObject.get("a");
|
||||
if (isValid) {
|
||||
assertTrue(obj instanceof Number);
|
||||
} else {
|
||||
assertTrue(obj instanceof String);
|
||||
}
|
||||
// may not work for scientific notation and BigDecimal
|
||||
// assertEquals(s.length(), obj.toString().length());
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to check JSONArray for a long number
|
||||
* @param s string containing the number digits to check
|
||||
* @param isValid true if number is valid, otherwise false
|
||||
*/
|
||||
private static void checkJSONArrayMaxLen(String s, boolean isValid, JSONParserConfiguration jsonParserConfiguration) {
|
||||
String str = "[" + s + "]";
|
||||
JSONArray jsonArray;
|
||||
if (jsonParserConfiguration == null) {
|
||||
jsonArray = new JSONArray(str);
|
||||
} else {
|
||||
jsonArray = new JSONArray(str, jsonParserConfiguration);
|
||||
}
|
||||
Object obj = jsonArray.get(0);
|
||||
if (isValid) {
|
||||
assertTrue(obj instanceof Number);
|
||||
} else {
|
||||
assertTrue(obj instanceof String);
|
||||
}
|
||||
// may not work for scientific notation and BigDecimal
|
||||
// assertEquals(s.length(), obj.toString().length());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3843,16 +3893,12 @@ public class JSONObjectTest {
|
||||
assertEquals(1000, sb.length());
|
||||
|
||||
// at max length: parsed as number
|
||||
JSONObject jo = new JSONObject("{ \"a\": " + sb + "}");
|
||||
Object val = jo.get("a");
|
||||
assertTrue("Expected Number but got " + val.getClass(), val instanceof Number);
|
||||
checkJSONObjectMaxLen(sb.toString(), true, null);
|
||||
|
||||
// over max length: returned as string
|
||||
sb.append("1");
|
||||
assertEquals(1001, sb.length());
|
||||
JSONObject jo2 = new JSONObject("{ \"a\": " + sb + "}");
|
||||
Object val2 = jo2.get("a");
|
||||
assertTrue("Expected String but got " + val2.getClass(), val2 instanceof String);
|
||||
checkJSONObjectMaxLen(sb.toString(), false, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3873,16 +3919,12 @@ public class JSONObjectTest {
|
||||
assertEquals(1000, sb.length());
|
||||
|
||||
// at max length: parsed as number (BigDecimal)
|
||||
JSONObject jo = new JSONObject("{ \"a\": " + sb + "}");
|
||||
Object val = jo.get("a");
|
||||
assertTrue("Expected Number but got " + val.getClass(), val instanceof Number);
|
||||
checkJSONObjectMaxLen(sb.toString(), true, null);
|
||||
|
||||
// over max length: returned as string
|
||||
sb.append("3");
|
||||
assertEquals(1001, sb.length());
|
||||
JSONObject jo2 = new JSONObject("{ \"a\": " + sb + "}");
|
||||
Object val2 = jo2.get("a");
|
||||
assertTrue("Expected String but got " + val2.getClass(), val2 instanceof String);
|
||||
checkJSONObjectMaxLen(sb.toString(), false, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -3899,9 +3941,7 @@ public class JSONObjectTest {
|
||||
assertEquals(1000, sb.length());
|
||||
|
||||
// at max length: parsed as number
|
||||
JSONObject jo = new JSONObject("{ \"a\": " + sb + "}");
|
||||
Object val = jo.get("a");
|
||||
assertTrue("Expected Number but got " + val.getClass(), val instanceof Number);
|
||||
checkJSONObjectMaxLen(sb.toString(), true, null);
|
||||
|
||||
// over max length: returned as string
|
||||
sb = new StringBuilder("1.");
|
||||
@@ -3910,9 +3950,7 @@ public class JSONObjectTest {
|
||||
}
|
||||
sb.append("e100");
|
||||
assertEquals(1001, sb.length());
|
||||
JSONObject jo2 = new JSONObject("{ \"a\": " + sb + "}");
|
||||
Object val2 = jo2.get("a");
|
||||
assertTrue("Expected String but got " + val2.getClass(), val2 instanceof String);
|
||||
checkJSONObjectMaxLen(sb.toString(), false, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user