feat(#871-strictMode): strictMode configuration add to JSONParserConfiguration

docs(#871-strictMode): add javadoc
This commit is contained in:
rikkarth 2024-03-15 00:19:25 +00:00
parent 48c092acfb
commit dcbbccc76c

View File

@ -4,11 +4,19 @@ package org.json;
* Configuration object for the JSON parser. The configuration is immutable. * Configuration object for the JSON parser. The configuration is immutable.
*/ */
public class JSONParserConfiguration extends ParserConfiguration { public class JSONParserConfiguration extends ParserConfiguration {
/** /**
* Used to indicate whether to overwrite duplicate key or not. * Used to indicate whether to overwrite duplicate key or not.
*/ */
private boolean overwriteDuplicateKey; private boolean overwriteDuplicateKey;
/**
* This flag, when set to true, instructs the parser to throw a JSONException if it encounters an invalid character
* immediately following the final ']' character in the input. This is useful for ensuring strict adherence to the
* JSON syntax, as any characters after the final closing bracket of a JSON array are considered invalid.
*/
private boolean strictMode;
/** /**
* Configuration with the default values. * Configuration with the default values.
*/ */
@ -26,10 +34,9 @@ public class JSONParserConfiguration extends ParserConfiguration {
} }
/** /**
* 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 a map
* when parsing a map into JSONObject or parsing a {@link java.util.Collection} instance into * into JSONObject or parsing a {@link java.util.Collection} instance into JSONArray. The default max nesting depth
* JSONArray. The default max nesting depth is 512, which means the parser will throw a JsonException * is 512, which means the parser will throw a JsonException if the maximum depth is reached.
* if the maximum depth is reached.
* *
* @param maxNestingDepth the maximum nesting depth allowed to the JSON parser * @param maxNestingDepth the maximum nesting depth allowed to the JSON 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.
@ -44,9 +51,8 @@ public class JSONParserConfiguration extends ParserConfiguration {
} }
/** /**
* Controls the parser's behavior when meeting duplicate keys. * Controls the parser's behavior when meeting duplicate keys. If set to false, the parser will throw a
* If set to false, the parser will throw a JSONException when meeting a duplicate key. * JSONException when meeting a duplicate key. Or the duplicate key's value will be overwritten.
* Or the duplicate key's value will be overwritten.
* *
* @param overwriteDuplicateKey defines should the parser overwrite duplicate keys. * @param overwriteDuplicateKey defines should the parser overwrite duplicate keys.
* @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.
@ -58,13 +64,45 @@ public class JSONParserConfiguration extends ParserConfiguration {
return clone; return clone;
} }
/** /**
* The parser's behavior when meeting duplicate keys, controls whether the parser should * Sets the strict mode configuration for the JSON parser.
* overwrite duplicate keys or not. * <p>
* When strict mode is enabled, the parser will throw a JSONException if it encounters an invalid character
* immediately following the final ']' character in the input. This is useful for ensuring strict adherence to the
* JSON syntax, as any characters after the final closing bracket of a JSON array are considered invalid.
*
* @param mode a boolean value indicating whether strict mode should be enabled or not
* @return a new JSONParserConfiguration instance with the updated strict mode setting
*/
public JSONParserConfiguration withStrictMode(final boolean mode) {
JSONParserConfiguration clone = this.clone();
clone.strictMode = mode;
return clone;
}
/**
* The parser's behavior when meeting duplicate keys, controls whether the parser should overwrite duplicate keys or
* not.
* *
* @return The <code>overwriteDuplicateKey</code> configuration value. * @return The <code>overwriteDuplicateKey</code> configuration value.
*/ */
public boolean isOverwriteDuplicateKey() { public boolean isOverwriteDuplicateKey() {
return this.overwriteDuplicateKey; return this.overwriteDuplicateKey;
} }
/**
* Retrieves the current strict mode setting of the JSON parser.
* <p>
* Strict mode, when enabled, instructs the parser to throw a JSONException if it encounters an invalid character
* immediately following the final ']' character in the input. This ensures strict adherence to the JSON syntax, as
* any characters after the final closing bracket of a JSON array are considered invalid.
*
* @return the current strict mode setting. True if strict mode is enabled, false otherwise.
*/
public boolean isStrictMode() {
return this.strictMode;
}
} }