Class JsonReader

  • All Implemented Interfaces:
    Closeable, AutoCloseable

    public class JsonReader
    extends Object
    implements Closeable
    Reads a JSON (RFC 8259) encoded value as a stream of tokens. This stream includes both literal values (strings, numbers, booleans, and nulls) as well as the begin and end delimiters of objects and arrays. The tokens are traversed in depth-first order, the same order that they appear in the JSON document. Within JSON objects, name/value pairs are represented by a single token.

    Parsing JSON

    To create a recursive descent parser for your own JSON streams, first create an entry point method that creates a JsonReader.

    Next, create handler methods for each structure in your JSON text. You'll need a method for each object type and for each array type.

    • Within array handling methods, first call beginArray() to consume the array's opening bracket. Then create a while loop that accumulates values, terminating when hasNext() is false. Finally, read the array's closing bracket by calling endArray().
    • Within object handling methods, first call beginObject() to consume the object's opening brace. Then create a while loop that assigns values to local variables based on their name. This loop should terminate when hasNext() is false. Finally, read the object's closing brace by calling endObject().

    When a nested object or array is encountered, delegate to the corresponding handler method.

    When an unknown name is encountered, strict parsers should fail with an exception. Lenient parsers should call skipValue() to recursively skip the value's nested tokens, which may otherwise conflict.

    If a value may be null, you should first check using peek(). Null literals can be consumed using either nextNull() or skipValue().

    Configuration

    The behavior of this reader can be customized with the following methods: The default configuration of JsonReader instances used internally by the Gson class differs, and can be adjusted with the various GsonBuilder methods.

    Example

    Suppose we'd like to parse a stream of messages such as the following:
    
     [
       {
         "id": 912345678901,
         "text": "How do I read a JSON stream in Java?",
         "geo": null,
         "user": {
           "name": "json_newb",
           "followers_count": 41
          }
       },
       {
         "id": 912345678902,
         "text": "@json_newb just use JsonReader!",
         "geo": [50.454722, -104.606667],
         "user": {
           "name": "jesse",
           "followers_count": 2
         }
       }
     ]
     
    This code implements the parser for the above structure:
    
     public List<Message> readJsonStream(InputStream in) throws IOException {
       JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
       try {
         return readMessagesArray(reader);
       } finally {
         reader.close();
       }
     }
    
     public List<Message> readMessagesArray(JsonReader reader) throws IOException {
       List<Message> messages = new ArrayList<>();
    
       reader.beginArray();
       while (reader.hasNext()) {
         messages.add(readMessage(reader));
       }
       reader.endArray();
       return messages;
     }
    
     public Message readMessage(JsonReader reader) throws IOException {
       long id = -1;
       String text = null;
       User user = null;
       List<Double> geo = null;
    
       reader.beginObject();
       while (reader.hasNext()) {
         String name = reader.nextName();
         if (name.equals("id")) {
           id = reader.nextLong();
         } else if (name.equals("text")) {
           text = reader.nextString();
         } else if (name.equals("geo") && reader.peek() != JsonToken.NULL) {
           geo = readDoublesArray(reader);
         } else if (name.equals("user")) {
           user = readUser(reader);
         } else {
           reader.skipValue();
         }
       }
       reader.endObject();
       return new Message(id, text, user, geo);
     }
    
     public List<Double> readDoublesArray(JsonReader reader) throws IOException {
       List<Double> doubles = new ArrayList<>();
    
       reader.beginArray();
       while (reader.hasNext()) {
         doubles.add(reader.nextDouble());
       }
       reader.endArray();
       return doubles;
     }
    
     public User readUser(JsonReader reader) throws IOException {
       String username = null;
       int followersCount = -1;
    
       reader.beginObject();
       while (reader.hasNext()) {
         String name = reader.nextName();
         if (name.equals("name")) {
           username = reader.nextString();
         } else if (name.equals("followers_count")) {
           followersCount = reader.nextInt();
         } else {
           reader.skipValue();
         }
       }
       reader.endObject();
       return new User(username, followersCount);
     }
     

    Number Handling

    This reader permits numeric values to be read as strings and string values to be read as numbers. For example, both elements of the JSON array [1, "1"] may be read using either nextInt() or nextString(). This behavior is intended to prevent lossy numeric conversions: double is JavaScript's only numeric type and very large values like 9007199254740993 cannot be represented exactly on that platform. To minimize precision loss, extremely large values should be written and read as strings in JSON.

    Non-Execute Prefix

    Web servers that serve private data using JSON may be vulnerable to Cross-site request forgery attacks. In such an attack, a malicious site gains access to a private JSON file by executing it with an HTML <script> tag.

    Prefixing JSON files with ")]}'\n" makes them non-executable by <script> tags, disarming the attack. Since the prefix is malformed JSON, strict parsing fails when it is encountered. This class permits the non-execute prefix when lenient parsing is enabled.

    Each JsonReader may be used to read a single JSON stream. Instances of this class are not thread safe.

    Since:
    1.6
    Author:
    Jesse Wilson
    • Constructor Detail

      • JsonReader

        public JsonReader​(Reader in)
        Creates a new instance that reads a JSON-encoded stream from in.
    • Method Detail

      • setLenient

        @Deprecated
        public final void setLenient​(boolean lenient)
        Deprecated.
        Please use setStrictness(Strictness) instead. JsonReader.setLenient(true) should be replaced by JsonReader.setStrictness(Strictness.LENIENT) and JsonReader.setLenient(false) should be replaced by JsonReader.setStrictness(Strictness.LEGACY_STRICT).
        However, if you used setLenient(false) before, you might prefer Strictness.STRICT now instead.
        Sets the strictness of this reader.
        Parameters:
        lenient - whether this reader should be lenient. If true, the strictness is set to Strictness.LENIENT. If false, the strictness is set to Strictness.LEGACY_STRICT.
        See Also:
        setStrictness(Strictness)
      • setStrictness

        public final void setStrictness​(Strictness strictness)
        Configures how liberal this parser is in what it accepts.

        In strict mode, the parser only accepts JSON in accordance with RFC 8259. In legacy strict mode (the default), only JSON in accordance with the RFC 8259 is accepted, with a few exceptions denoted below for backwards compatibility reasons. In lenient mode, all sort of non-spec compliant JSON is accepted (see below).

        Strictness.STRICT
        In strict mode, only input compliant with RFC 8259 is accepted.
        Strictness.LEGACY_STRICT
        In legacy strict mode, the following departures from RFC 8259 are accepted:
        • JsonReader allows the literals true, false and null to have any capitalization, for example fAlSe or NULL
        • JsonReader supports the escape sequence \', representing a ' (single-quote)
        • JsonReader supports the escape sequence \LF (with LF being the Unicode character U+000A), resulting in a LF within the read JSON string
        • JsonReader allows unescaped control characters (U+0000 through U+001F)
        Strictness.LENIENT
        In lenient mode, all input that is accepted in legacy strict mode is accepted in addition to the following departures from RFC 8259:
        • Streams that start with the non-execute prefix, ")]'\n"}
        • Streams that include multiple top-level values. With legacy strict or strict parsing, each stream must contain exactly one top-level value.
        • Numbers may be NaNs or infinities represented by NaN and (-)Infinity respectively.
        • End of line comments starting with // or # and ending with a newline character.
        • C-style comments starting with /* and ending with */. Such comments may not be nested.
        • Names that are unquoted or 'single quoted'.
        • Strings that are unquoted or 'single quoted'.
        • Array elements separated by ; instead of ,.
        • Unnecessary array separators. These are interpreted as if null was the omitted value.
        • Names and values separated by = or => instead of :.
        • Name/value pairs separated by ; instead of ,.
        Parameters:
        strictness - the new strictness value of this reader. May not be null.
        Since:
        2.11.0
        See Also:
        getStrictness()
      • setNestingLimit

        public final void setNestingLimit​(int limit)
        Sets the nesting limit of this reader.

        The nesting limit defines how many JSON arrays or objects may be open at the same time. For example a nesting limit of 0 means no arrays or objects may be opened at all, a nesting limit of 1 means one array or object may be open at the same time, and so on. So a nesting limit of 3 allows reading the JSON data [{"a":[true]}], but for a nesting limit of 2 it would fail at the inner [true].

        The nesting limit can help to protect against a StackOverflowError when recursive TypeAdapter implementations process deeply nested JSON data.

        The default nesting limit is 255.

        Throws:
        IllegalArgumentException - if the nesting limit is negative.
        Since:
        2.12.0
        See Also:
        getNestingLimit()
      • getNestingLimit

        public final int getNestingLimit()
        Returns the nesting limit of this reader.
        Since:
        2.12.0
        See Also:
        setNestingLimit(int)
      • beginArray

        public void beginArray()
                        throws IOException
        Consumes the next token from the JSON stream and asserts that it is the beginning of a new array.
        Throws:
        IllegalStateException - if the next token is not the beginning of an array.
        IOException
      • endArray

        public void endArray()
                      throws IOException
        Consumes the next token from the JSON stream and asserts that it is the end of the current array.
        Throws:
        IllegalStateException - if the next token is not the end of an array.
        IOException
      • beginObject

        public void beginObject()
                         throws IOException
        Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.
        Throws:
        IllegalStateException - if the next token is not the beginning of an object.
        IOException
      • endObject

        public void endObject()
                       throws IOException
        Consumes the next token from the JSON stream and asserts that it is the end of the current object.
        Throws:
        IllegalStateException - if the next token is not the end of an object.
        IOException
      • hasNext

        public boolean hasNext()
                        throws IOException
        Returns true if the current array or object has another element.
        Throws:
        IOException
      • nextNull

        public void nextNull()
                      throws IOException
        Consumes the next token from the JSON stream and asserts that it is a literal null.
        Throws:
        IllegalStateException - if the next token is not a JSON null.
        IOException
      • nextLong

        public long nextLong()
                      throws IOException
        Returns the long value of the next token, consuming it. If the next token is a string, this method will attempt to parse it as a long. If the next token's numeric value cannot be exactly represented by a Java long, this method throws.
        Throws:
        IllegalStateException - if the next token is neither a number nor a string.
        NumberFormatException - if the next literal value cannot be parsed as a number, or exactly represented as a long.
        IOException
      • nextInt

        public int nextInt()
                    throws IOException
        Returns the int value of the next token, consuming it. If the next token is a string, this method will attempt to parse it as an int. If the next token's numeric value cannot be exactly represented by a Java int, this method throws.
        Throws:
        IllegalStateException - if the next token is neither a number nor a string.
        NumberFormatException - if the next literal value cannot be parsed as a number, or exactly represented as an int.
        IOException
      • skipValue

        public void skipValue()
                       throws IOException
        Skips the next value recursively. This method is intended for use when the JSON token stream contains unrecognized or unhandled values.

        The behavior depends on the type of the next JSON token:

        • Start of a JSON array or object: It and all of its nested values are skipped.
        • Primitive value (for example a JSON number): The primitive value is skipped.
        • Property name: Only the name but not the value of the property is skipped. skipValue() has to be called again to skip the property value as well.
        • End of a JSON array or object: Only this end token is skipped.
        • End of JSON document: Skipping has no effect, the next token continues to be the end of the document.
        Throws:
        IOException
      • getPath

        public String getPath()
        Returns a JSONPath in dot-notation to the next (or current) location in the JSON document. That means:
        • For JSON arrays the path points to the index of the next element (even if there are no further elements).
        • For JSON objects the path points to the last property, or to the current property if its name has already been consumed.

        This method can be useful to add additional context to exception messages before a value is consumed, for example when the peeked token is unexpected.

      • getPreviousPath

        public String getPreviousPath()
        Returns a JSONPath in dot-notation to the previous (or current) location in the JSON document. That means:
        • For JSON arrays the path points to the index of the previous element.
          If no element has been consumed yet it uses the index 0 (even if there are no elements).
        • For JSON objects the path points to the last property, or to the current property if its name has already been consumed.

        This method can be useful to add additional context to exception messages after a value has been consumed.