This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
使用SimplePropertyPreFilter过滤属性
wenshao edited this page May 9, 2016
·
4 revisions
需要根据不同的环境返回定制化返回属性时,可以使用SimplePropertyPreFilter。
SimplePropertyPreFilter的代码接口如下:
public class SimplePropertyPreFilter implements PropertyPreFilter {
public SimplePropertyPreFilter(String... properties){
this(null, properties);
}
public SimplePropertyPreFilter(Class<?> clazz, String... properties){
// ... ...
}
public Class<?> getClazz() {
return clazz;
}
public Set<String> getIncludes();
public Set<String> getExcludes();
/**
* @since 1.2.9
*/
public int getMaxLevel();
/**
* @since 1.2.9
*/
public void setMaxLevel(int maxLevel)
//...
}
你可以配置includes、excludes。当class不为null时,针对特定类型;当class为null时,针对所有类型。
当includes的size > 0时,属性必须在includes中才会被序列化,excludes优先于includes。
在1.1.23版本之后,JSON提供新的序列化接口toJSONString,如下:
String toJSONString(Object, SerializeFilter, SerializerFeature...);
使用方式如下:
VO vo = new VO();
vo.setId(123);
vo.setName("flym");
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(VO.class, "name");
Assert.assertEquals("{\"name\":\"flym\"}", JSON.toJSONString(vo, filter));
public static class VO {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
如果你需要在类级别配置Filter,可以看这里
如有需要修改本注脚,请联系阿里巴巴,
© Alibaba Fastjson Develop Team
注明: 版权所有阿里巴巴,请注明版权所有者
If you need to amend this footnote, please contact Alibaba.
© Alibaba Fastjson Develop Team
Note: Copyright Alibaba, please indicate the copyright owner