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 Details

    • 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.
  • Constructor Details

    • JSONParserConfiguration

      public JSONParserConfiguration()
      Configuration with the default values.
  • Method Details

    • clone

      protected JSONParserConfiguration clone()
      Description copied from class: ParserConfiguration
      Provides a new instance of the same configuration.
      Overrides:
      clone in class ParserConfiguration
    • 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 a Collection 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 class ParserConfiguration
      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 value

      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.

      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 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.