Compare commits
21
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34efda6423 | ||
|
|
22ab2fb724 | ||
|
|
392a352901 | ||
|
|
b594a3229c | ||
|
|
94854a1dfa | ||
|
|
1795e8cf26 | ||
|
|
da757c6656 | ||
|
|
d24bc9e5d6 | ||
|
|
80efb529d7 | ||
|
|
9953b76050 | ||
|
|
a28328c706 | ||
|
|
703ac34a55 | ||
|
|
e40d933d50 | ||
|
|
fce83c91bb | ||
|
|
90563eb877 | ||
|
|
1efb5f6c43 | ||
|
|
ab92bb9088 | ||
|
|
c91d821dbe | ||
|
|
4a23fd8d9b | ||
|
|
8353b59fa0 | ||
|
|
26ee6eb25a |
@@ -11,7 +11,7 @@ JSON in Java [package org.json]
|
||||
[](https://github.com/stleary/JSON-java/actions/workflows/codeql-analysis.yml)
|
||||
[](https://javadoc.io/doc/org.json/json)
|
||||
|
||||
**[Click here if you just want the latest release jar file.](https://search.maven.org/remotecontent?filepath=org/json/json/20260522/json-20260522.jar)**
|
||||
**[Click here if you just want the latest release jar file.](https://repo1.maven.org/maven2/org/json/json/20260814/json-20260814.jar)**
|
||||
|
||||
|
||||
# Overview
|
||||
|
||||
+9
-1
@@ -21,6 +21,7 @@ repositories {
|
||||
}
|
||||
|
||||
// To view the report open build/reports/jacoco/test/html/index.html
|
||||
// Note: disable jacocoTestReport and test blocks, and the jacoco plugin if it interferes with stepping in the debugger
|
||||
jacocoTestReport {
|
||||
reports {
|
||||
html.required = true
|
||||
@@ -31,6 +32,13 @@ test {
|
||||
finalizedBy jacocoTestReport
|
||||
}
|
||||
|
||||
// enable logging to stdout during test
|
||||
tasks.withType(Test) {
|
||||
testLogging {
|
||||
showStandardStreams = true
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
testImplementation 'com.jayway.jsonpath:json-path:2.9.0'
|
||||
@@ -42,7 +50,7 @@ subprojects {
|
||||
}
|
||||
|
||||
group = 'org.json'
|
||||
version = 'v20260522-SNAPSHOT'
|
||||
version = 'v20260814-SNAPSHOT'
|
||||
description = 'JSON in Java'
|
||||
sourceCompatibility = '1.8'
|
||||
|
||||
|
||||
@@ -5,6 +5,10 @@ and artifactId "json". For example:
|
||||
[https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav](https://search.maven.org/search?q=g:org.json%20AND%20a:json&core=gav)
|
||||
|
||||
~~~
|
||||
20260814 Fixes XML.unescape whitespace handling
|
||||
|
||||
20260719 Fixes CVE-2026-59171 very large BigInteger, BigDecimal
|
||||
|
||||
20260522 Publish key data, recent commits for minor fixes
|
||||
|
||||
20251224 Records, fromJson(), and recent commits
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20260522</version>
|
||||
<version>20260814</version>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<name>JSON in Java</name>
|
||||
@@ -46,20 +46,6 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>ossrh</id>
|
||||
<name>Central Repository OSSRH</name>
|
||||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
||||
</repository>
|
||||
<snapshotRepository>
|
||||
<id>ossrh</id>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
|
||||
</snapshotRepository>
|
||||
</distributionManagement>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
@@ -158,17 +144,7 @@
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.sonatype.plugins</groupId>
|
||||
<artifactId>nexus-staging-maven-plugin</artifactId>
|
||||
<version>1.6.3</version>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<serverId>ossrh</serverId>
|
||||
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
|
||||
<autoReleaseAfterClose>false</autoReleaseAfterClose>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.moditect</groupId>
|
||||
<artifactId>moditect-maven-plugin</artifactId>
|
||||
|
||||
@@ -183,29 +183,71 @@ public class CDL {
|
||||
sb.append(delimiter);
|
||||
}
|
||||
Object object = ja.opt(i);
|
||||
if (object != null) {
|
||||
String string = object.toString();
|
||||
if (!string.isEmpty() && (string.indexOf(delimiter) >= 0 ||
|
||||
string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||
|
||||
string.indexOf(0) >= 0 || string.charAt(0) == '"')) {
|
||||
sb.append('"');
|
||||
int length = string.length();
|
||||
for (int j = 0; j < length; j += 1) {
|
||||
char c = string.charAt(j);
|
||||
if (c >= ' ' && c != '"') {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
sb.append('"');
|
||||
} else {
|
||||
sb.append(string);
|
||||
}
|
||||
}
|
||||
appendRowValue(sb, object, delimiter);
|
||||
}
|
||||
sb.append('\n');
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a single row value, quoting it when required by the delimiter or
|
||||
* content.
|
||||
*
|
||||
* @param sb the destination buffer
|
||||
* @param object the value to append
|
||||
* @param delimiter the delimiter used between row values
|
||||
*/
|
||||
private static void appendRowValue(StringBuilder sb, Object object, char delimiter) {
|
||||
if (object == null) {
|
||||
return;
|
||||
}
|
||||
String string = object.toString();
|
||||
if (shouldQuoteValue(string, delimiter)) {
|
||||
appendQuotedValue(sb, string);
|
||||
} else {
|
||||
sb.append(string);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether a row value should be quoted.
|
||||
*
|
||||
* @param value the row value to evaluate
|
||||
* @param delimiter the delimiter used between row values
|
||||
* @return {@code true} if the value should be quoted
|
||||
*/
|
||||
private static boolean shouldQuoteValue(String value, char delimiter) {
|
||||
if (value.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
boolean containsDelimiter = value.indexOf(delimiter) >= 0;
|
||||
boolean containsNewline = value.indexOf('\n') >= 0;
|
||||
boolean containsCarriageReturn = value.indexOf('\r') >= 0;
|
||||
boolean containsNullCharacter = value.indexOf(0) >= 0;
|
||||
boolean startsWithQuote = value.charAt(0) == '"';
|
||||
return containsDelimiter || containsNewline || containsCarriageReturn ||
|
||||
containsNullCharacter || startsWithQuote;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a row value surrounded by quotes, omitting characters that should
|
||||
* not appear inside the quoted value.
|
||||
*
|
||||
* @param sb the destination buffer
|
||||
* @param value the value to append
|
||||
*/
|
||||
private static void appendQuotedValue(StringBuilder sb, String value) {
|
||||
sb.append('"');
|
||||
int length = value.length();
|
||||
for (int j = 0; j < length; j += 1) {
|
||||
char c = value.charAt(j);
|
||||
if (c >= ' ' && c != '"') {
|
||||
sb.append(c);
|
||||
}
|
||||
}
|
||||
sb.append('"');
|
||||
}
|
||||
|
||||
/**
|
||||
* Produce a JSONArray of JSONObjects from a comma delimited text string,
|
||||
* using the first row as a source of names.
|
||||
|
||||
@@ -483,8 +483,29 @@ public class JSONArray implements Iterable<Object> {
|
||||
* to a BigInteger.
|
||||
*/
|
||||
public BigInteger getBigInteger (int index) throws JSONException {
|
||||
return this.getBigInteger(index, new JSONParserConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the BigInteger value associated with an index.
|
||||
*
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @param jsonParserConfiguration
|
||||
* A configuration whose {@code maxNumberLength} bounds the number of
|
||||
* decimal digits in the returned integer. Values exceeding this length
|
||||
* are treated as unconvertible. Pass a configuration with
|
||||
* {@link ParserConfiguration#UNDEFINED_MAXIMUM_NUMBER_LENGTH} to disable
|
||||
* this check.
|
||||
* @return The value.
|
||||
* @throws JSONException
|
||||
* If the key is not found or if the value cannot be converted
|
||||
* to a BigInteger.
|
||||
*/
|
||||
public BigInteger getBigInteger (int index, JSONParserConfiguration jsonParserConfiguration)
|
||||
throws JSONException {
|
||||
Object object = this.get(index);
|
||||
BigInteger val = JSONObject.objectToBigInteger(object, null);
|
||||
BigInteger val = JSONObject.objectToBigInteger(object, null, jsonParserConfiguration);
|
||||
if(val == null) {
|
||||
throw wrongValueFormatException(index, "BigInteger", object, null);
|
||||
}
|
||||
@@ -960,8 +981,8 @@ public class JSONArray implements Iterable<Object> {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional BigInteger value associated with an index. The
|
||||
* defaultValue is returned if there is no value for the index, or if the
|
||||
* Get the optional BigInteger value associated with an index. The
|
||||
* defaultValue is returned if there is no value for the index, or if the
|
||||
* value is not a number and cannot be converted to a number.
|
||||
*
|
||||
* @param index
|
||||
@@ -971,8 +992,31 @@ public class JSONArray implements Iterable<Object> {
|
||||
* @return The value.
|
||||
*/
|
||||
public BigInteger optBigInteger(int index, BigInteger defaultValue) {
|
||||
return this.optBigInteger(index, defaultValue, new JSONParserConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the optional BigInteger value associated with an index. The
|
||||
* defaultValue is returned if there is no value for the index, or if the
|
||||
* value is not a number and cannot be converted to a number.
|
||||
*
|
||||
* @param index
|
||||
* The index must be between 0 and length() - 1.
|
||||
* @param defaultValue
|
||||
* The default value.
|
||||
* @param jsonParserConfiguration
|
||||
* A configuration whose {@code maxNumberLength} bounds the number of
|
||||
* decimal digits in the returned integer. Values exceeding this length
|
||||
* are treated as unconvertible and {@code defaultValue} is returned.
|
||||
* Pass a configuration with
|
||||
* {@link ParserConfiguration#UNDEFINED_MAXIMUM_NUMBER_LENGTH} to disable
|
||||
* this check.
|
||||
* @return The value.
|
||||
*/
|
||||
public BigInteger optBigInteger(int index, BigInteger defaultValue,
|
||||
JSONParserConfiguration jsonParserConfiguration) {
|
||||
Object val = this.opt(index);
|
||||
return JSONObject.objectToBigInteger(val, defaultValue);
|
||||
return JSONObject.objectToBigInteger(val, defaultValue, jsonParserConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -760,8 +760,29 @@ public class JSONObject {
|
||||
* be converted to BigInteger.
|
||||
*/
|
||||
public BigInteger getBigInteger(String key) throws JSONException {
|
||||
return this.getBigInteger(key, new JSONParserConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the BigInteger value associated with a key.
|
||||
*
|
||||
* @param key
|
||||
* A key string.
|
||||
* @param jsonParserConfiguration
|
||||
* A configuration whose {@code maxNumberLength} bounds the number of
|
||||
* decimal digits in the returned integer. Values exceeding this length
|
||||
* are treated as unconvertible. Pass a configuration with
|
||||
* {@link ParserConfiguration#UNDEFINED_MAXIMUM_NUMBER_LENGTH} to disable
|
||||
* this check.
|
||||
* @return The numeric value.
|
||||
* @throws JSONException
|
||||
* if the key is not found or if the value cannot
|
||||
* be converted to BigInteger.
|
||||
*/
|
||||
public BigInteger getBigInteger(String key, JSONParserConfiguration jsonParserConfiguration)
|
||||
throws JSONException {
|
||||
Object object = this.get(key);
|
||||
BigInteger ret = objectToBigInteger(object, null);
|
||||
BigInteger ret = objectToBigInteger(object, null, jsonParserConfiguration);
|
||||
if (ret != null) {
|
||||
return ret;
|
||||
}
|
||||
@@ -1381,8 +1402,31 @@ public class JSONObject {
|
||||
* @return An object which is the value.
|
||||
*/
|
||||
public BigInteger optBigInteger(String key, BigInteger defaultValue) {
|
||||
return this.optBigInteger(key, defaultValue, new JSONParserConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an optional BigInteger associated with a key, or the defaultValue if
|
||||
* there is no such key or if its value is not a number. If the value is a
|
||||
* string, an attempt will be made to evaluate it as a number.
|
||||
*
|
||||
* @param key
|
||||
* A key string.
|
||||
* @param defaultValue
|
||||
* The default.
|
||||
* @param jsonParserConfiguration
|
||||
* A configuration whose {@code maxNumberLength} bounds the number of
|
||||
* decimal digits in the returned integer. Values exceeding this length
|
||||
* are treated as unconvertible and {@code defaultValue} is returned.
|
||||
* Pass a configuration with
|
||||
* {@link ParserConfiguration#UNDEFINED_MAXIMUM_NUMBER_LENGTH} to disable
|
||||
* this check.
|
||||
* @return An object which is the value.
|
||||
*/
|
||||
public BigInteger optBigInteger(String key, BigInteger defaultValue,
|
||||
JSONParserConfiguration jsonParserConfiguration) {
|
||||
Object val = this.opt(key);
|
||||
return objectToBigInteger(val, defaultValue);
|
||||
return objectToBigInteger(val, defaultValue, jsonParserConfiguration);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1392,14 +1436,43 @@ public class JSONObject {
|
||||
* to convert.
|
||||
*/
|
||||
static BigInteger objectToBigInteger(Object val, BigInteger defaultValue) {
|
||||
return objectToBigInteger(val, defaultValue, new JSONParserConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param val value to convert
|
||||
* @param defaultValue default value to return is the conversion doesn't work or is null.
|
||||
* @param jsonParserConfiguration parser configuration whose {@code maxNumberLength}
|
||||
* bounds the number of decimal digits in the resulting integer. Values whose
|
||||
* integer part would exceed this length are treated as unconvertible and
|
||||
* {@code defaultValue} is returned. Pass a configuration with
|
||||
* {@link ParserConfiguration#UNDEFINED_MAXIMUM_NUMBER_LENGTH} to disable this check.
|
||||
* @return BigInteger conversion of the original value, or the defaultValue if unable
|
||||
* to convert.
|
||||
*/
|
||||
static BigInteger objectToBigInteger(Object val, BigInteger defaultValue,
|
||||
JSONParserConfiguration jsonParserConfiguration) {
|
||||
if (NULL.equals(val)) {
|
||||
return defaultValue;
|
||||
}
|
||||
if (jsonParserConfiguration == null) {
|
||||
jsonParserConfiguration = new JSONParserConfiguration();
|
||||
}
|
||||
final int maxNumberLength = jsonParserConfiguration.getMaxNumberLength();
|
||||
if (val instanceof BigInteger){
|
||||
return (BigInteger) val;
|
||||
}
|
||||
if (val instanceof BigDecimal){
|
||||
return ((BigDecimal) val).toBigInteger();
|
||||
BigDecimal bd = (BigDecimal) val;
|
||||
// Same ceiling as the parse-time maxNumberLength guard: refuse to
|
||||
// materialise an integer whose decimal representation would exceed
|
||||
// maxNumberLength digits. Prevents DoS via short exponent literals
|
||||
// like 1e100000000 (CVE-2026-59171, see issue #1063).
|
||||
if (maxNumberLength != ParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH
|
||||
&& (long) bd.precision() - bd.scale() > maxNumberLength) {
|
||||
return defaultValue;
|
||||
}
|
||||
return bd.toBigInteger();
|
||||
}
|
||||
if (val instanceof Double || val instanceof Float){
|
||||
if (!numberIsFinite((Number)val)) {
|
||||
@@ -1411,6 +1484,18 @@ public class JSONObject {
|
||||
|| val instanceof Short || val instanceof Byte){
|
||||
return BigInteger.valueOf(((Number) val).longValue());
|
||||
}
|
||||
return attemptConversionToBigInteger(val, defaultValue, maxNumberLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method to attempt conversion of value to BigInteger.
|
||||
* Added to reduce complexity of objectToBigInteger()
|
||||
* @param val the value to be converted
|
||||
* @param defaultValue the default value to use if conversion is not attempted or fails
|
||||
* @param maxNumberLength the max length allowed for BigIntegers
|
||||
* @return the converted value, or the defaultValue
|
||||
*/
|
||||
private static BigInteger attemptConversionToBigInteger(Object val, BigInteger defaultValue, int maxNumberLength) {
|
||||
// don't check if it's a string in case of unchecked Number subclasses
|
||||
try {
|
||||
/**
|
||||
@@ -1422,7 +1507,12 @@ public class JSONObject {
|
||||
*/
|
||||
final String valStr = val.toString();
|
||||
if(isDecimalNotation(valStr)) {
|
||||
return new BigDecimal(valStr).toBigInteger();
|
||||
BigDecimal bd = new BigDecimal(valStr);
|
||||
if (maxNumberLength != ParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH
|
||||
&& (long) bd.precision() - bd.scale() > maxNumberLength) {
|
||||
return defaultValue;
|
||||
}
|
||||
return bd.toBigInteger();
|
||||
}
|
||||
return new BigInteger(valStr);
|
||||
} catch (Exception e) {
|
||||
@@ -2667,16 +2757,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 +2807,16 @@ public class JSONObject {
|
||||
char initial = string.charAt(0);
|
||||
if ((initial >= '0' && initial <= '9') || initial == '-') {
|
||||
try {
|
||||
return stringToNumber(string);
|
||||
if (jsonParserConfiguration == null) {
|
||||
jsonParserConfiguration = new JSONParserConfiguration();
|
||||
}
|
||||
// user declines max number checking
|
||||
if (jsonParserConfiguration.getMaxNumberLength() == ParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH) {
|
||||
return stringToNumber(string);
|
||||
}
|
||||
if (string.length() <= jsonParserConfiguration.getMaxNumberLength()) {
|
||||
return stringToNumber(string);
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@@ -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,32 @@ 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 Use the with*() methods instead
|
||||
*/
|
||||
@Deprecated
|
||||
protected ParserConfiguration(final boolean keepStrings, final int maxNestingDepth) {
|
||||
this.keepStrings = keepStrings;
|
||||
this.maxNestingDepth = maxNestingDepth;
|
||||
this.maxNumberLength = DEFAULT_MAX_NUMBER_LENGTH;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,10 +77,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 +143,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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -172,8 +172,14 @@ public class XML {
|
||||
&& cp != 0xA
|
||||
&& cp != 0xD
|
||||
) || !(
|
||||
// valid the range of acceptable characters that aren't control
|
||||
(cp >= 0x20 && cp <= 0xD7FF)
|
||||
// Valid character range per W3C XML 1.0 spec:
|
||||
// #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
|
||||
// Previously omitted #x9/#xA/#xD, causing unescape(" ") etc.
|
||||
// to reject valid LF/TAB/CR as illegal (see #1059)
|
||||
cp == 0x9
|
||||
|| cp == 0xA
|
||||
|| cp == 0xD
|
||||
|| (cp >= 0x20 && cp <= 0xD7FF)
|
||||
|| (cp >= 0xE000 && cp <= 0xFFFD)
|
||||
|| (cp >= 0x10000 && cp <= 0x10FFFF)
|
||||
)
|
||||
@@ -619,14 +625,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 +672,13 @@ public class XML {
|
||||
char initial = string.charAt(0);
|
||||
if ((initial >= '0' && initial <= '9') || initial == '-') {
|
||||
try {
|
||||
return stringToNumber(string);
|
||||
// user declines max number checking
|
||||
if (xmlParserConfiguration.getMaxNumberLength() == ParserConfiguration.UNDEFINED_MAXIMUM_NUMBER_LENGTH) {
|
||||
return stringToNumber(string);
|
||||
}
|
||||
if(string.length() <= xmlParserConfiguration.getMaxNumberLength()) {
|
||||
return stringToNumber(string);
|
||||
}
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1537,6 +1537,43 @@ public class XMLTest {
|
||||
assertEquals("A", jsonObject.getString("a"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that valid XML numeric character references for whitespace
|
||||
* control characters (TAB, LF, CR) are correctly unescaped. These
|
||||
* codepoints are explicitly allowed by the XML 1.0 spec
|
||||
* (https://www.w3.org/TR/REC-xml/#charsets) but were previously rejected
|
||||
* as invalid. See issue #1059.
|
||||
*/
|
||||
@Test
|
||||
public void testValidWhitespaceNumericEntityUnescape() {
|
||||
// decimal references for the three allowed control characters
|
||||
assertEquals("\t", XML.unescape("	"));
|
||||
assertEquals("\n", XML.unescape(" "));
|
||||
assertEquals("\r", XML.unescape(" "));
|
||||
// hex references for the same codepoints
|
||||
assertEquals("\t", XML.unescape("	"));
|
||||
assertEquals("\n", XML.unescape("
"));
|
||||
assertEquals("\r", XML.unescape("
"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that {@code XML.toJSONObject} accepts numeric character references
|
||||
* for the XML-allowed control characters (TAB, LF, CR) without throwing.
|
||||
* Regression test for #1059, where {@code XML.toJSONObject("<a> </a>")}
|
||||
* threw JSONException in versions after 20251224.
|
||||
*/
|
||||
@Test
|
||||
public void testValidWhitespaceNumericEntityToJSONObject() {
|
||||
// LF reference should round-trip through toJSONObject without throwing
|
||||
JSONObject jsonObject = XML.toJSONObject("<a> </a>");
|
||||
// the value is the LF character (possibly trimmed by the JSON path,
|
||||
// but the call must not throw)
|
||||
assertTrue(jsonObject.has("a"));
|
||||
// TAB and CR references also accepted
|
||||
XML.toJSONObject("<a>	</a>");
|
||||
XML.toJSONObject("<a> </a>");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user