Package org.json
Class JSONParserConfiguration
- java.lang.Object
-
- org.json.ParserConfiguration
-
- org.json.JSONParserConfiguration
-
public class JSONParserConfiguration extends ParserConfiguration
Configuration object for the JSON parser. The configuration is immutable.
-
-
Field Summary
Fields Modifier and Type Field Description private booleanoverwriteDuplicateKeyUsed to indicate whether to overwrite duplicate key or not.private booleanstrictModeThis flag, when set to true, instructs the parser to enforce strict mode when parsing JSON text.private booleanuseNativeNullsUsed to indicate whether to convert java null values to JSONObject.NULL or ignoring the entry when converting java maps.-
Fields inherited from class org.json.ParserConfiguration
DEFAULT_MAX_NUMBER_LENGTH, DEFAULT_MAXIMUM_NESTING_DEPTH, keepStrings, maxNestingDepth, maxNumberLength, UNDEFINED_MAXIMUM_NESTING_DEPTH, UNDEFINED_MAXIMUM_NUMBER_LENGTH
-
-
Constructor Summary
Constructors Constructor Description JSONParserConfiguration()Configuration with the default values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected JSONParserConfigurationclone()Provides a new instance of the same configuration.booleanisOverwriteDuplicateKey()The parser's behavior when meeting duplicate keys, controls whether the parser should overwrite duplicate keys or not.booleanisStrictMode()The parser throws an Exception when strict mode is true and tries to parse invalid JSON characters.booleanisUseNativeNulls()The parser's behavior when meeting a null value in a java map, controls whether the parser should write a JSON entry with a null value (isUseNativeNulls() == true) or ignore that map entry (isUseNativeNulls() == false).JSONParserConfigurationwithMaxNestingDepth(int maxNestingDepth)Defines the maximum nesting depth that the parser will descend before throwing an exception when parsing a map into JSONObject or parsing aCollectioninstance into JSONArray.JSONParserConfigurationwithOverwriteDuplicateKey(boolean overwriteDuplicateKey)Controls the parser's behavior when meeting duplicate keys.JSONParserConfigurationwithStrictMode()Sets the strict mode configuration for the JSON parser with default true valueJSONParserConfigurationwithStrictMode(boolean mode)Sets the strict mode configuration for the JSON parser.JSONParserConfigurationwithUseNativeNulls(boolean useNativeNulls)Controls the parser's behavior when meeting Java null values while converting maps.-
Methods inherited from class org.json.ParserConfiguration
getMaxNestingDepth, getMaxNumberLength, isKeepStrings, withKeepStrings, withMaxNumberLength
-
-
-
-
Field Detail
-
overwriteDuplicateKey
private boolean overwriteDuplicateKey
Used to indicate whether to overwrite duplicate key or not.
-
useNativeNulls
private boolean useNativeNulls
Used to indicate whether to convert java null values to JSONObject.NULL or ignoring the entry when converting java maps.
-
strictMode
private boolean strictMode
This flag, when set to true, instructs the parser to enforce strict mode when parsing JSON text. Garbage chars at the end of the doc, unquoted string, and single-quoted strings are all disallowed.
-
-
Method Detail
-
clone
protected JSONParserConfiguration clone()
Description copied from class:ParserConfigurationProvides a new instance of the same configuration.- Overrides:
clonein classParserConfiguration
-
withMaxNestingDepth
public JSONParserConfiguration withMaxNestingDepth(int maxNestingDepth)
Defines the maximum nesting depth that the parser will descend before throwing an exception when parsing a map into JSONObject or parsing aCollectioninstance into JSONArray. The default max nesting depth is 512, which means the parser will throw a JsonException if the maximum depth is reached.- Overrides:
withMaxNestingDepthin classParserConfiguration- Parameters:
maxNestingDepth- the maximum nesting depth allowed to the JSON parser- Returns:
- The existing configuration will not be modified. A new configuration is returned.
-
withOverwriteDuplicateKey
public JSONParserConfiguration withOverwriteDuplicateKey(boolean overwriteDuplicateKey)
Controls the parser's behavior when meeting duplicate keys. If set to false, the parser will throw a JSONException when meeting a duplicate key. Or the duplicate key's value will be overwritten.- Parameters:
overwriteDuplicateKey- defines should the parser overwrite duplicate keys.- Returns:
- The existing configuration will not be modified. A new configuration is returned.
-
withUseNativeNulls
public JSONParserConfiguration withUseNativeNulls(boolean useNativeNulls)
Controls the parser's behavior when meeting Java null values while converting maps. If set to true, the parser will put a JSONObject.NULL into the resulting JSONObject. Or the map entry will be ignored.- Parameters:
useNativeNulls- defines if the parser should convert null values in Java maps- Returns:
- The existing configuration will not be modified. A new configuration is returned.
-
withStrictMode
public JSONParserConfiguration withStrictMode()
Sets the strict mode configuration for the JSON parser with default true valueWhen 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.
- Returns:
- a new JSONParserConfiguration instance with the updated strict mode setting
-
withStrictMode
public JSONParserConfiguration withStrictMode(boolean mode)
Sets the strict mode configuration for the JSON parser.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.
- Parameters:
mode- a boolean value indicating whether strict mode should be enabled or not- Returns:
- a new JSONParserConfiguration instance with the updated strict mode setting
-
isOverwriteDuplicateKey
public boolean isOverwriteDuplicateKey()
The parser's behavior when meeting duplicate keys, controls whether the parser should overwrite duplicate keys or not.- Returns:
- The
overwriteDuplicateKeyconfiguration value.
-
isUseNativeNulls
public boolean isUseNativeNulls()
The parser's behavior when meeting a null value in a java map, controls whether the parser should write a JSON entry with a null value (isUseNativeNulls() == true) or ignore that map entry (isUseNativeNulls() == false).- Returns:
- The
useNativeNullsconfiguration value.
-
isStrictMode
public boolean isStrictMode()
The parser throws an Exception when strict mode is true and tries to parse invalid JSON characters. Otherwise, the parser is more relaxed and might tolerate some invalid characters.- Returns:
- the current strict mode setting.
-
-