日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

GSON使用筆記(1)

 王慶峰 2015-06-19

GSON是Google發(fā)布的JSON序列化/反序列化工具,非常容易使用。本文簡(jiǎn)要討論在使用GSON將Java對(duì)象轉(zhuǎn)成JSON時(shí),如何排除某些字段。


最簡(jiǎn)單的用法

假設(shè)有下面這個(gè)類:

  1. class MyObj {  
  2.       
  3.     public int x;  
  4.     public int y;  
  5.       
  6.     public MyObj(int x, int y) {  
  7.         this.x = x;  
  8.         this.y = y;  
  9.     }  
  10.       
  11. }  

最簡(jiǎn)單的GSON用法如下所示:
  1. @Test  
  2. public void gson() {  
  3.     MyObj obj = new MyObj(12);  
  4.     String json = new Gson().toJson(obj);  
  5.     Assert.assertEquals("{\"x\":1,\"y\":2}", json);  
  6. }  

方法1:排除null字段

null字段,默認(rèn)就不會(huì)序列化的,如下所示:

  1. class MyObj {  
  2.       
  3.     private int intField;  
  4.     private String strField;  
  5.       
  6. }  
  1. @Test  
  2. public void gson() {  
  3.     MyObj obj = new MyObj();  
  4.     Assert.assertEquals("{\"intField\":0}"new Gson().toJson(obj));  
  5. }  
要想序列化null字段,需要顯示的進(jìn)行設(shè)置:
  1. @Test  
  2. public void serializeNulls() {  
  3.     MyObj obj = new MyObj();  
  4.     Gson gson = new GsonBuilder().serializeNulls().create();  
  5.     Assert.assertEquals("{\"intField\":0,\"strField\":null}", gson.toJson(obj));  
  6. }  

方法2:排除transient字段

這個(gè)方法最簡(jiǎn)單,給字段加上transient修飾符就可以了,如下所示:

  1. class MyObj {  
  2.       
  3.     public transient int x; // <---  
  4.     public int y;  
  5.       
  6.     public MyObj(int x, int y) {  
  7.         this.x = x;  
  8.         this.y = y;  
  9.     }  
  10.       
  11. }  
  1. @Test  
  2. public void gson() {  
  3.     MyObj obj = new MyObj(12);  
  4.     String json = new Gson().toJson(obj);  
  5.     Assert.assertEquals("{\"y\":2}", json); // <---  
  6. }  

方法3:排除Modifier為指定類型的字段

這個(gè)方法需要用GsonBuilder定制一個(gè)GSON實(shí)例,如下所示:

  1. class MyObj {  
  2.       
  3.     protected int x; // <---  
  4.     public int y;  
  5.       
  6.     public MyObj(int x, int y) {  
  7.         this.x = x;  
  8.         this.y = y;  
  9.     }  
  10.       
  11. }  
  1. @Test  
  2. public void gson() {  
  3.     Gson gson = new GsonBuilder()  
  4.             .excludeFieldsWithModifiers(Modifier.PROTECTED) // <---  
  5.             .create();  
  6.       
  7.     MyObj obj = new MyObj(12);  
  8.     String json = gson.toJson(obj); // <---  
  9.     Assert.assertEquals("{\"y\":2}", json);  
  10. }  

方法4:使用@Expose注解

注意,沒有被@Expose標(biāo)注的字段會(huì)被排除,如下所示:

  1. class MyObj {  
  2.       
  3.     public int x;  
  4.     @Expose public int y; // <---  
  5.       
  6.     public MyObj(int x, int y) {  
  7.         this.x = x;  
  8.         this.y = y;  
  9.     }  
  10.       
  11. }  
  1. @Test  
  2. public void gson() {  
  3.     Gson gson = new GsonBuilder()  
  4.             .excludeFieldsWithoutExposeAnnotation() // <---  
  5.             .create();  
  6.       
  7.     MyObj obj = new MyObj(12);  
  8.     String json = gson.toJson(obj);  
  9.     Assert.assertEquals("{\"y\":2}", json);  
  10. }  

方法5:使用ExclusionStrategy定制字段排除策略

這種方式最靈活,下面的例子把所有以下劃線開頭的字段全部都排除掉:

  1. class MyObj {  
  2.       
  3.     public int _x; // <---  
  4.     public int y;  
  5.       
  6.     public MyObj(int x, int y) {  
  7.         this._x = x;  
  8.         this.y = y;  
  9.     }  
  10.       
  11. }  
  1. @Test  
  2. public void gson() {  
  3.     ExclusionStrategy myExclusionStrategy = new ExclusionStrategy() {  
  4.   
  5.         @Override  
  6.         public boolean shouldSkipField(FieldAttributes fa) {  
  7.             return fa.getName().startsWith("_"); // <---  
  8.         }  
  9.   
  10.         @Override  
  11.         public boolean shouldSkipClass(Class<?> clazz) {  
  12.             return false;  
  13.         }  
  14.           
  15.     };  
  16.       
  17.     Gson gson = new GsonBuilder()  
  18.             .setExclusionStrategies(myExclusionStrategy) // <---  
  19.             .create();  
  20.       
  21.     MyObj obj = new MyObj(12);  
  22.     String json = gson.toJson(obj);  
  23.     Assert.assertEquals("{\"y\":2}", json);  
  24. }  





    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多