[转]Gson过滤字段
原文地址: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);
}
[转]Gson过滤字段的更多相关文章
- php过滤字段htmlentities,htmlspecialchars,strip_tags
1.strip_tags:过滤html标签比如<a> <html> <script> 如: $str = '<a href="test.html&q ...
- 使用dao时,如何同时使用动态表名和过滤字段?
使用dao时,如何同时使用动态表名和过滤字段? 发布于 630天前 作者 wukonggg 316 次浏览 复制 上一个帖子 下一个帖子 标签: 无 如题.求样例代码 1 回复 wend ...
- Fastjson 实体类JSON化过滤字段操作-PropertyFilter
过滤实体类中年龄等于5的字段 List<Users> models=new ArrayList<>(); for(int i=0;i<11;i++){ Users mod ...
- Spring MVC灵活控制返回json的值(自定义过滤字段)
在使用spring MVC开发过程中,为了提高项目执行效率,所以在一些外键字段的实体中会注解”@ManyToOne(fetch = FetchType.LAZY)”以实现延迟加载的效果. 但是,在使用 ...
- FastJson序列化时过滤字段(属性)的方法总结
FastJson序列化时(即转成JSON字符串时),可以过滤掉部分字段,或者只保留部分字段,方法有很多,下面举一些常用的方法. 方法一.FastJson的注解 @JSONField(serialize ...
- fastjson过滤字段
1.注解(字段上添加) @JSONField(serialize=false) 2.过滤器 PropertyFilter propertyFilter = new PropertyFilter() { ...
- 过滤字段中HTML标签
代码中只是过滤了一部分标签,例如span这些还是没有过滤,如果有更好办法的,可以帮忙补充 create FUNCTION [dbo].[CleanHTML] (@HTMLText VARCHAR(MA ...
- Gson解决字段为空是报错的问题
json解析有很多工具,这里说的是最常用也是解析速度最快的Gson,Gson是google家出的,有一个缺点就是无法设置null替换, 我们只能手动的批量替换服务器返回的null了,正常的接口定义的时 ...
- PHP数组去重..............过滤字段
$test_data = M('hot'); //实例化数据表 $data = $test_data->Distinct(true)->field('descriprion')->o ...
随机推荐
- JS实现悬浮移动窗口(悬浮广告)的特效
页面加载完成之后向页面插入窗口,之后向窗口插入关闭按钮,使用setInterval()函数触发moves()函数开始动画 js方法: 复制代码代码如下: <!DOCTYPE HTML PUB ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- 源码编译安装screen
OS:Amazon Linux AMI 2015.09.2 (HVM) #sudo su #wget http://ftp.gnu.org/gnu/screen/screen-4.3.1.tar.gz ...
- MVC数据库数据分页显示
首先从数据库获取数据 using System; using System.Collections.Generic; using System.Linq; using System.Web; usin ...
- composer 学习资料
今天看了一下composer,前几天开始用包依赖,以前一直都是自己手动配.今天用了composer,要学习的话可以按照以下链接学习: 1 官方文档: http://www.phpcomposer.co ...
- webapp设置适应pc和手机的页面宽高以及布局层叠图片文字
<!DOCTYPE html> <html lang="zh-cn"> <head> <title>我趣旅行网-美剧迷</ti ...
- openstack新建虚机、网络、路由时候对应的ovs网桥的变化
[root@wb5 ~]# [root@wb5 ~]# [root@wb5 ~]# [root@wb5 ~]# [root@wb5 ~]# [root@wb5 ~]# [root@wb5 ~]# [r ...
- u盘安装CENTOS后,启动missing operating system ,只能用U盘才能启动系统
好久之前就想把家里闲置的那台老的不能再老的笔记本换成linux的,用来学习 从N久之前用光盘安装的时候发现光驱坏掉了之后就没有再装过,最近又想安装于是就试了U盘安装 U盘安装过程也很简单,只需要制作一 ...
- SQLServer基本函数
1.字符串函数 长度与分析用 datalength(Char_expr) 返回字符串包含字符数,但不包含后面的空格 substring(expression,start,length) 取子串 ri ...
- apache指定的网络名不再可用
如果Apache的error.log还是出现大量的:Sat Dec 24 17:21:28 2006] [warn] (OS 64)指定的网络名不再可 用. : winnt_accept: Async ...