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