zoukankan      html  css  js  c++  java
  • GSON使用笔记(1) -- 序列化时排除字段的几种方式

    http://blog.csdn.net/zxhoo/article/details/21471005

    GSON是Google发布的JSON序列化/反序列化工具,非常容易使用。本文简要讨论在使用GSON将Java对象转成JSON时,如何排除某些字段。

    最简单的用法

    假设有下面这个类:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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. }  


    最简单的GSON用法如下所示:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. @Test  
    2. public void gson() {  
    3.     MyObj obj = new MyObj(1, 2);  
    4.     String json = new Gson().toJson(obj);  
    5.     Assert.assertEquals("{"x":1,"y":2}", json);  
    6. }  

    方法1:排除null字段

    null字段,默认就不会序列化的,如下所示:

    [java] view plain copy
     
    1. class MyObj {  
    2.       
    3.     private int intField;  
    4.     private String strField;  
    5.       
    6. }  
    [java] view plain copy
     
    1. @Test  
    2. public void gson() {  
    3.     MyObj obj = new MyObj();  
    4.     Assert.assertEquals("{"intField":0}", new Gson().toJson(obj));  
    5. }  

    要想序列化null字段,需要显示的进行设置:

    [java] view plain copy
     
    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字段

    这个方法最简单,给字段加上transient修饰符就可以了,如下所示:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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. }  
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. @Test  
    2. public void gson() {  
    3.     MyObj obj = new MyObj(1, 2);  
    4.     String json = new Gson().toJson(obj);  
    5.     Assert.assertEquals("{"y":2}", json); // <---  
    6. }  

    方法3:排除Modifier为指定类型的字段

    这个方法需要用GsonBuilder定制一个GSON实例,如下所示:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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. }  
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. @Test  
    2. public void gson() {  
    3.     Gson gson = new GsonBuilder()  
    4.             .excludeFieldsWithModifiers(Modifier.PROTECTED) // <---  
    5.             .create();  
    6.       
    7.     MyObj obj = new MyObj(1, 2);  
    8.     String json = gson.toJson(obj); // <---  
    9.     Assert.assertEquals("{"y":2}", json);  
    10. }  

    方法4:使用@Expose注解

    注意,没有被@Expose标注的字段会被排除,如下所示:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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. }  
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. @Test  
    2. public void gson() {  
    3.     Gson gson = new GsonBuilder()  
    4.             .excludeFieldsWithoutExposeAnnotation() // <---  
    5.             .create();  
    6.       
    7.     MyObj obj = new MyObj(1, 2);  
    8.     String json = gson.toJson(obj);  
    9.     Assert.assertEquals("{"y":2}", json);  
    10. }  

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

    这种方式最灵活,下面的例子把所有以下划线开头的字段全部都排除掉:

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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. }  
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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(1, 2);  
    22.     String json = gson.toJson(obj);  
    23.     Assert.assertEquals("{"y":2}", json);  
    24. }  



  • 相关阅读:
    H5项目开发分享——用Canvas合成文字
    《JavaScript设计模式 张》整理
    Linux常用指令指南,终端装逼利器
    飞起来的正则表达式
    JavaScript特性(attribute)、属性(property)和样式(style)
    使用 Nginx 提升网站访问速度
    centos 邮件服务 腾讯企业邮箱(免费) 使用iRedmail 需要有公网的centos主机 发邮件协议:smtp 端口25 收邮件协议:pop3 端口110 iredmail安装配置 使用邮箱系统 第三十一节课
    用nginx的反向代理机制解决前端跨域问题在nginx上部署web静态页面
    mysql字符集调整总结
    因为smb和nfs挂掉导致客户端开机启动不了
  • 原文地址:https://www.cnblogs.com/bigben0123/p/5525900.html
Copyright © 2011-2022 走看看