GSON - JsonReader

The GSON's JsonReader is the GSON streaming JSON parser. The GSON JsonReader enables us to read a JSON string or file as a stream of JSON tokens.
Using this we can Iterate over token of JSON

Creating a GSON JsonReader

new JsonReader(java.io.Reader in) 

Which creates a new instance that reads a JSON-encoded stream from 'in' parameter.

Key Methods of JSONReader

Signature Description
boolean hasNext() Returns true if the current array or object has another element.
void beginObject() Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.(without using beginobject() can't read it's elements)
JsonToken peek() Returns the type of the next token without consuming it.
(JSONToekn is enum with constants BEGIN_ARRAY, BEGIN_OBJECT, BOOLEAN,NAME, NULL,NUMBER, STRING)
int nextInt() Returns the int value of the next token, consuming it.
long nextLong() Returns the long value of the next token, consuming it.
String nextString() Returns the String value of the next token, consuming it.

Iterating the JSON Tokens of a JsonReader

Once we have created a JsonReader instance we can iterate through the JSON tokens. It reads from the Reader passed to the JsonReader's constructor.
The main loop for iterating the JSON tokens of a JsonReader looks like this:
while(jsonReader.hasNext() {
}
The hasNext() method of the JsonReader returns true if the JsonReader has more tokens.
To access the tokens of the JsonReader we use a loop similar to the following:

/**
 * @author TutorialToUs.com
 */
package com.tutorialtous.gson;

import java.io.IOException;
import java.io.StringReader;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;

public class JSonReaderExample {
  public static void main(String args[]) {
    String jsonstring = "{\"name\":\"Alex\",\"age\":29}";
    JsonReader reader = new JsonReader(new StringReader(jsonstring));
    try {
      while (reader.hasNext()) {
        JsonToken nextToken = reader.peek();
        System.out.println("TokenType to Process:"+nextToken);
        if (JsonToken.BEGIN_OBJECT.equals(nextToken)) {
          reader.beginObject();
        } else if (JsonToken.NAME.equals(nextToken)) {
          String name = reader.nextName();
          System.out.println(name);
        } else if (JsonToken.STRING.equals(nextToken)) {
          String value = reader.nextString();
          System.out.println(value);
        } else if (JsonToken.NUMBER.equals(nextToken)) {
          long value = reader.nextLong();
          System.out.println(value);
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
TokenType to Process:BEGIN_OBJECT
TokenType to Process:NAME
name
TokenType to Process:STRING
Alex
TokenType to Process:NAME
age
TokenType to Process:NUMBER
29

The JsonReader peek() method returns the next JSON token, but without moving over it.

Multiple calls to peek() subsequently will return the same JSON token.
The JsonToken returned by peek() can be compared to constants in the JsonToken class to find out what type of token it is. we can see that done in the loop above.
Inside each 'if' statement a JsonReader method is called which moves the JsonReader over the current token, and on to the next token. All of beginObject(), nextString() and nextLong() return the value of the current token and moves the internal pointer to the next.

Copyright © 2018-2020 TutorialToUs. All rights reserved.