日期:2014-05-16  浏览次数:20319 次

google Gson学习笔记及与json-lib的比较
因为gson网上的帮助文档打开时比较慢,所以把帮助文档摘录如此,方便查看:

1. 基本类型转化

public static void main(String[] args) {   
        Gson gson = new Gson();   
        System.out.println(gson.toJson(1)); // ==> prints 1   
        System.out.println(gson.toJson("abcd"));// ==> prints "abcd"   
        System.out.println(gson.toJson(new Long(10)));// ==> prints 10   
        int[] values = { 1 };   
        System.out.println(gson.toJson(values));// ==> prints [1]   
        System.out.println("============");   
  
        int one = gson.fromJson("1", int.class);   
        Integer one1 = gson.fromJson("1", Integer.class);   
        Long one2 = gson.fromJson("1", Long.class);   
        String str = gson.fromJson("\"abc\"", String.class);   
        String anotherStr = gson.fromJson("[\"abc\"]", String.class);   
        int[] ints = gson.fromJson("[1,2,3,4,5]", int[].class);   
        Boolean b = gson.fromJson("false", Boolean.class);   
        System.out.println(b == false); //==> prints true   
    }  


2.对象转化
public class BagOfPrimitives {   
  
    private int           value1    = 1;   
    private String        value2    = "abc";   
//是用于声明变量在序列化的时候不被存储   
    private transient int   value3  = 3;   
  
    BagOfPrimitives() {   
        // no-args constructor   
    }   
  
    public static void main(String[] args) {   
        BagOfPrimitives obj = new BagOfPrimitives();   
        Gson gson = new Gson();   
        String json = gson.toJson(obj);     
        System.out.println(json); //==> json is {"value1":1,"value2":"abc"}   
           
        BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);    
        System.out.println(obj2.value1);   
        System.out.println(obj2.value2);   
        System.out.println(obj2.value3);//==>3   
           
        String json1 = "{'value1':1,'value2':'abc','value3':4}";   
        BagOfPrimitives obj3 = gson.fromJson(json1, BagOfPrimitives.class);    
        System.out.println(obj3.value1);   
        System.out.println(obj3.value2);   
        System.out.println(obj3.value3); //==>3   
    }   
}  


Note that you can not serialize objects with circular references since that will result in infinite recursion. 

如果要是用json lib的话,上面的代码可以写成如下所示:

String json1 = "{'value1':1,'value2':'abc','value3':4}";   
JSONObject jsonObj = JSONObject.fromObject( json1 );    
BagOfPrimitives obj3 = (BagOfPrimitives) JSONObject.toBean( jsonObj, BagOfPrimitives.class ); 


Finer Points with Objects

It is perfectly fine (and recommended) to use private fields

There is no need to use any annotations to indicate a field is to be included for serialization and deserialization. All fields in the current class (and from all super classes) are included by default.

If a field is marked transient, (by default) it is ignored and not included in the JSON serialization or deserialization.

This implementation handles nulls correctly
While serialization, a null field is skipped from the output
While deserialization, a missing entry in JSON results in setting the corresponding field in the object to null

If a field is synthetic , it is ignored and not included in JSON serialization or deserialization

Fields corresponding to the outer classes in  inner classes, anonymous classes, and local classes are ignored and not included in serialization or deserialization
Nested Classes (including Inner Classes)

Gson can serialize static nested classes quite easily.


Gson can also deserialize static nested classes. However, Gson can not automatically deserialize the pure inner classes since their no-args constructor also need a reference to the containing Object which