您的位置:首页 > Web前端 > JavaScript

fastjson过滤属性,重点在于PropertyFilter 这个东西(应用场景,hibernate懒加载过滤不要的属性)

2016-05-28 15:10 579 查看
更多内容,可以参考这个文章:

http://www.360doc.com/content/12/0319/15/1542811_195663419.shtml

[java] view
plain copy

//实现PropertyFilter中的apply方法  

[java] view
plain copy

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;  

    }  

}  

测试类:

[java] view
plain copy

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());  

    }  

  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: