SerializeFilter是通过编程扩展的方式定制序列化。Fastjson 支持6种 SerializeFilter,用于不同场景的定制序列化。
PropertyPreFilter:根据 PropertyName 判断是否序列化
PropertyFilter:根据 PropertyName 和 PropertyValue 来判断是否序列化
NameFilter:修改 Key,如果需要修改 Key,process 返回值则可
ValueFilter:修改 Value
BeforeFilter:序列化时在最前添加内容
AfterFilter:序列化时在最后添加内容
1. 需求
JSON 数据格式如下,需要过滤掉其中 "book" 的 "price" 属性。
JSON数据格式:
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
2. SimplePropertyPreFilter 过滤器
该过滤器由 Fastjson 提供,代码实现:
String json = "{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}";
SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
filter.getExcludes().add("price");
JSONObject jsonObject = JSON.parseObject(json);
String str = JSON.toJSONString(jsonObject, filter);
System.out.println(str);
运行结果:
{
"store": {
"bicycle": {
"color": "red"
},
"book": [
{
"author": "Nigel Rees",
"category": "reference",
"title": "Sayings of the Century"
},
{
"author": "Evelyn Waugh",
"category": "fiction",
"title": "Sword of Honour"
}
]
},
"expensive": 10
}
查看 JSON 数据的过滤结果,发现 "bicycle" 中的 "price" 属性也被过滤掉了,不符合需求。
3. LevelPropertyPreFilter 过滤器
该自定义过滤器实现 PropertyPreFilter 接口,实现根据层级过滤 JSON 数据中的属性。
扩展类:
/**
* 层级属性删除
*
* @author yinjianwei
* @date 2017年8月24日 下午3:55:19
*
*/
public class LevelPropertyPreFilter implements PropertyPreFilter {
private final Class> clazz;
private final Set includes = new HashSet();
private final Set excludes = new HashSet();
private int maxLevel = 0;
public LevelPropertyPreFilter(String... properties) {
this(null, properties);
}
public LevelPropertyPreFilter(Class> clazz, String... properties) {
super();
this.clazz = clazz;
for (String item : properties) {
if (item != null) {
this.includes.add(item);
}
}
}
public LevelPropertyPreFilter addExcludes(String... filters) {
for (int i = 0; i < filters.length; i++) {
this.getExcludes().add(filters[i]);
}
return this;
}
public LevelPropertyPreFilter addIncludes(String... filters) {
for (int i = 0; i < filters.length; i++) {
this.getIncludes().add(filters[i]);
}
return this;
}
public boolean apply(JSONSerializer serializer, Object source, String name) {
if (source == null) {
return true;
}
if (clazz != null && !clazz.isInstance(source)) {
return true;
}
// 过滤带层级属性(store.book.price)
SerialContext serialContext = serializer.getContext();
String levelName = serialContext.toString();
levelName = levelName + "." + name;
levelName = levelName.replace("$.", "");
levelName = levelName.replaceAll("\\[\\d+\\]", "");
if (this.excludes.contains(levelName)) {
return false;
}
if (maxLevel > 0) {
int level = 0;
SerialContext context = serializer.getContext();
while (context != null) {
level++;
if (level > maxLevel) {
return false;
}
context = context.parent;
}
}
if (includes.size() == 0 || includes.contains(name)) {
return true;
}
return false;
}
public int getMaxLevel() {
return maxLevel;
}
public void setMaxLevel(int maxLevel) {
this.maxLevel = maxLevel;
}
public Class> getClazz() {
return clazz;
}
public Set getIncludes() {
return includes;
}
public Set getExcludes() {
return excludes;
}
}
代码实现:
public static void main(String[] args) {
String json = "{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}";
JSONObject jsonObj = JSON.parseObject(json);
LevelPropertyPreFilter propertyPreFilter = new LevelPropertyPreFilter();
propertyPreFilter.addExcludes("store.book.price");
String json2 = JSON.toJSONString(jsonObj, propertyPreFilter);
System.out.println(json2);
}
运行结果:
{
"store": {
"bicycle": {
"color": "red",
"price": 19.95
},
"book": [
{
"author": "Nigel Rees",
"category": "reference",
"title": "Sayings of the Century"
},
{
"author": "Evelyn Waugh",
"category": "fiction",
"title": "Sword of Honour"
}
]
},
"expensive": 10
}
查看 JSON 数据的过滤结果,实现了上面的需求。
参考:http://www.cnblogs.com/dirgo/...