Package org.apache.groovy.json.internal

# Class BaseJsonParser

java.lang.Object
org.apache.groovy.json.internal.BaseJsonParser

All Implemented Interfaces:
  `JsonParser`

Direct Known Subclasses:
  `JsonParserCharArray`, `JsonParserUsingCharacterSource`

public abstract class BaseJsonParser extends Object implements JsonParser
Base JSON parser. Scaled down version of Boon JsonParser with features removed that are JDK 1.7 dependent or Groovy duplicated functionality.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected static final int`
  `ALPHA_0`
  Character code for digit `0`.
  `protected static final int`
  `ALPHA_1`
  Character code for digit `1`.
  `protected static final int`
  `ALPHA_2`
  Character code for digit `2`.
  `protected static final int`
  `ALPHA_3`
  Character code for digit `3`.
  `protected static final int`
  `ALPHA_4`
  Character code for digit `4`.
  `protected static final int`
  `ALPHA_5`
  Character code for digit `5`.
  `protected static final int`
  `ALPHA_6`
  Character code for digit `6`.
  `protected static final int`
  `ALPHA_7`
  Character code for digit `7`.
  `protected static final int`
  `ALPHA_8`
  Character code for digit `8`.
  `protected static final int`
  `ALPHA_9`
  Character code for digit `9`.
  `protected int`
  `bufSize`
  Initial buffer size used when reading character streams.
  `protected String`
  `charset`
  Charset used for byte-based input when no override is supplied.
  `protected static final int`
  `CLOSED_BRACKET`
  Character code for the closing array delimiter `]`.
  `protected static final int`
  `CLOSED_CURLY`
  Character code for the closing object delimiter }.
  `protected static final int`
  `COLON`
  Character code for the `:` name/value separator.
  `protected static final int`
  `COMMA`
  Character code for the `,` element separator.
  `protected static final int`
  `DECIMAL_POINT`
  Character code for the decimal point in number literals.
  `static final int`
  `DEFAULT_MAX_NESTING_DEPTH`
  Default maximum nesting depth for arrays/objects.
  `static final int`
  `DEFAULT_MAX_NUMBER_LENGTH`
  Default maximum length, in characters, of a single JSON number token.
  `protected static final int`
  `DOUBLE_QUOTE`
  Character code for the JSON string delimiter `"`.
  `protected static final int`
  `ESCAPE`
  Character code for the JSON escape marker `\`.
  `protected static final ConcurrentHashMap<String,String>`
  `internedKeysCache`
  Cache used when key interning is enabled.
  `protected static final boolean`
  `internKeys`
  Whether parsed object keys should be interned.
  `protected static final int`
  `LETTER_BIG_E`
  Uppercase exponent marker used in JSON numbers.
  `protected static final int`
  `LETTER_E`
  Lowercase exponent marker used in JSON numbers.
  `protected int`
  `maxNestingDepth`
  Maximum nesting depth permitted for this parser.
  `protected int`
  `maxNumberLength`
  Maximum length permitted for a single number token.
  `protected static final int`
  `MINUS`
  Character code for the minus sign in number literals.
  `protected static final int`
  `PLUS`
  Character code for the plus sign in exponent literals.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `BaseJsonParser()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected String`
  `charDescription(int c)`
  Builds a readable description of a character code for parser errors.
  `protected final void`
  `checkNumberLength(int length)`
  Enforces `maxNumberLength` against a number token about to be converted.
  `protected final void`
  `enterNesting()`
  Records entry into a nested array or object, enforcing `maxNestingDepth`.
  `protected final void`
  `exitNesting()`
  Records exit from a nested array or object.
  `protected static int`
  `findEndQuote(char[] array, int index)`
  Finds the closing quote of a JSON string while honoring escapes.
  `int`
  `getMaxNestingDepth()`
  Returns the maximum nesting depth permitted while parsing.
  `int`
  `getMaxNumberLength()`
  Returns the maximum length permitted for a single number token.
  `protected static boolean`
  `hasEscapeChar(char[] array, int index, int[] indexHolder)`
  Scans a string body until it finds an escape or the closing quote.
  `protected static boolean`
  `isDecimalChar(int currentChar)`
  Checks whether a character can appear in a decimal JSON number.
  `protected static boolean`
  `isDelimiter(int c)`
  Checks whether a character terminates the current JSON value.
  `protected static final boolean`
  `isDoubleQuote(int c)`
  Checks whether a character code is a double quote.
  `protected static final boolean`
  `isEscape(int c)`
  Checks whether a character code is the JSON escape marker.
  `protected static final boolean`
  `isNumberDigit(int c)`
  Checks whether a character code is an ASCII digit.
  `Object`
  `parse(byte[] bytes)`
  Parses JSON bytes using the parser's configured charset.
  `Object`
  `parse(byte[] bytes, String charset)`
  Parses JSON bytes using the supplied charset.
  `Object`
  `parse(File file, String charset)`
  Parses JSON from a file.
  `Object`
  `parse(InputStream input)`
  Parses JSON from a byte stream using the configured charset.
  `Object`
  `parse(InputStream input, String charset)`
  Parses JSON from a byte stream using the supplied charset.
  `Object`
  `parse(Reader reader)`
  Parses JSON from a reader.
  `Object`
  `parse(CharSequence charSequence)`
  Parses JSON from a character sequence.
  `Object`
  `parse(String jsonString)`
  Parses a JSON string.
  `protected final void`
  `resetNesting()`
  Resets the nesting-depth counter; called at the start of a top-level parse so a reused parser instance starts cleanly.
  `void`
  `setCharset(String charset)`
  Sets the default charset used for byte and stream input.
  `void`
  `setMaxNestingDepth(int maxNestingDepth)`
  Sets the maximum nesting depth permitted while parsing.
  `void`
  `setMaxNumberLength(int maxNumberLength)`
  Sets the maximum length permitted for a single number token.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface groovy.json.JsonParser

  `parse`

- ## Field Details

  - ### COLON

    protected static final int COLON
    Character code for the `:` name/value separator.
    See Also:
      - Constant Field Values

  - ### COMMA

    protected static final int COMMA
    Character code for the `,` element separator.
    See Also:
      - Constant Field Values

  - ### CLOSED_CURLY

    protected static final int CLOSED_CURLY
    Character code for the closing object delimiter }.
    See Also:
      - Constant Field Values

  - ### CLOSED_BRACKET

    protected static final int CLOSED_BRACKET
    Character code for the closing array delimiter `]`.
    See Also:
      - Constant Field Values

  - ### LETTER_E

    protected static final int LETTER_E
    Lowercase exponent marker used in JSON numbers.
    See Also:
      - Constant Field Values

  - ### LETTER_BIG_E

    protected static final int LETTER_BIG_E
    Uppercase exponent marker used in JSON numbers.
    See Also:
      - Constant Field Values

  - ### MINUS

    protected static final int MINUS
    Character code for the minus sign in number literals.
    See Also:
      - Constant Field Values

  - ### PLUS

    protected static final int PLUS
    Character code for the plus sign in exponent literals.
    See Also:
      - Constant Field Values

  - ### DECIMAL_POINT

    protected static final int DECIMAL_POINT
    Character code for the decimal point in number literals.
    See Also:
      - Constant Field Values

  - ### ALPHA_0

    protected static final int ALPHA_0
    Character code for digit `0`.
    See Also:
      - Constant Field Values

  - ### ALPHA_1

    protected static final int ALPHA_1
    Character code for digit `1`.
    See Also:
      - Constant Field Values

  - ### ALPHA_2

    protected static final int ALPHA_2
    Character code for digit `2`.
    See Also:
      - Constant Field Values

  - ### ALPHA_3

    protected static final int ALPHA_3
    Character code for digit `3`.
    See Also:
      - Constant Field Values

  - ### ALPHA_4

    protected static final int ALPHA_4
    Character code for digit `4`.
    See Also:
      - Constant Field Values

  - ### ALPHA_5

    protected static final int ALPHA_5
    Character code for digit `5`.
    See Also:
      - Constant Field Values

  - ### ALPHA_6

    protected static final int ALPHA_6
    Character code for digit `6`.
    See Also:
      - Constant Field Values

  - ### ALPHA_7

    protected static final int ALPHA_7
    Character code for digit `7`.
    See Also:
      - Constant Field Values

  - ### ALPHA_8

    protected static final int ALPHA_8
    Character code for digit `8`.
    See Also:
      - Constant Field Values

  - ### ALPHA_9

    protected static final int ALPHA_9
    Character code for digit `9`.
    See Also:
      - Constant Field Values

  - ### DOUBLE_QUOTE

    protected static final int DOUBLE_QUOTE
    Character code for the JSON string delimiter `"`.
    See Also:
      - Constant Field Values

  - ### ESCAPE

    protected static final int ESCAPE
    Character code for the JSON escape marker `\`.
    See Also:
      - Constant Field Values

  - ### DEFAULT_MAX_NESTING_DEPTH

    public static final int DEFAULT_MAX_NESTING_DEPTH
    Default maximum nesting depth for arrays/objects. A small, crafted but deeply-nested document would otherwise drive the recursive-descent parsers into a `StackOverflowError`; this Groovy-level bound turns that into a clean `JsonException` instead. Chosen to match Jackson's `StreamReadConstraints` default so JSON is bounded consistently with the Jackson-backed YAML/TOML/CSV slurpers, while sitting far above any realistic document.
    See Also:
      - Constant Field Values

  - ### maxNestingDepth

    protected int maxNestingDepth
    Maximum nesting depth permitted for this parser. A value of `0` or less disables the check (restoring the previous, unbounded behaviour). Defaults to `DEFAULT_MAX_NESTING_DEPTH`, overridable globally via the `groovy.json.maxNestingDepth` system property.

  - ### DEFAULT_MAX_NUMBER_LENGTH

    public static final int DEFAULT_MAX_NUMBER_LENGTH
    Default maximum length, in characters, of a single JSON number token. Converting a digit run past the `long` range builds a `BigInteger` (or `BigDecimal`), and decimal conversion is superlinear in the digit count, so an uncapped token lets a small document cost quadratic CPU. Chosen to match Jackson's `StreamReadConstraints` default, while sitting far above any realistic number.
    See Also:
      - Constant Field Values

  - ### maxNumberLength

    protected int maxNumberLength
    Maximum length permitted for a single number token. A value of `0` or less disables the check (restoring the previous, unbounded behaviour). Defaults to `DEFAULT_MAX_NUMBER_LENGTH`, overridable globally via the `groovy.json.maxNumberLength` system property.

  - ### internKeys

    protected static final boolean internKeys
    Whether parsed object keys should be interned.

  - ### internedKeysCache

    protected static final ConcurrentHashMap<String,String> internedKeysCache
    Cache used when key interning is enabled.

  - ### charset

    protected String charset
    Charset used for byte-based input when no override is supplied.

  - ### bufSize

    protected int bufSize
    Initial buffer size used when reading character streams.

- ## Constructor Details

  - ### BaseJsonParser

    public BaseJsonParser()

- ## Method Details

  - ### charDescription

    protected String charDescription(int c)
    Builds a readable description of a character code for parser errors.
    Parameters:
      `c` - character code to describe
    Returns:
      human-readable description of `c`

  - ### setCharset

    public void setCharset(String charset)
    Sets the default charset used for byte and stream input.
    Parameters:
      `charset` - charset name to use for subsequent parsing

  - ### getMaxNestingDepth

    public int getMaxNestingDepth()
    Returns the maximum nesting depth permitted while parsing.
    Returns:
      the maximum nesting depth, or a value `<= 0` when the check is disabled

  - ### setMaxNestingDepth

    public void setMaxNestingDepth(int maxNestingDepth)
    Sets the maximum nesting depth permitted while parsing. A value of `0` or less disables the check.
    Parameters:
      `maxNestingDepth` - maximum number of nested arrays/objects to allow

  - ### getMaxNumberLength

    public int getMaxNumberLength()
    Returns the maximum length permitted for a single number token.
    Returns:
      the maximum number length, or a value `<= 0` when the check is disabled

  - ### setMaxNumberLength

    public void setMaxNumberLength(int maxNumberLength)
    Sets the maximum length permitted for a single number token. A value of `0` or less disables the check.
    Parameters:
      `maxNumberLength` - maximum number of characters in a number token

  - ### checkNumberLength

    protected final void checkNumberLength(int length)
    Enforces `maxNumberLength` against a number token about to be converted.
    Parameters:
      `length` - the length, in characters, of the number token
    Throws:
      `JsonException` - if the configured maximum number length would be exceeded

  - ### enterNesting

    protected final void enterNesting()
    Records entry into a nested array or object, enforcing `maxNestingDepth`.
    Throws:
      `JsonException` - if the configured maximum nesting depth would be exceeded

  - ### exitNesting

    protected final void exitNesting()
    Records exit from a nested array or object.

  - ### resetNesting

    protected final void resetNesting()
    Resets the nesting-depth counter; called at the start of a top-level parse so a reused parser instance starts cleanly.

  - ### parse

    public Object parse(String jsonString)
    Parses a JSON string.
    Specified by:
      `parse` in interface `JsonParser`
    Parameters:
      `jsonString` - JSON text to parse
    Returns:
      parsed Groovy JSON value

  - ### parse

    public Object parse(byte[] bytes)
    Parses JSON bytes using the parser's configured charset.
    Specified by:
      `parse` in interface `JsonParser`
    Parameters:
      `bytes` - JSON bytes to parse
    Returns:
      parsed Groovy JSON value

  - ### parse

    public Object parse(byte[] bytes, String charset)
    Parses JSON bytes using the supplied charset.
    Specified by:
      `parse` in interface `JsonParser`
    Parameters:
      `bytes` - JSON bytes to parse
      `charset` - charset name to use
    Returns:
      parsed Groovy JSON value

  - ### parse

    public Object parse(CharSequence charSequence)
    Parses JSON from a character sequence.
    Specified by:
      `parse` in interface `JsonParser`
    Parameters:
      `charSequence` - JSON text to parse
    Returns:
      parsed Groovy JSON value

  - ### parse

    public Object parse(Reader reader)
    Parses JSON from a reader.
    Specified by:
      `parse` in interface `JsonParser`
    Parameters:
      `reader` - reader supplying JSON content
    Returns:
      parsed Groovy JSON value

  - ### parse

    public Object parse(InputStream input)
    Parses JSON from a byte stream using the configured charset.
    Specified by:
      `parse` in interface `JsonParser`
    Parameters:
      `input` - input stream supplying JSON bytes
    Returns:
      parsed Groovy JSON value

  - ### parse

    public Object parse(InputStream input, String charset)
    Parses JSON from a byte stream using the supplied charset.
    Specified by:
      `parse` in interface `JsonParser`
    Parameters:
      `input` - input stream supplying JSON bytes
      `charset` - charset name to use
    Returns:
      parsed Groovy JSON value

  - ### parse

    public Object parse(File file, String charset)
    Parses JSON from a file.
    Specified by:
      `parse` in interface `JsonParser`
    Parameters:
      `file` - file containing JSON content
      `charset` - charset name to use, or the platform default when blank
    Returns:
      parsed Groovy JSON value

  - ### isDecimalChar

    protected static boolean isDecimalChar(int currentChar)
    Checks whether a character can appear in a decimal JSON number.
    Parameters:
      `currentChar` - character code to test
    Returns:
      `true` for decimal-number markers

  - ### isDelimiter

    protected static boolean isDelimiter(int c)
    Checks whether a character terminates the current JSON value.
    Parameters:
      `c` - character code to test
    Returns:
      `true` when `c` is a value delimiter

  - ### isNumberDigit

    protected static final boolean isNumberDigit(int c)
    Checks whether a character code is an ASCII digit.
    Parameters:
      `c` - character code to test
    Returns:
      `true` when `c` is between `0` and `9`

  - ### isDoubleQuote

    protected static final boolean isDoubleQuote(int c)
    Checks whether a character code is a double quote.
    Parameters:
      `c` - character code to test
    Returns:
      `true` when `c` is `"`

  - ### isEscape

    protected static final boolean isEscape(int c)
    Checks whether a character code is the JSON escape marker.
    Parameters:
      `c` - character code to test
    Returns:
      `true` when `c` is `\`

  - ### hasEscapeChar

    protected static boolean hasEscapeChar(char[] array, int index, int[] indexHolder)
    Scans a string body until it finds an escape or the closing quote.
    Parameters:
      `array` - source buffer
      `index` - index at which to start scanning
      `indexHolder` - single-item holder updated with the stop index
    Returns:
      `true` when an escape character is encountered before the closing quote

  - ### findEndQuote

    protected static int findEndQuote(char[] array, int index)
    Finds the closing quote of a JSON string while honoring escapes.
    Parameters:
      `array` - source buffer
      `index` - index at which to start scanning
    Returns:
      index of the terminating quote or the buffer end