之前发表的文章涉及到JSONObject JSONArray属性过滤时,都是通过判断来实现的,那如果我们要在多处进行过滤的话,就会重复写很多代码,而且也不好修改。所以,这里我设计了一个注解annotation类,通过标注注解来实现JSONObject 和JSOnArray的属性过滤。
1、编写注解类
package com.renyiwei.wydns.json.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
*
* @author RenYiwei
*
*/
@Retention(RetentionPolicy.RUNTIME)
// 可以注解在字段,方法,类上
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.TYPE })
public @interface JSONPropertyFilter {
// 默认注解了就是过滤
boolean Value() default true;
}
2、编写AnnotationPropertyFilter实现PropertyFilter
package com.renyiwei.wydns.json;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import net.sf.json.util.PropertyFilter;
import com.renyiwei.wydns.json.annotation.JSONPropertyFilter;
/**
*
* @author RenYiwei
*
*/
public class AnnotationPropertyFilter implements PropertyFilter {
public boolean apply(Object object, String name, Object value) {
// 通过读取注解来决定是否序列化
try {
// 获取类上的注解
JSONPropertyFilter clazzAnnotation = object.getClass().getAnnotation(JSONPropertyFilter.class);
// 如果类上有注解,并且值为true,表示需要过滤
if (clazzAnnotation != null && clazzAnnotation.Value()) {
// 不要这个字段 返回true
return true;
}
// 获取字段上的注解
Field field = object.getClass().getDeclaredField(name);
JSONPropertyFilter fieldAnnotation = field.getAnnotation(JSONPropertyFilter.class);
// 如果字段上注解了,并且值为true,表示需要过滤
if (null != fieldAnnotation && fieldAnnotation.Value()) {
// 不要这个字段 返回true
return true;
}
// 通过属性描述器 获取属性get方法的注解
PropertyDescriptor pd = new PropertyDescriptor(field.getName(), object.getClass());
Method getMethod = pd.getReadMethod();
if (null != getMethod) {
JSONPropertyFilter methodAnnotation = getMethod.getAnnotation(JSONPropertyFilter.class);
// 如果get方法上注解了,并且值为true,表示需要过滤
if (null != fieldAnnotation && fieldAnnotation.Value()) {
// 不要这个字段 返回true
return true;
}
}
} catch (NoSuchFieldException e) {
return false;
} catch (SecurityException e) {
return false;
} catch (IntrospectionException e) {
return false;
}
return false;
}
}
3、在javaBean上面标注注解
package com.renyiwei.wydns.domain;
import java.sql.Timestamp;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.xml.bind.annotation.XmlTransient;
import com.renyiwei.wydns.json.annotation.JSONPropertyFilter;
public class Product {
private Long id;
// 在产品系统的产品编号
private Long productid;
// 在订单系统的产品编号
private Long orderpid;
private Long orderid;
private String name;
private String type;
private String extension;
// 在这里标注
@JSONPropertyFilter
private Server server;
// 创建时间
private String createtime;
// 到期时间
private String expdate;
// 购买时长
private String period;
// 在这里标注
@JSONPropertyFilter
private Client client;
private Timestamp ordertime;
// 省略get set方法
}
4、调用的时候传入AnnotationPropertyFilter对象
public String listAjax() {
List productList = productService.getAll();
Map returnMap = new HashMap();
JsonConfig config = new JsonConfig();
// 在这里传入AnnotationPropertyFilter对象
config.setJsonPropertyFilter(new AnnotationPropertyFilter());
returnMap.put("results", productList);
returnObj = JSONObject.fromObject(returnMap, config);
return SUCCESS;
}