mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-02 11:05:28 -04:00
fix: amend XMLParserConfiguration.clone() to include the new maxNestingDepth param.
Amend Javadoc for XML and XMLParserConfiguration classes.
This commit is contained in:
parent
401495ae86
commit
2391d248cc
@ -98,7 +98,7 @@ public class XML {
|
|||||||
/**
|
/**
|
||||||
* Replace special characters with XML escapes:
|
* Replace special characters with XML escapes:
|
||||||
*
|
*
|
||||||
* <pre>{@code
|
* <pre>{@code
|
||||||
* & (ampersand) is replaced by &amp;
|
* & (ampersand) is replaced by &amp;
|
||||||
* < (less than) is replaced by &lt;
|
* < (less than) is replaced by &lt;
|
||||||
* > (greater than) is replaced by &gt;
|
* > (greater than) is replaced by &gt;
|
||||||
@ -229,8 +229,12 @@ public class XML {
|
|||||||
* The JSONObject that will include the new material.
|
* The JSONObject that will include the new material.
|
||||||
* @param name
|
* @param name
|
||||||
* The tag name.
|
* The tag name.
|
||||||
|
* @param config
|
||||||
|
* The XML parser configuration.
|
||||||
|
* @param currentNestingDepth
|
||||||
|
* The current nesting depth.
|
||||||
* @return true if the close tag is processed.
|
* @return true if the close tag is processed.
|
||||||
* @throws JSONException
|
* @throws JSONException Thrown if any parsing error occurs.
|
||||||
*/
|
*/
|
||||||
private static boolean parse(XMLTokener x, JSONObject context, String name, XMLParserConfiguration config, int currentNestingDepth)
|
private static boolean parse(XMLTokener x, JSONObject context, String name, XMLParserConfiguration config, int currentNestingDepth)
|
||||||
throws JSONException {
|
throws JSONException {
|
||||||
@ -427,7 +431,7 @@ public class XML {
|
|||||||
context.accumulate(tagName, jsonObject);
|
context.accumulate(tagName, jsonObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -491,7 +495,7 @@ public class XML {
|
|||||||
}
|
}
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* direct copy of {@link JSONObject#stringToNumber(String)} to maintain Android support.
|
* direct copy of {@link JSONObject#stringToNumber(String)} to maintain Android support.
|
||||||
*/
|
*/
|
||||||
@ -538,7 +542,7 @@ public class XML {
|
|||||||
// integer representation.
|
// integer representation.
|
||||||
// This will narrow any values to the smallest reasonable Object representation
|
// This will narrow any values to the smallest reasonable Object representation
|
||||||
// (Integer, Long, or BigInteger)
|
// (Integer, Long, or BigInteger)
|
||||||
|
|
||||||
// BigInteger down conversion: We use a similar bitLength compare as
|
// BigInteger down conversion: We use a similar bitLength compare as
|
||||||
// BigInteger#intValueExact uses. Increases GC, but objects hold
|
// BigInteger#intValueExact uses. Increases GC, but objects hold
|
||||||
// only what they need. i.e. Less runtime overhead if the value is
|
// only what they need. i.e. Less runtime overhead if the value is
|
||||||
@ -554,7 +558,7 @@ public class XML {
|
|||||||
}
|
}
|
||||||
throw new NumberFormatException("val ["+val+"] is not a valid number.");
|
throw new NumberFormatException("val ["+val+"] is not a valid number.");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* direct copy of {@link JSONObject#isDecimalNotation(String)} to maintain Android support.
|
* direct copy of {@link JSONObject#isDecimalNotation(String)} to maintain Android support.
|
||||||
*/
|
*/
|
||||||
@ -572,7 +576,7 @@ public class XML {
|
|||||||
* name/value pairs and arrays of values. JSON does not does not like to
|
* name/value pairs and arrays of values. JSON does not does not like to
|
||||||
* distinguish between elements and attributes. Sequences of similar
|
* distinguish between elements and attributes. Sequences of similar
|
||||||
* elements are represented as JSONArrays. Content text may be placed in a
|
* elements are represented as JSONArrays. Content text may be placed in a
|
||||||
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
||||||
* <[ [ ]]>}</pre>
|
* <[ [ ]]>}</pre>
|
||||||
* are ignored.
|
* are ignored.
|
||||||
*
|
*
|
||||||
@ -593,7 +597,7 @@ public class XML {
|
|||||||
* name/value pairs and arrays of values. JSON does not does not like to
|
* name/value pairs and arrays of values. JSON does not does not like to
|
||||||
* distinguish between elements and attributes. Sequences of similar
|
* distinguish between elements and attributes. Sequences of similar
|
||||||
* elements are represented as JSONArrays. Content text may be placed in a
|
* elements are represented as JSONArrays. Content text may be placed in a
|
||||||
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
||||||
* <[ [ ]]>}</pre>
|
* <[ [ ]]>}</pre>
|
||||||
* are ignored.
|
* are ignored.
|
||||||
*
|
*
|
||||||
@ -673,7 +677,7 @@ public class XML {
|
|||||||
* name/value pairs and arrays of values. JSON does not does not like to
|
* name/value pairs and arrays of values. JSON does not does not like to
|
||||||
* distinguish between elements and attributes. Sequences of similar
|
* distinguish between elements and attributes. Sequences of similar
|
||||||
* elements are represented as JSONArrays. Content text may be placed in a
|
* elements are represented as JSONArrays. Content text may be placed in a
|
||||||
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
||||||
* <[ [ ]]>}</pre>
|
* <[ [ ]]>}</pre>
|
||||||
* are ignored.
|
* are ignored.
|
||||||
*
|
*
|
||||||
@ -699,7 +703,7 @@ public class XML {
|
|||||||
* name/value pairs and arrays of values. JSON does not does not like to
|
* name/value pairs and arrays of values. JSON does not does not like to
|
||||||
* distinguish between elements and attributes. Sequences of similar
|
* distinguish between elements and attributes. Sequences of similar
|
||||||
* elements are represented as JSONArrays. Content text may be placed in a
|
* elements are represented as JSONArrays. Content text may be placed in a
|
||||||
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
* "content" member. Comments, prologs, DTDs, and <pre>{@code
|
||||||
* <[ [ ]]>}</pre>
|
* <[ [ ]]>}</pre>
|
||||||
* are ignored.
|
* are ignored.
|
||||||
*
|
*
|
||||||
|
@ -39,14 +39,14 @@ public class XMLParserConfiguration {
|
|||||||
* they should try to be guessed into JSON values (numeric, boolean, string)
|
* they should try to be guessed into JSON values (numeric, boolean, string)
|
||||||
*/
|
*/
|
||||||
private boolean keepStrings;
|
private boolean keepStrings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The name of the key in a JSON Object that indicates a CDATA section. Historically this has
|
* The name of the key in a JSON Object that indicates a CDATA section. Historically this has
|
||||||
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
|
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
|
||||||
* processing.
|
* processing.
|
||||||
*/
|
*/
|
||||||
private String cDataTagName;
|
private String cDataTagName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
|
* When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
|
||||||
* should be kept as attribute(<code>false</code>), or they should be converted to
|
* should be kept as attribute(<code>false</code>), or they should be converted to
|
||||||
@ -66,8 +66,7 @@ public class XMLParserConfiguration {
|
|||||||
private Set<String> forceList;
|
private Set<String> forceList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When parsing the XML into JSON, specifies the tags whose values should be converted
|
* The maximum nesting depth when parsing a XML document to JSON.
|
||||||
* to arrays
|
|
||||||
*/
|
*/
|
||||||
private int maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;
|
private int maxNestingDepth = DEFAULT_MAXIMUM_NESTING_DEPTH;
|
||||||
|
|
||||||
@ -157,15 +156,18 @@ public class XMLParserConfiguration {
|
|||||||
* <code>false</code> to parse values with attribute xsi:nil="true" as {"xsi:nil":true}.
|
* <code>false</code> to parse values with attribute xsi:nil="true" as {"xsi:nil":true}.
|
||||||
* @param xsiTypeMap <code>new HashMap<String, XMLXsiTypeConverter<?>>()</code> to parse values with attribute
|
* @param xsiTypeMap <code>new HashMap<String, XMLXsiTypeConverter<?>>()</code> to parse values with attribute
|
||||||
* xsi:type="integer" as integer, xsi:type="string" as string
|
* xsi:type="integer" as integer, xsi:type="string" as string
|
||||||
* @param forceList <code>new HashSet<String>()</code> to parse the provided tags' values as arrays
|
* @param forceList <code>new HashSet<String>()</code> to parse the provided tags' values as arrays
|
||||||
|
* @param maxNestingDepth <code>int</code> to limit the nesting depth
|
||||||
*/
|
*/
|
||||||
private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName,
|
private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName,
|
||||||
final boolean convertNilAttributeToNull, final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap, final Set<String> forceList ) {
|
final boolean convertNilAttributeToNull, final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap, final Set<String> forceList,
|
||||||
|
final int maxNestingDepth) {
|
||||||
this.keepStrings = keepStrings;
|
this.keepStrings = keepStrings;
|
||||||
this.cDataTagName = cDataTagName;
|
this.cDataTagName = cDataTagName;
|
||||||
this.convertNilAttributeToNull = convertNilAttributeToNull;
|
this.convertNilAttributeToNull = convertNilAttributeToNull;
|
||||||
this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
|
this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
|
||||||
this.forceList = Collections.unmodifiableSet(forceList);
|
this.forceList = Collections.unmodifiableSet(forceList);
|
||||||
|
this.maxNestingDepth = maxNestingDepth;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -183,14 +185,15 @@ public class XMLParserConfiguration {
|
|||||||
this.cDataTagName,
|
this.cDataTagName,
|
||||||
this.convertNilAttributeToNull,
|
this.convertNilAttributeToNull,
|
||||||
this.xsiTypeMap,
|
this.xsiTypeMap,
|
||||||
this.forceList
|
this.forceList,
|
||||||
|
this.maxNestingDepth
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
|
* When parsing the XML into JSON, 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)
|
* they should try to be guessed into JSON values (numeric, boolean, string)
|
||||||
*
|
*
|
||||||
* @return The <code>keepStrings</code> configuration value.
|
* @return The <code>keepStrings</code> configuration value.
|
||||||
*/
|
*/
|
||||||
public boolean isKeepStrings() {
|
public boolean isKeepStrings() {
|
||||||
@ -200,10 +203,10 @@ public class XMLParserConfiguration {
|
|||||||
/**
|
/**
|
||||||
* When parsing the XML into JSON, specifies if values should be kept as strings (<code>true</code>), or if
|
* When parsing the XML into JSON, 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)
|
* they should try to be guessed into JSON values (numeric, boolean, string)
|
||||||
*
|
*
|
||||||
* @param newVal
|
* @param newVal
|
||||||
* new value to use for the <code>keepStrings</code> configuration option.
|
* new value to use for the <code>keepStrings</code> configuration option.
|
||||||
*
|
*
|
||||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||||
*/
|
*/
|
||||||
public XMLParserConfiguration withKeepStrings(final boolean newVal) {
|
public XMLParserConfiguration withKeepStrings(final boolean newVal) {
|
||||||
@ -216,7 +219,7 @@ public class XMLParserConfiguration {
|
|||||||
* The name of the key in a JSON Object that indicates a CDATA section. Historically this has
|
* The name of the key in a JSON Object that indicates a CDATA section. Historically this has
|
||||||
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
|
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
|
||||||
* processing.
|
* processing.
|
||||||
*
|
*
|
||||||
* @return The <code>cDataTagName</code> configuration value.
|
* @return The <code>cDataTagName</code> configuration value.
|
||||||
*/
|
*/
|
||||||
public String getcDataTagName() {
|
public String getcDataTagName() {
|
||||||
@ -227,10 +230,10 @@ public class XMLParserConfiguration {
|
|||||||
* The name of the key in a JSON Object that indicates a CDATA section. Historically this has
|
* The name of the key in a JSON Object that indicates a CDATA section. Historically this has
|
||||||
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
|
* been the value "content" but can be changed. Use <code>null</code> to indicate no CDATA
|
||||||
* processing.
|
* processing.
|
||||||
*
|
*
|
||||||
* @param newVal
|
* @param newVal
|
||||||
* new value to use for the <code>cDataTagName</code> configuration option.
|
* new value to use for the <code>cDataTagName</code> configuration option.
|
||||||
*
|
*
|
||||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||||
*/
|
*/
|
||||||
public XMLParserConfiguration withcDataTagName(final String newVal) {
|
public XMLParserConfiguration withcDataTagName(final String newVal) {
|
||||||
@ -243,7 +246,7 @@ public class XMLParserConfiguration {
|
|||||||
* When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
|
* When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
|
||||||
* should be kept as attribute(<code>false</code>), or they should be converted to
|
* should be kept as attribute(<code>false</code>), or they should be converted to
|
||||||
* <code>null</code>(<code>true</code>)
|
* <code>null</code>(<code>true</code>)
|
||||||
*
|
*
|
||||||
* @return The <code>convertNilAttributeToNull</code> configuration value.
|
* @return The <code>convertNilAttributeToNull</code> configuration value.
|
||||||
*/
|
*/
|
||||||
public boolean isConvertNilAttributeToNull() {
|
public boolean isConvertNilAttributeToNull() {
|
||||||
@ -254,10 +257,10 @@ public class XMLParserConfiguration {
|
|||||||
* When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
|
* When parsing the XML into JSON, specifies if values with attribute xsi:nil="true"
|
||||||
* should be kept as attribute(<code>false</code>), or they should be converted to
|
* should be kept as attribute(<code>false</code>), or they should be converted to
|
||||||
* <code>null</code>(<code>true</code>)
|
* <code>null</code>(<code>true</code>)
|
||||||
*
|
*
|
||||||
* @param newVal
|
* @param newVal
|
||||||
* new value to use for the <code>convertNilAttributeToNull</code> configuration option.
|
* new value to use for the <code>convertNilAttributeToNull</code> configuration option.
|
||||||
*
|
*
|
||||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||||
*/
|
*/
|
||||||
public XMLParserConfiguration withConvertNilAttributeToNull(final boolean newVal) {
|
public XMLParserConfiguration withConvertNilAttributeToNull(final boolean newVal) {
|
||||||
@ -295,7 +298,7 @@ public class XMLParserConfiguration {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* When parsing the XML into JSON, specifies that tags that will be converted to arrays
|
* When parsing the XML into JSON, specifies that tags that will be converted to arrays
|
||||||
* in this configuration {@code Set<String>} to parse the provided tags' values as arrays
|
* in this configuration {@code Set<String>} to parse the provided tags' values as arrays
|
||||||
* @return <code>forceList</code> unmodifiable configuration set.
|
* @return <code>forceList</code> unmodifiable configuration set.
|
||||||
*/
|
*/
|
||||||
public Set<String> getForceList() {
|
public Set<String> getForceList() {
|
||||||
@ -304,8 +307,8 @@ public class XMLParserConfiguration {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* When parsing the XML into JSON, specifies that tags that will be converted to arrays
|
* When parsing the XML into JSON, specifies that tags that will be converted to arrays
|
||||||
* in this configuration {@code Set<String>} to parse the provided tags' values as arrays
|
* in this configuration {@code Set<String>} to parse the provided tags' values as arrays
|
||||||
* @param forceList {@code new HashSet<String>()} to parse the provided tags' values as arrays
|
* @param forceList {@code new HashSet<String>()} to parse the provided tags' values as arrays
|
||||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||||
*/
|
*/
|
||||||
public XMLParserConfiguration withForceList(final Set<String> forceList) {
|
public XMLParserConfiguration withForceList(final Set<String> forceList) {
|
||||||
@ -327,8 +330,9 @@ public class XMLParserConfiguration {
|
|||||||
/**
|
/**
|
||||||
* Defines the maximum nesting depth that the parser will descend before throwing an exception
|
* Defines the maximum nesting depth that the parser will descend before throwing an exception
|
||||||
* when parsing the XML into JSON. The default max nesting depth is 512, which means the parser
|
* when parsing the XML into JSON. The default max nesting depth is 512, which means the parser
|
||||||
* will go as deep as the maximum call stack size allows. Using any negative value as a
|
* will throw a JsonException if the maximum depth is reached.
|
||||||
* parameter is equivalent to setting no limit to the nesting depth.
|
* Using any negative value as a parameter is equivalent to setting no limit to the nesting depth,
|
||||||
|
* which means the parses will go as deep as the maximum call stack size allows.
|
||||||
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser
|
* @param maxNestingDepth the maximum nesting depth allowed to the XML parser
|
||||||
* @return The existing configuration will not be modified. A new configuration is returned.
|
* @return The existing configuration will not be modified. A new configuration is returned.
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user