Package 

Class JSONTokener


  • 
    public class JSONTokener
    
                        

    Parses a JSON (RFC 4627) encoded string into the corresponding object. Most clients of this class will use only need the constructor and nextValue method. Example usage:

    String json = "{"
            + "  \"query\": \"Pizza\", "
            + "  \"locations\": [ 94043, 90210 ] "
            + "}";
    

    For best interoperability and performance use JSON that complies with RFC 4627, such as that generated by JSONStringer. For legacy reasons this parser is lenient, so a successful parse does not indicate that the input string was valid JSON. All of the following syntax errors will be ignored:

    • End of line comments starting with {@code //} or {@code #} and ending with a newline character.
    • C-style comments starting with {@code /*} and ending with {@code *}{@code /}. Such comments may not be nested.
    • Strings that are unquoted or {@code 'single quoted'}.
    • Hexadecimal integers prefixed with {@code 0x} or {@code 0X}.
    • Octal integers prefixed with {@code 0}.
    • Array elements separated by {@code ;}.
    • Unnecessary array separators. These are interpreted as if null was the omitted value.
    • Key-value pairs separated by {@code =} or {@code =>}.
    • Key-value pairs separated by {@code ;}.

    Each tokener may be used to parse a single JSON string. Instances of this class are not thread safe. Although this class is nonfinal, it was not designed for inheritance and should not be subclassed. In particular, self-use by overrideable methods is not specified. See Effective JavaItem 17, "Design and Document or inheritance or else prohibit it" for further information.

    • Method Summary

      Modifier and Type Method Description
      Object nextValue() Returns the next value from the input.
      String nextString(char quote) Returns the string up to but not including {@code quote}, unescaping anycharacter escape sequences encountered along the way.
      JSONException syntaxError(String message) Returns an exception containing the given message plus the currentposition and the entire input string.
      String toString() Returns the current position and the entire input string.
      boolean more() Returns true until the input has been exhausted.
      char next() Returns the next available character, or the null character '\0' if allinput has been exhausted.
      char next(char c) Returns the next available character if it equals {@code c}.
      char nextClean() Returns the next character that is not whitespace and does not belong toa comment.
      String next(int length) Returns the next {@code length} characters of the input.
      String nextTo(String excluded) Returns the trimmed string holding the characters upto but not including the first of:
      • any character in {@code excluded}
      • a newline character '\n'
      • a carriage return '\r'
      The returned string shares its backing character array with thistokener's input string.
      String nextTo(char excluded) Equivalent to {@code nextTo(String.valueOf(excluded))}.
      void skipPast(String thru) Advances past all input up to and including the next occurrence of {@code thru}.
      char skipTo(char to) Advances past all input up to but not including the next occurrence of {@code to}.
      void back() Unreads the most recent character of input.
      static int dehexchar(char hex) Returns the integer [0..15] value for the given hex character, or -1for non-hex input.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JSONTokener

        JSONTokener(String in)
        Parameters:
        in - JSON encoded string.
    • Method Detail

      • nextString

         String nextString(char quote)

        Returns the string up to but not including {@code quote}, unescaping anycharacter escape sequences encountered along the way. The opening quoteshould have already been read. This consumes the closing quote, but doesnot include it in the returned string.

        Parameters:
        quote - either ' or ".
      • syntaxError

         JSONException syntaxError(String message)

        Returns an exception containing the given message plus the currentposition and the entire input string.

      • toString

         String toString()

        Returns the current position and the entire input string.

      • more

         boolean more()

        Returns true until the input has been exhausted.

      • next

         char next()

        Returns the next available character, or the null character '\0' if allinput has been exhausted. The return value of this method is ambiguousfor JSON strings that contain the character '\0'.

      • next

         char next(char c)

        Returns the next available character if it equals {@code c}. Otherwise anexception is thrown.

      • nextClean

         char nextClean()

        Returns the next character that is not whitespace and does not belong toa comment. If the input is exhausted before such a character can befound, the null character '\0' is returned. The return value of thismethod is ambiguous for JSON strings that contain the character '\0'.

      • next

         String next(int length)

        Returns the next {@code length} characters of the input.

        The returned string shares its backing character array with thistokener's input string. If a reference to the returned string may be heldindefinitely, you should use {@code new String(result)} to copy it firstto avoid memory leaks.

      • nextTo

         String nextTo(String excluded)

        Returns the trimmed string holding the characters upto but not including the first of:

        • any character in {@code excluded}
        • a newline character '\n'
        • a carriage return '\r'

        The returned string shares its backing character array with thistokener's input string. If a reference to the returned string may be heldindefinitely, you should use {@code new String(result)} to copy it firstto avoid memory leaks.

      • nextTo

         String nextTo(char excluded)

        Equivalent to {@code nextTo(String.valueOf(excluded))}.

      • skipPast

         void skipPast(String thru)

        Advances past all input up to and including the next occurrence of {@code thru}. If the remaining input doesn't contain {@code thru}, theinput is exhausted.

      • skipTo

         char skipTo(char to)

        Advances past all input up to but not including the next occurrence of {@code to}. If the remaining input doesn't contain {@code to}, the inputis unchanged.

      • back

         void back()

        Unreads the most recent character of input. If no input characters havebeen read, the input is unchanged.

      • dehexchar

         static int dehexchar(char hex)

        Returns the integer [0..15] value for the given hex character, or -1for non-hex input.

        Parameters:
        hex - a character in the ranges [0-9], [A-F] or [a-f].