- Excluding fields from Serialization
- Gson - Versioning Support
- Gson - Null Object Support
- Gson - Custom Type Adapters
- Gson - Serializing Inner Classes
- Gson - Serialization Examples
- Gson - Streaming
- Gson - Tree Model
- Gson - Object Data Binding
- Gson - Data Binding
- Gson - Object Serialization
- Gson - Class
- Gson - First Application
- Gson - Environment Setup
- Gson - Overview
- Gson - Home
Gson Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Gson - Seriapzation Examples
In this chapter, we will discuss the seriapzation/deseriapzation of arrays, collections, and generics.
Array Example
int[] marks = {100,90,85}; //Seriapzation System.out.println("marks:" + gson.toJson(marks)); //De-seriapzation marks = gson.fromJson("[100,90,85]", int[].class); System.out.println("marks:" + Arrays.toString(marks));
Example
Let s see Array seriapzation/de-seriapzation in action. Create a Java class file named GsonTester in C:>GSON_WORKSPACE.
File − GsonTester.java
import java.util.Arrays; import com.google.gson.Gson; pubpc class GsonTester { pubpc static void main(String args[]) { Gson gson = new Gson(); int[] marks = {100,90,85}; String[] names = {"Ram","Shyam","Mohan"}; //Seriapzation System.out.print("{"); System.out.print("marks:" + gson.toJson(marks) + ","); System.out.print("names:" + gson.toJson(names)); System.out.println("}"); //De-seriapzation marks = gson.fromJson("[100,90,85]", int[].class); names = gson.fromJson("["Ram","Shyam","Mohan"]", String[].class); System.out.println("marks:" + Arrays.toString(marks)); System.out.println("names:" + Arrays.toString(names)); } }
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.
{marks:[100,90,85],names:["Ram","Shyam","Mohan"]} marks:[100, 90, 85] names:[Ram, Shyam, Mohan]
Collections Example
List marks = new ArrayList(); //Seriapzation System.out.println("marks:" + gson.toJson(marks)); //De-seriapzation //get the type of the collection. Type pstType = new TypeToken<pst>(){}.getType(); //pass the type of collection marks = gson.fromJson("[100,90,85]", pstType); System.out.println("marks:" +marks);</pst>
Example
Let s see Collection seriapzation/de-seriapzation in action. Create a Java class file named GsonTester in C:>GSON_WORKSPACE.
File − GsonTester.java
import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collection; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; pubpc class GsonTester { pubpc static void main(String args[]) { Gson gson = new Gson(); Collection<Integer> marks = new ArrayList<Integer>(); marks.add(100); marks.add(90); marks.add(85); //Seriapzation System.out.print("{"); System.out.print("marks:" + gson.toJson(marks)); System.out.println("}"); //De-seriapzation Type pstType = new TypeToken<Collection<Integer>>(){}.getType(); marks = gson.fromJson("[100,90,85]", pstType); System.out.println("marks:" +marks); } }
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.
{marks:[100,90,85]} marks:[100, 90, 85]
Generics Example
Gson uses Java reflection API to get the type of the object to which a Json text is to be mapped. But with generics, this information is lost during seriapzation. To counter this problem, Gson provides a class com.google.gson.reflect.TypeToken to store the type of the generic object.
Example
Let s see Generics seriapzation/de-seriapzation in action. Create a Java class file named GsonTester in C:>GSON_WORKSPACE.
File − GsonTester.java
import java.lang.reflect.Type; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; pubpc class GsonTester { pubpc static void main(String args[]) { // create a shape class of type circle. Shape<Circle> shape = new Shape<Circle>(); // Create a Circle object Circle circle = new Circle(5.0); //assign circle to shape shape.setShape(circle); Gson gson = new Gson(); // Define a Type shapeType of type circle. Type shapeType = new TypeToken<Shape<Circle>>() {}.getType(); //Seriapze the json as ShapeType String jsonString = gson.toJson(shape, shapeType); System.out.println(jsonString); Shape shape1 = gson.fromJson(jsonString, Shape.class); System.out.println(shape1.get().getClass()); System.out.println(shape1.get().toString()); System.out.println(shape1.getArea()); Shape shape2 = gson.fromJson(jsonString, shapeType); System.out.println(shape2.get().getClass()); System.out.println(shape2.get().toString()); System.out.println(shape2.getArea()); } } class Shape <T> { pubpc T shape; pubpc void setShape(T shape) { this.shape = shape; } pubpc T get() { return shape; } pubpc double getArea() { if(shape instanceof Circle) { return ((Circle) shape).getArea(); } else { return 0.0; } } } class Circle { private double radius; pubpc Circle(double radius){ this.radius = radius; } pubpc String toString() { return "Circle"; } pubpc double getRadius() { return radius; } pubpc void setRadius(double radius) { this.radius = radius; } pubpc double getArea() { return (radius*radius*3.14); } }
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.
{"shape":{"radius":5.0}} class com.google.gson.internal.LinkedTreeMap {radius = 5.0} 0.0 class Circle Circle 78.5Advertisements