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

    • DEFAULT_MAXIMUM_SHARED_KEYS

      public static final int DEFAULT_MAXIMUM_SHARED_KEYS
      The default maximum number of distinct object keys shared within one parse.
      See Also:
    • UNLIMITED_SHARED_KEYS

      public static final int UNLIMITED_SHARED_KEYS
      Used to indicate that there is no limit on the number of distinct object keys shared within one parse.
      See Also:
    • 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.
    • maxSharedKeys

      private int maxSharedKeys
      The maximum number of distinct object keys shared within one parse.
    • 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.
    • withMaxSharedKeys

      public JSONParserConfiguration withMaxSharedKeys(int maxSharedKeys)
      Defines how many distinct object keys the parser shares within one parse. When a key repeats, for example in each element of an array of objects, the parser reuses the first String instance read for that key instead of keeping a separate copy in every object. Only the first maxSharedKeys distinct keys are shared, so input that begins with many unique keys may need a higher limit. The default is 1000. Parsed values are the same with any setting.
      Parameters:
      maxSharedKeys - the maximum number of distinct keys to share; 0 disables sharing, and UNLIMITED_SHARED_KEYS (or any negative value) removes the limit
      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.
    • getMaxSharedKeys

      public int getMaxSharedKeys()
      The maximum number of distinct object keys shared within one parse. 0 means keys are not shared, and UNLIMITED_SHARED_KEYS means there is no limit.
      Returns:
      The maxSharedKeys 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.