Saturday, February 23, 2019

Gson - Streaming

Streaming API is used to read JSON token by token. It reads and writes JSON content as discrete events. JsonReader and JsonWriter read/write the data as token, referred as JsonToken.
It is the most powerful approach among the three approaches to process JSON. It has the lowest overhead and it is quite fast in read/write operations. It is analogous to Stax parser for XML.
In this chapter, we will showcase the usage of GSON streaming APIs to read JSON data. Streaming API works with the concept of token and every details of Json is to be handled carefully.
//create JsonReader object and pass it the json source or json text. 
JsonReader reader = new JsonReader(new StringReader(jsonString));  

//start reading json   
reader.beginObject(); 

//get the next token 
JsonToken token = reader.peek(); 

//check the type of the token 
if (token.equals(JsonToken.NAME)) {     
   //get the current token 
   fieldname = reader.nextName(); 
}

Example

Let's see JsonReader in action. Create a Java class file named GsonTester in C:\>GSON_WORKSPACE.

File - GsonTester.java

import java.io.IOException; 
import java.io.StringReader;  

import com.google.gson.stream.JsonReader; 
import com.google.gson.stream.JsonToken;  

public class GsonTester { 
   public static void main(String args[]) { 
   
      String jsonString = 
         "{\"name\":\"Mahesh Kumar\", \"age\":21,\"verified\":false,\"marks\": [100,90,85]}";  
      JsonReader reader = new JsonReader(new StringReader(jsonString));    
      try { 
         handleJsonObject(reader); 
      } 
      catch (IOException e) { 
         e.printStackTrace(); 
      } 
   } 
   
   private static void handleJsonObject(JsonReader reader) throws IOException { 
      reader.beginObject(); 
      String fieldname = null; 
      
      while (reader.hasNext()) { 
         JsonToken token = reader.peek(); 
         
         if (token.equals(JsonToken.BEGIN_ARRAY)) { 
            System.out.print("Marks [ "); 
            handleJsonArray(reader); 
            System.out.print("]"); 
         } else if (token.equals(JsonToken.END_OBJECT)) { 
            reader.endObject(); 
            return; 
         } else {            
            if (token.equals(JsonToken.NAME)) {     
               //get the current token 
               fieldname = reader.nextName(); 
            } 
            
            if ("name".equals(fieldname)) {       
               //move to next token 
               token = reader.peek(); 
               System.out.println("Name: "+reader.nextString());           
            } 
            
            if("age".equals(fieldname)) { 
               //move to next token 
               token = reader.peek(); 
               System.out.println("Age:" + reader.nextInt());       
            } 
            
            if("verified".equals(fieldname)) { 
               //move to next token 
               token = reader.peek(); 
               System.out.println("Verified:" + reader.nextBoolean());           
            }             
         } 
      } 
   }  
   
   private static void handleJsonArray(JsonReader reader) throws IOException { 
      reader.beginArray(); 
      String fieldname = null; 
      
      while (true) { 
         JsonToken token = reader.peek(); 
         
         if (token.equals(JsonToken.END_ARRAY)) { 
            reader.endArray(); 
            break; 
         } else if (token.equals(JsonToken.BEGIN_OBJECT)) { 
            handleJsonObject(reader); 
         } else if (token.equals(JsonToken.END_OBJECT)) { 
            reader.endObject(); 
         } else {            
            System.out.print(reader.nextInt() + " ");            
         } 
      } 
   } 
}

Verify the result

Compile the classes using javac compiler as follows −
C:\GSON_WORKSPACE>javac GsonTester.java 
Now run the GsonTester to see the result −
C:\GSON_WORKSPACE>java GsonTester 
Verify the output.
Name: Mahesh Kumar 
Age:21 
Verified:false 
Marks [ 100 90 85 ] 

No comments:

Post a Comment

Popular Posts