原文地址:http://my.oschina.net/orgsky/blog/368768

摘要 Gson过滤字段

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

最简单的用法

假设有下面这个类:

class MyObj {    public int x;  public int y;    public MyObj(int x, int y) {   this.x = x;   this.y = y;  }   }

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

@Test public void gson() {
MyObj obj = new MyObj(1, 2);
String json = new Gson().toJson(obj);
Assert.assertEquals("{\"x\":1,\"y\":2}", json);
}

方法1:排除transient字段

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

class MyObj {    public transient int x; // <---  public int y;    public MyObj(int x, int y) {   this.x = x;   this.y = y;  }   }
@Test public void gson() {
MyObj obj = new MyObj(1, 2);
String json = new Gson().toJson(obj);
Assert.assertEquals("{\"y\":2}", json); // <--- }

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

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

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

方法3:使用@Expose注解

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

class MyObj {    public int x;  @Expose public int y; // <---    public MyObj(int x, int y) {   this.x = x;   this.y = y;  }   }
@Test public void gson() {  Gson gson = new GsonBuilder()   .excludeFieldsWithoutExposeAnnotation() // <---   .create();    MyObj obj = new MyObj(1, 2);  String json = gson.toJson(obj);  Assert.assertEquals("{\"y\":2}", json);
}

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

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

class MyObj {    public int _x; // <---  public int y;    public MyObj(int x, int y) {   this._x = x;   this.y = y;  }   }
@Test public void gson() {  ExclusionStrategy myExclusionStrategy = new ExclusionStrategy() {   @Override   public boolean shouldSkipField(FieldAttributes fa) {   return fa.getName().startsWith("_");   }   @Override   public boolean shouldSkipClass(Class<?> clazz) {   return false;   }    };    Gson gson = new GsonBuilder()   .setExclusionStrategies(myExclusionStrategy) // <---   .create();    MyObj obj = new MyObj(1, 2);  String json = gson.toJson(obj);  Assert.assertEquals("{\"y\":2}", json);
}

最新文章

  1. 写给程序员和UI--Android的切图标准
  2. JVM 1.类的加载、连接、初始化
  3. jQuery tab plugin
  4. oracle nvl()函数在使用中出现的问题
  5. hdu_5585_Less Time, More profit(二分+最大权闭合图)
  6. 使用 Composer 安装 Laravel 框架
  7. iOS学习之应用之间的操作(转发)
  8. python怎么实现数组增加一行或多行
  9. FFmpeg源代码简单分析:av_find_decoder()和av_find_encoder()
  10. 树dp:边覆盖,点覆盖
  11. 马拉车算法,mannacher查找最长回文子串
  12. Scrapy实战篇(九)之爬取链家网天津租房数据
  13. (网页)websocket后台调用Service层
  14. 【Zend Studio】在Zend Studio中调试ThinkPhp框架
  15. 【sql基础】按照名字分组查询时间最早的一条记录
  16. Apache Commons Digester 三(规则注解)
  17. GitHub使用笔记1:git客户端配置多ssh key
  18. 对Linux内核中进程上下文和中断上下文的理解
  19. rdlc报表函数
  20. 解决:ubuntu 里文件夹带锁

热门文章

  1. android Gui系统之SurfaceFlinger(5)---Vsync(2)
  2. 模仿password输入框
  3. 如何正确并完全安装Visual Studio 2015企业版本?
  4. jQuery简单入门(四)
  5. SQL Server调优系列基础篇(联合运算符总结)
  6. 手写一个json格式化 api
  7. pl/sql developer——instant-client 简单配置
  8. python基础(一)
  9. Python时间处理之time模块
  10. docker-6 管理工具