Package org.json

Class JSONTokener

java.lang.Object
org.json.JSONTokener
Direct Known Subclasses:
HTTPTokener, XMLTokener

public class JSONTokener extends Object
A JSONTokener takes a source string and extracts characters and tokens from it. It is used by the JSONObject and JSONArray constructors to parse JSON source strings.
Version:
2014-05-03
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private long
    current read character position on the current line.
    private long
    the number of characters read in the previous line.
    private boolean
    flag to indicate if the end of the input has been found.
    private long
    current read index of the input.
    private long
    current line of the input.
    private char
    previous character read from the input.
    private final Reader
    Reader for the input.
    private boolean
    flag to indicate that a previous character was requested.
  • Constructor Summary

    Constructors
    Constructor
    Description
    JSONTokener(InputStream inputStream)
    Construct a JSONTokener from an InputStream.
    Construct a JSONTokener from a Reader.
    Construct a JSONTokener from a string.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Back up one character.
    void
    Closes the underlying reader, releasing any resources associated with it.
    private void
    Decrements the indexes for the back() method based on the previous character read.
    static int
    dehexchar(char c)
    Get the hex value of a character (base16).
    boolean
    end()
    Checks if the end of the input has been reached.
    protected char
    Get the last character read from the input or '\0' if nothing has been read yet.
    private void
    Increments the internal indexes according to the previous character read and the character passed as the current character.
    boolean
    Determine if the source string still contains characters that next() can consume.
    char
    Get the next character in the source string.
    char
    next(char c)
    Consume the next character, and check that it matches a specified character.
    next(int n)
    Get the next n characters.
    char
    Get the next char in the string, skipping whitespace.
    (package private) Object
     
    nextString(char quote)
    Return the characters up to the next close quote character.
    nextTo(char delimiter)
    Get the text up but not including the specified character or the end of line, whichever comes first.
    nextTo(String delimiters)
    Get the text up but not including one of the specified delimiter characters or the end of line, whichever comes first.
    Get the next value.
    char
    skipTo(char to)
    Skip characters until the next character is the requested character.
    Make a JSONException to signal a syntax error.
    syntaxError(String message, Throwable causedBy)
    Make a JSONException to signal a syntax error.
    Make a printable string of this JSONTokener.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • character

      private long character
      current read character position on the current line.
    • eof

      private boolean eof
      flag to indicate if the end of the input has been found.
    • index

      private long index
      current read index of the input.
    • line

      private long line
      current line of the input.
    • previous

      private char previous
      previous character read from the input.
    • reader

      private final Reader reader
      Reader for the input.
    • usePrevious

      private boolean usePrevious
      flag to indicate that a previous character was requested.
    • characterPreviousLine

      private long characterPreviousLine
      the number of characters read in the previous line.
  • Constructor Details

    • JSONTokener

      public JSONTokener(Reader reader)
      Construct a JSONTokener from a Reader. The caller must close the Reader.
      Parameters:
      reader - A reader.
    • JSONTokener

      public JSONTokener(InputStream inputStream)
      Construct a JSONTokener from an InputStream. The caller must close the input stream.
      Parameters:
      inputStream - The source.
    • JSONTokener

      public JSONTokener(String s)
      Construct a JSONTokener from a string.
      Parameters:
      s - A source string.
  • Method Details

    • back

      public void back() throws JSONException
      Back up one character. This provides a sort of lookahead capability, so that you can test for a digit or letter before attempting to parse the next number or identifier.
      Throws:
      JSONException - Thrown if trying to step back more than 1 step or if already at the start of the string
    • decrementIndexes

      private void decrementIndexes()
      Decrements the indexes for the back() method based on the previous character read.
    • dehexchar

      public static int dehexchar(char c)
      Get the hex value of a character (base16).
      Parameters:
      c - A character between '0' and '9' or between 'A' and 'F' or between 'a' and 'f'.
      Returns:
      An int between 0 and 15, or -1 if c was not a hex digit.
    • end

      public boolean end()
      Checks if the end of the input has been reached.
      Returns:
      true if at the end of the file and we didn't step back
    • more

      public boolean more() throws JSONException
      Determine if the source string still contains characters that next() can consume.
      Returns:
      true if not yet at the end of the source.
      Throws:
      JSONException - thrown if there is an error stepping forward or backward while checking for more data.
    • next

      public char next() throws JSONException
      Get the next character in the source string.
      Returns:
      The next character, or 0 if past the end of the source string.
      Throws:
      JSONException - Thrown if there is an error reading the source string.
    • getPrevious

      protected char getPrevious()
      Get the last character read from the input or '\0' if nothing has been read yet.
      Returns:
      the last character read from the input.
    • incrementIndexes

      private void incrementIndexes(int c)
      Increments the internal indexes according to the previous character read and the character passed as the current character.
      Parameters:
      c - the current character read.
    • next

      public char next(char c) throws JSONException
      Consume the next character, and check that it matches a specified character.
      Parameters:
      c - The character to match.
      Returns:
      The character.
      Throws:
      JSONException - if the character does not match.
    • next

      public String next(int n) throws JSONException
      Get the next n characters.
      Parameters:
      n - The number of characters to take.
      Returns:
      A string of n characters.
      Throws:
      JSONException - Substring bounds error if there are not n characters remaining in the source string.
    • nextClean

      public char nextClean() throws JSONException
      Get the next char in the string, skipping whitespace.
      Returns:
      A character, or 0 if there are no more characters.
      Throws:
      JSONException - Thrown if there is an error reading the source string.
    • nextString

      public String nextString(char quote) throws JSONException
      Return the characters up to the next close quote character. Backslash processing is done. The formal JSON format does not allow strings in single quotes, but an implementation is allowed to accept them.
      Parameters:
      quote - The quoting character, either " (double quote) or ' (single quote).
      Returns:
      A String.
      Throws:
      JSONException - Unterminated string.
    • nextTo

      public String nextTo(char delimiter) throws JSONException
      Get the text up but not including the specified character or the end of line, whichever comes first.
      Parameters:
      delimiter - A delimiter character.
      Returns:
      A string.
      Throws:
      JSONException - Thrown if there is an error while searching for the delimiter
    • nextTo

      public String nextTo(String delimiters) throws JSONException
      Get the text up but not including one of the specified delimiter characters or the end of line, whichever comes first.
      Parameters:
      delimiters - A set of delimiter characters.
      Returns:
      A string, trimmed.
      Throws:
      JSONException - Thrown if there is an error while searching for the delimiter
    • nextValue

      public Object nextValue() throws JSONException
      Get the next value. The value can be a Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the JSONObject.NULL object.
      Returns:
      An object.
      Throws:
      JSONException - If syntax error.
    • nextSimpleValue

      Object nextSimpleValue(char c)
    • skipTo

      public char skipTo(char to) throws JSONException
      Skip characters until the next character is the requested character. If the requested character is not found, no characters are skipped.
      Parameters:
      to - A character to skip to.
      Returns:
      The requested character, or zero if the requested character is not found.
      Throws:
      JSONException - Thrown if there is an error while searching for the to character
    • syntaxError

      public JSONException syntaxError(String message)
      Make a JSONException to signal a syntax error.
      Parameters:
      message - The error message.
      Returns:
      A JSONException object, suitable for throwing
    • syntaxError

      public JSONException syntaxError(String message, Throwable causedBy)
      Make a JSONException to signal a syntax error.
      Parameters:
      message - The error message.
      causedBy - The throwable that caused the error.
      Returns:
      A JSONException object, suitable for throwing
    • toString

      public String toString()
      Make a printable string of this JSONTokener.
      Overrides:
      toString in class Object
      Returns:
      " at {index} [character {character} line {line}]"
    • close

      public void close() throws IOException
      Closes the underlying reader, releasing any resources associated with it.
      Throws:
      IOException - If an I/O error occurs while closing the reader.