GSON使用笔记(1) -- 序列化时排除字段的几种方式
http://blog.csdn.net/zxhoo/article/details/21471005
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:排除null字段
null字段,默认就不会序列化的,如下所示:
- class MyObj {
- private int intField;
- private String strField;
- }
- @Test
- public void gson() {
- MyObj obj = new MyObj();
- Assert.assertEquals("{\"intField\":0}", new Gson().toJson(obj));
- }
要想序列化null字段,需要显示的进行设置:
- @Test
- public void serializeNulls() {
- MyObj obj = new MyObj();
- Gson gson = new GsonBuilder().serializeNulls().create();
- Assert.assertEquals("{\"intField\":0,\"strField\":null}", gson.toJson(obj));
- }
方法2:排除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); // <---
- }
方法3:排除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);
- }
方法4:使用@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);
- }
方法5:使用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);
- }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2015-05-25 通过HTTP协议实现多线程下载