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

更多内容,可以参考这个文章:
[java]   view plain  copy
  1. //实现PropertyFilter中的apply方法  
[java]  view plain  copy

  1. public class ComplexPropertyPreFilter implements PropertyFilter {  
  2.     private Map<Class<?>, Set<String>> includeMap = new HashMap<Class<?>, Set<String>>();  
  3.     //@Override  
  4.     public boolean apply(Object source, String name, Object value) {  
  5.         for(Entry<Class<?>, Set<String>> entry : includeMap.entrySet()) {  
  6.             Class<?> class1 = entry.getKey();  
  7.             if(source.getClass() == class1){  
  8.                 Set<String> fields = entry.getValue();  
  9.                 for(String field : fields) {  
  10.                     if(field.equals(name)){  
  11.                         return false;  
  12.                     }  
  13.                 }  
  14.             }  
  15.         }  
  16.         return true;  
  17.     }  
  18.       
  19.     public ComplexPropertyPreFilter(Map<Class<?>, Set<String>> includeMap){  
  20.         this.includeMap = includeMap;  
  21.     }  
  22. }  


测试类:

[java]  view plain  copy
  1. class Wheel{  
  2.     String name;  
  3.     int size;  
  4.     String color;  
  5.     public String getName() {  
  6.         return name;  
  7.     }  
  8.     public void setName(String name) {  
  9.         this.name = name;  
  10.     }  
  11.     public int getSize() {  
  12.         return size;  
  13.     }  
  14.     public void setSize(int size) {  
  15.         this.size = size;  
  16.     }  
  17.     public String getColor() {  
  18.         return color;  
  19.     }  
  20.     public void setColor(String color) {  
  21.         this.color = color;  
  22.     }  
  23.     public Wheel() {  
  24.         super();  
  25.         this.color="black";  
  26.         this.name="miqilin";  
  27.         this.size = 17;  
  28.     }  
  29.       
  30.       
  31. }  
  32. class Sofa{  
  33.     String color;  
  34.     int count;  
  35.     String texture;  
  36.     public String getColor() {  
  37.         return color;  
  38.     }  
  39.     public void setColor(String color) {  
  40.         this.color = color;  
  41.     }  
  42.     public int getCount() {  
  43.         return count;  
  44.     }  
  45.     public void setCount(int count) {  
  46.         this.count = count;  
  47.     }  
  48.     public String getTexture() {  
  49.         return texture;  
  50.     }  
  51.     public void setTexture(String texture) {  
  52.         this.texture = texture;  
  53.     }  
  54.     public Sofa() {  
  55.         super();  
  56.         this.color = "white";  
  57.         this.count = 4;  
  58.         this.texture = "fur";  
  59.     }  
  60.       
  61.       
  62. }  
  63. class Car {  
  64.     private Wheel wheel;  
  65.     private Sofa sofa;  
  66.     public Wheel getWheel() {  
  67.         return wheel;  
  68.     }  
  69.     public void setWheel(Wheel wheel) {  
  70.         this.wheel = wheel;  
  71.     }  
  72.     public Sofa getSofa() {  
  73.         return sofa;  
  74.     }  
  75.     public void setSofa(Sofa sofa) {  
  76.         this.sofa = sofa;  
  77.     }  
  78.     public Car() {  
  79.         super();  
  80.         this.wheel = new Wheel();  
  81.         this.sofa = new Sofa();  
  82.     }  
  83.       
  84.       
  85. }  
  86. public class Test {  
  87.   
  88.     public static void main(String args[]){  
  89.         Map<String,Car> map = new HashMap<String, Car>();  
  90.         map.put("car1"new Car());  
  91.         map.put("car2"new Car());  
  92.         //需要过滤的类 + 属性  
  93.         Map<Class<?>, Set<String>> includeMap = new HashMap<Class<?>, Set<String>>();  
  94.         Set<String> set = new HashSet<String>();  
  95.         set.add("color");  
  96.         includeMap.put(Wheel.class, set);  
  97.         set = new HashSet<String>();  
  98.         set.add("texture");  
  99.         includeMap.put(Sofa.class, set);  
  100.           
  101.           
  102.         ComplexPropertyPreFilter cfilter = new ComplexPropertyPreFilter(includeMap);  
  103.         SerializeWriter sw = new SerializeWriter();  
  104.         JSONSerializer serializer = new JSONSerializer(sw);   
  105.         serializer.getPropertyFilters().add(cfilter);  
  106.         serializer.write(map);    
  107.           
  108.         System.out.println(sw.toString());  
  109.     }  
  110.   
  111. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值