You could read & write file using Gson streaming. It is a powerful feature of Gson. Gson Streaming API is based on sequential read & write standard. And JsonWriter & JsonReader are core classes built for Streaming Write and Read in Streaming API.
Other interesting posts you may like
I show you how to use JsonWriter and JsonReader via two programs below:
Gson library dependency should be added in pom.xml
1 2 3 4 5 |
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> </dependency> |
Write JSON data using JsonWriter class
Basically in this idea, you should build the JSON string from java object using methods beginObject/endObject & method beginArray/endArray.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.javabycode.json; import java.io.FileWriter; import com.google.gson.stream.JsonWriter; public class GsonWriteStreamingSample { public static void main(String args[]) { JsonWriter writer; try { writer = new JsonWriter(new FileWriter("input.json")); writer.beginObject(); writer.name("name").value("Orange"); writer.name("cost").value(78); writer.name("pruduceby"); writer.beginArray(); writer.value("Cuba"); writer.value("Indonesia"); writer.value("Brazil"); writer.endArray(); writer.endObject(); writer.close(); } catch (Exception e) { e.printStackTrace(); } } } |
Here is output when you run this program
1 |
{"name":"Orange","cost":78,"pruduceby":["Cuba","Indonesia","Brazil"]} |
Read the JSON data from a file.
Basically you loop over the input json string and read each token subsequently using hasNext & nextName methods.
It’s is same as writing, the methods beginObject/endObject & beginArray/endArray is used to read opening/closing brackets.
The output file generated from above is input of this program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.javabycode.json; import java.io.FileReader; import com.google.gson.stream.JsonReader; public class GsonReadStreamingSample { public static void main(String args[]) { JsonReader reader; try { reader = new JsonReader(new FileReader("C:/javabycode/json/input.json")); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("name")) { System.out.println(reader.nextString()); } else if (name.equals("cost")) { System.out.println(reader.nextInt()); } else if (name.equals("pruduceby")) { // it's an array in a json structure. reader.beginArray(); while (reader.hasNext()) { System.out.println(reader.nextString()); } reader.endArray(); } else {// skip the unexpected value reader.skipValue(); } } reader.endObject(); reader.close(); } catch (Exception e) { e.printStackTrace(); } } } |
Here is output of the above program
1 2 3 4 5 |
Orange 78 Cuba Indonesia Brazil |
After reading json content from file you have the output and want to build json object from it, please refer the article
If you have any question or other opinion about how to read & write file using Gson streaming, please leave a comment.
Happy learing!