- Gson - Home
- Gson - Overview
- Gson - Environment Setup
- Gson - First Application
- Gson - Class
- Gson - Object Serialization
- Gson - Data Binding
- Gson - Object Data Binding
- Gson - Tree Model
- Gson - Streaming
- Gson - Serialization Examples
- Gson - Serializing Inner Classes
- Gson - Custom Type Adapters
- Gson - Null Object Support
- Gson - Versioning Support
- Excluding fields from Serialization
GSON - Useful Resources
GSON - Streaming
Overview
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 - Streaming API
GsonTester.java
package com.tutorialspoint;
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() + " ");
}
}
}
}
Output
Run the GsonTester and verify the output.
Name: Mahesh Kumar Age:21 Verified:false Marks [ 100 90 85 ]
Advertisements