Diagram of Unicast Streaming

Since Gson version 1.6, two new classes – JsonReader and JsonWriter, are introduce to provide streaming processing on JSON data. Read this Gson streaming documentation to understand what are the benefits of using it.

Here we show you two full examples of using following Gson streaming APIs to read and write JSON data.

  1. JsonWriter – Streaming write to JSON.
  2. JsonReader – Streaming read from JSON.

Gson streaming processing is fast, but difficult to code, because you need to handle each and every detail of processing JSON data.

1. JsonWriter

In this example, you use “JsonWriter” to write JSON data into a file name “file.json“. See comments for self-expalantory.

import java.io.FileWriter;
import java.io.IOException;
import com.google.gson.stream.JsonWriter;

public class GsonStreamExample {
   public static void main(String[] args) {

     JsonWriter writer;
     try {
	writer = new JsonWriter(new FileWriter("c:\\user.json"));

	writer.beginObject(); // {
	writer.name("name").value("mkyong"); // "name" : "mkyong"
	writer.name("age").value(29); // "age" : 29

	writer.name("messages"); // "messages" : 
	writer.beginArray(); // [
	writer.value("msg 1"); // "msg 1"
	writer.value("msg 2"); // "msg 2"
	writer.value("msg 3"); // "msg 3"
	writer.endArray(); // ]

	writer.endObject(); // }


     } catch (IOException e) {



As result, following new file named “file.json” is created :

	"messages":["msg 1","msg 2","msg 3"]

2. JsonReader

Example to use “JsonReader” to parse or read above file “file.json”.

In streaming mode, every JSON “data” is consider as an individual token. When you use JsonReaderto process it, each tokens will be processed sequential.For example,


Token 1 = “{”
Token 2 = “url”
Token 3 = “www.mkyong.com”
Token 4 = “}”

As result, you need to keep calling “next****” method to move to next token manually.

See full example.

package com.mkyong.core;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import com.google.gson.stream.JsonReader;

public class GsonStreamExample {
   public static void main(String[] args) {

     try {
	JsonReader reader = new JsonReader(new FileReader("c:\\user.json"));


	while (reader.hasNext()) {

	  String name = reader.nextName();

	  if (name.equals("name")) {


	  } else if (name.equals("age")) {


	  } else if (name.equals("message")) {

		// read array

		while (reader.hasNext()) {


	  } else {
		reader.skipValue(); //avoid some unhandle events


     } catch (FileNotFoundException e) {
     } catch (IOException e) {




msg 1
msg 2
msg 3

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.