fastjson过滤属性

//实现PropertyFilter中的apply方法
public class ComplexPropertyPreFilter implements PropertyFilter {
	private Map<Class<?>, Set<String>> includeMap = new HashMap<Class<?>, Set<String>>();
	//@Override
	public boolean apply(Object source, String name, Object value) {
		for(Entry<Class<?>, Set<String>> entry : includeMap.entrySet()) {
			Class<?> class1 = entry.getKey();
			if(source.getClass() == class1){
				Set<String> fields = entry.getValue();
				for(String field : fields) {
					if(field.equals(name)){
						return false;
					}
				}
			}
		}
		return true;
	}
	
	public ComplexPropertyPreFilter(Map<Class<?>, Set<String>> includeMap){
		this.includeMap = includeMap;
	}
}


测试类:

class Wheel{
	String name;
	int size;
	String color;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public Wheel() {
		super();
		this.color="black";
		this.name="miqilin";
		this.size = 17;
	}
	
	
}
class Sofa{
	String color;
	int count;
	String texture;
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public String getTexture() {
		return texture;
	}
	public void setTexture(String texture) {
		this.texture = texture;
	}
	public Sofa() {
		super();
		this.color = "white";
		this.count = 4;
		this.texture = "fur";
	}
	
	
}
class Car {
	private Wheel wheel;
	private Sofa sofa;
	public Wheel getWheel() {
		return wheel;
	}
	public void setWheel(Wheel wheel) {
		this.wheel = wheel;
	}
	public Sofa getSofa() {
		return sofa;
	}
	public void setSofa(Sofa sofa) {
		this.sofa = sofa;
	}
	public Car() {
		super();
		this.wheel = new Wheel();
		this.sofa = new Sofa();
	}
	
	
}
public class Test {

	public static void main(String args[]){
		Map<String,Car> map = new HashMap<String, Car>();
		map.put("car1", new Car());
		map.put("car2", new Car());
		//需要过滤的类 + 属性
		Map<Class<?>, Set<String>> includeMap = new HashMap<Class<?>, Set<String>>();
		Set<String> set = new HashSet<String>();
		set.add("color");
		includeMap.put(Wheel.class, set);
		set = new HashSet<String>();
		set.add("texture");
		includeMap.put(Sofa.class, set);
		
		
		ComplexPropertyPreFilter cfilter = new ComplexPropertyPreFilter(includeMap);
		SerializeWriter sw = new SerializeWriter();
		JSONSerializer serializer = new JSONSerializer(sw); 
		serializer.getPropertyFilters().add(cfilter);
		serializer.write(map);  
		
		System.out.println(sw.toString());
	}

}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值