JsonReaderJsonWriter

Gson Streaming to read and write JSON in Java example

In this example we are going to see GSON straming API to read and write JSON in a Java application. Streaming operation is the fastest and most efficient way to process JSON data. On the other hand it can be tricky to use, especially when reading from JSON represenation, because you have to handle all the data manually. You can take look ta the Jackson Streaming API to get the notion of streaming processing.
 
 
 
 
 
 

1. Write JSON to a File using JsonWriter

GsonStreamAPIExample.java:

package com.javacodegeeks.java.core;

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

public class GsonStreamAPIExample {

	private static final String jsonFilePath = "C:\\Users\\nikos7\\Desktop\\filesForExamples\\jsonFile.json";

	public static void main(String[] args) {

		try {

			FileWriter fileWriter = new FileWriter(jsonFilePath);

			JsonWriter jsonWriter = new JsonWriter(fileWriter);

			jsonWriter.beginObject(); 

			jsonWriter.name("domain").value("www.javacodegeeks.com");

			jsonWriter.name("members").value(200);

			jsonWriter.name("names");

			jsonWriter.beginArray();

			jsonWriter.value("Jack");

			jsonWriter.value("James");

			jsonWriter.value(3);

			jsonWriter.endArray();

			jsonWriter.endObject();

			jsonWriter.close();

			System.out.println("The file was created successfully!");

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

jsonFile.json:

{"domain":"www.javacodegeeks.com","members":200,"names":["Jack","James",3]}

1. Read JSON from File using JsonReader

This is the tricky part in the Streaming API. In this, every single string is considered a token. It’s a classic parsing procedure.

We are going to use jsonFile.json as an input file for the demp.

GsonStreamAPIExample.java:

package com.javacodegeeks.java.core;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import com.google.gson.stream.JsonReader;

public class GsonStreamAPIExample {

	private static final String jsonFilePath = "C:\\Users\\nikos7\\Desktop\\filesForExamples\\jsonFile.json";

	public static void main(String[] args) {

		try {

			FileReader fileReader = new FileReader(jsonFilePath);

			JsonReader jsonReader = new JsonReader(fileReader);

			jsonReader.beginObject();

			while (jsonReader.hasNext()) {

				String name = jsonReader.nextName();

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

					System.out.println("domain: "+jsonReader.nextString());

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

					System.out.println("members: "+jsonReader.nextInt());

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

					System.out.println("names: ");

					jsonReader.beginArray();

					while (jsonReader.hasNext()) {
						System.out.println(" "+jsonReader.nextString());
					}

					jsonReader.endArray();

				} else {
					// use this when you are not sure about all the contents in th JSON file
					jsonReader.skipValue();
				}
			}

			jsonReader.endObject();
			jsonReader.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

output:

domain: www.javacodegeeks.com
members: 200
names: 
 Jack
 James
 3

 
This was an example on how to use Gson Streaming API to read and write JSON in Java.

Nikos Maravitsas

Nikos has graduated from the Department of Informatics and Telecommunications of The National and Kapodistrian University of Athens. During his studies he discovered his interests about software development and he has successfully completed numerous assignments in a variety of fields. Currently, his main interests are system’s security, parallel systems, artificial intelligence, operating systems, system programming, telecommunications, web applications, human – machine interaction and mobile development.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button