Gson 序列化示例
gson 序列化示例
在本章中,我们将讨论数组,集合和泛型的序列化/反序列化。
1. 数组示例
int[] marks = {100,90,85}; //serialization system.out.println("marks:" + gson.tojson(marks)); //de-serialization marks = gson.fromjson("[100,90,85]", int[].class); system.out.println("marks:" + arrays.tostring(marks));
1) 范例
让我们看看阵列序列化/反序列化的实际应用。在c:> gson_workspace中创建名为 gsontester 的java类文件。
文件:gsontester.java
import java.util.arrays; import com.google.gson.gson; public class gsontester { public static void main(string args[]) { gson gson = new gson(); int[] marks = {100,90,85}; string[] names = {"ram","shyam","mohan"}; //serialization system.out.print("{"); system.out.print("marks:" + gson.tojson(marks) + ","); system.out.print("names:" + gson.tojson(names)); system.out.println("}"); //de-serialization 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)); } }
2) 验证结果
使用 javac 编译器编译类如下:
c:\gson_workspace>javac gsontester.java
现在运行gsontester查看结果:
c:\gson_workspace>java gsontester
验证输出。
{marks:[100,90,85],names:["ram","shyam","mohan"]} marks:[100, 90, 85] names:[ram, shyam, mohan]
2. 集合示例
list marks = new arraylist(); //serialization system.out.println("marks:" + gson.tojson(marks)); //de-serialization //get the type of the collection. type listtype = new typetoken<list>(){}.gettype(); //pass the type of collection marks = gson.fromjson("[100,90,85]", listtype); system.out.println("marks:" +marks);</list>
1) 范例
让我们看一下collection序列化/反序列化的实际应用。在c:> gson_workspace中创建名为 gsontester 的java类文件。
文件: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; public class gsontester { public static void main(string args[]) { gson gson = new gson(); collection<integer> marks = new arraylist<integer>(); marks.add(100); marks.add(90); marks.add(85); //serialization system.out.print("{"); system.out.print("marks:" + gson.tojson(marks)); system.out.println("}"); //de-serialization type listtype = new typetoken<collection<integer>>(){}.gettype(); marks = gson.fromjson("[100,90,85]", listtype); system.out.println("marks:" +marks); } }
2) 验证结果
使用 javac 编译器编译类如下 -
c:\gson_workspace>javac gsontester.java
现在运行 gsontester 查看结果 -
c:\gson_workspace>java gsontester
验证输出。
{marks:[100,90,85]} marks:[100, 90, 85]
3. 泛型示例
gson使用java反射api来获取要映射json文本的对象的类型。但是对于泛型,这些信息在序列化过程中会丢失。为了解决这个问题,gson提供了一个 com.google.gson.reflect.typetoken 类来存储通用对象的类型。
1) 范例
让我们看一下generics序列化/反序列化的实际应用。在c:> gson_workspace中创建名为 gsontester 的java类文件。
文件:gsontester.java
import java.lang.reflect.type; import com.google.gson.gson; import com.google.gson.reflect.typetoken; public class gsontester { public 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(); //serialize 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> { public t shape; public void setshape(t shape) { this.shape = shape; } public t get() { return shape; } public double getarea() { if(shape instanceof circle) { return ((circle) shape).getarea(); } else { return 0.0; } } } class circle { private double radius; public circle(double radius){ this.radius = radius; } public string tostring() { return "circle"; } public double getradius() { return radius; } public void setradius(double radius) { this.radius = radius; } public double getarea() { return (radius*radius*3.14); } }
2) 验证结果
使用 javac 编译器编译类如下:
c:\gson_workspace>javac gsontester.java
现在运行 gsontester 查看结果:
c:\gson_workspace>java gsontester
验证输出。
{"shape":{"radius":5.0}} class com.google.gson.internal.linkedtreemap {radius = 5.0} 0.0 class circle circle 78.5