json lib 属性过滤



import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Set;

import net.sf.json.util.PropertyFilter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* <p>Title: 忽略属性</p>
* <p>Description:忽略JAVABEAN的指定属性、是否忽略集合类属性</p>
*
*/
public class IgnoreFieldProcessorImpl implements PropertyFilter {

Log log = LogFactory.getLog(this.getClass());

/**
* 忽略的属性名称
*/
private String[] fields;

/**
* 是否忽略集合
*/
private boolean ignoreColl = false;

/**
* 空参构造方法<br/>
* 默认不忽略集合
*/
public IgnoreFieldProcessorImpl() {
// empty
}

/**
* 构造方法
* @param fields 忽略属性名称数组
*/
public IgnoreFieldProcessorImpl(String[] fields) {
this.fields = fields;
}

/**
* 构造方法
* @param ignoreColl 是否忽略集合
* @param fields 忽略属性名称数组
*/
public IgnoreFieldProcessorImpl(boolean ignoreColl, String[] fields) {
this.fields = fields;
this.ignoreColl = ignoreColl;
}

/**
* 构造方法
* @param ignoreColl 是否忽略集合
*/
public IgnoreFieldProcessorImpl(boolean ignoreColl) {
this.ignoreColl = ignoreColl;
}

public boolean apply(Object source, String name, Object value) {
Field declaredField = null;
//忽略值为null的属性
if(value == null)
return true;
//剔除自定义属性,获取属性声明类型
if(!"data".equals(name) && "data"!=name && !"totalSize".equals(name) && "totalSize"!=name ){
try {
declaredField = source.getClass().getDeclaredField(name);
} catch (NoSuchFieldException e) {
log.equals("没有找到属性" + name);
e.printStackTrace();
}
}
// 忽略集合
if (declaredField != null) {
if(ignoreColl) {
if(declaredField.getType() == Collection.class
|| declaredField.getType() == Set.class) {
return true;
}
}
}

// 忽略设定的属性
if(fields != null && fields.length > 0) {
if(juge(fields,name)) {
return true;
} else {
return false;
}
}

return false;
}
/**
* 过滤忽略的属性
* @param s
* @param s2
* @return
*/
public boolean juge(String[] s,String s2){
boolean b = false;
for(String sl : s){
if(s2.equals(sl)){
b=true;
}
}
return b;
}
public String[] getFields() {
return fields;
}

/**
* 设置忽略的属性
* @param fields
*/
public void setFields(String[] fields) {
this.fields = fields;
}

public boolean isIgnoreColl() {
return ignoreColl;
}

/**
* 设置是否忽略集合类
* @param ignoreColl
*/
public void setIgnoreColl(boolean ignoreColl) {
this.ignoreColl = ignoreColl;
}
}



JsonConfig config = new JsonConfig();
config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(true)); // 忽略掉集合对象

People people= session.get(People.class, 20);
JSONObject fromObject = JSONObject.fromObject(person, config );
response.getWriter().print(fromObject.toString());


JsonConfig config = new JsonConfig();
config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(true, new String[]{"name"})); // 忽略掉name属性及集合对象

People people= session.get(People.class, 20);
JSONObject fromObject = JSONObject.fromObject(person, config );
response.getWriter().print(fromObject.toString());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值