Package org.json
Class JSONParserConfiguration
java.lang.Object
org.json.ParserConfiguration
org.json.JSONParserConfiguration
Configuration object for the JSON parser. The configuration is immutable.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate boolean
Used to indicate whether to overwrite duplicate key or not.private boolean
This flag, when set to true, instructs the parser to enforce strict mode when parsing JSON text.private boolean
Used 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_MAXIMUM_NESTING_DEPTH, keepStrings, maxNestingDepth, UNDEFINED_MAXIMUM_NESTING_DEPTH
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected JSONParserConfiguration
clone()
Provides a new instance of the same configuration.boolean
The parser's behavior when meeting duplicate keys, controls whether the parser should overwrite duplicate keys or not.boolean
The parser throws an Exception when strict mode is true and tries to parse invalid JSON characters.boolean
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
).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 aCollection
instance into JSONArray.withOverwriteDuplicateKey
(boolean overwriteDuplicateKey) Controls the parser's behavior when meeting duplicate keys.Sets the strict mode configuration for the JSON parser with default true valuewithStrictMode
(boolean mode) Sets the strict mode configuration for the JSON parser.withUseNativeNulls
(boolean useNativeNulls) Controls the parser's behavior when meeting Java null values while converting maps.Methods inherited from class org.json.ParserConfiguration
getMaxNestingDepth, isKeepStrings, withKeepStrings
-
Field Details
-
overwriteDuplicateKey
private boolean overwriteDuplicateKeyUsed to indicate whether to overwrite duplicate key or not. -
useNativeNulls
private boolean useNativeNullsUsed to indicate whether to convert java null values to JSONObject.NULL or ignoring the entry when converting java maps. -
strictMode
private boolean strictModeThis 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.
-
-
Constructor Details
-
JSONParserConfiguration
public JSONParserConfiguration()Configuration with the default values.
-
-
Method Details
-
clone
Description copied from class:ParserConfiguration
Provides a new instance of the same configuration.- Overrides:
clone
in classParserConfiguration
-
withMaxNestingDepth
Defines the maximum nesting depth that the parser will descend before throwing an exception when parsing a map into JSONObject or parsing aCollection
instance into JSONArray. The default max nesting depth is 512, which means the parser will throw a JsonException if the maximum depth is reached.- Overrides:
withMaxNestingDepth
in 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
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
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
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
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
overwriteDuplicateKey
configuration 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
useNativeNulls
configuration 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.
-