fastjson 多级联属性过滤

来源:互联网 发布:c语言转义字符怎么用 编辑:程序博客网 时间:2024/05/16 12:02

最近使用FastJson结合hibernate做项目,发现关于对象的级联属性的过滤上用的不是很顺。当然简单的属性过滤 @温少 已经提供了 SimplePropertyPreFilter 使用,使用方法有详细说明的。这里我针对级联属性的过滤对该类做了补充。(当然你也可以使用注解实现)

代码如下:

 

  1 package com.example.util.fastjson;  2         3 import java.util.Date;  4 import java.util.HashMap;  5 import java.util.Map;  6         7 import com.alibaba.fastjson.JSON;  8 import com.alibaba.fastjson.serializer.JSONSerializer;  9 import com.alibaba.fastjson.serializer.PropertyPreFilter; 10 import com.alibaba.fastjson.serializer.SerializerFeature; 11 import com.suncompass.example.auth.entity.AuthEmployee; 12 import com.suncompass.example.auth.entity.AuthMenu; 13 import com.suncompass.framework.base.entity.BaseEntity; 14        15 /** 16  * @author :晨风²º¹³ <br> 17  * @Comment : fastjson 针对类型的属性选择过滤器(可以跨层级) <br> 18  */ 19 public class ComplexPropertyPreFilter implements PropertyPreFilter { 20            21     private Map<Class<?>, String[]> includes = new HashMap<>(); 22     private Map<Class<?>, String[]> excludes = new HashMap<>(); 23            24     static { 25         JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.DisableCircularReferenceDetect.getMask(); 26     } 27            28     public ComplexPropertyPreFilter() { 29                30     } 31            32     public ComplexPropertyPreFilter(Map<Class<?>, String[]> includes) { 33         super(); 34         this.includes = includes; 35     } 36            37     public boolean apply(JSONSerializer serializer, Object source, String name) { 38                39         //对象为空。直接放行 40         if (source == null) { 41             return true; 42         } 43                44         // 获取当前需要序列化的对象的类对象 45         Class<?> clazz = source.getClass(); 46                47         // 无需序列的对象、寻找需要过滤的对象,可以提高查找层级 48         // 找到不需要的序列化的类型 49         for (Map.Entry<Class<?>, String[]> item : this.excludes.entrySet()) { 50             // isAssignableFrom(),用来判断类型间是否有继承关系 51             if (item.getKey().isAssignableFrom(clazz)) { 52                 String[] strs = item.getValue(); 53                        54                 // 该类型下 此 name 值无需序列化 55                 if (isHave(strs, name)) { 56                     return false; 57                 } 58             } 59         } 60                61         // 需要序列的对象集合为空 表示 全部需要序列化 62         if (this.includes.isEmpty()) { 63             return true; 64         } 65                66         // 需要序列的对象 67         // 找到不需要的序列化的类型 68         for (Map.Entry<Class<?>, String[]> item : this.includes.entrySet()) { 69             // isAssignableFrom(),用来判断类型间是否有继承关系 70             if (item.getKey().isAssignableFrom(clazz)) { 71                 String[] strs = item.getValue(); 72                 // 该类型下 此 name 值无需序列化 73                 if (isHave(strs, name)) { 74                     return true; 75                 } 76             } 77         } 78                79         return false; 80     } 81            82     /* 83      * 此方法有两个参数,第一个是要查找的字符串数组,第二个是要查找的字符或字符串 84      */ 85     public static boolean isHave(String[] strs, String s) { 86                87         for (int i = 0; i < strs.length; i++) { 88             // 循环查找字符串数组中的每个字符串中是否包含所有查找的内容 89             if (strs[i].equals(s)) { 90                 // 查找到了就返回真,不在继续查询 91                 return true; 92             } 93         } 94                95         // 没找到返回false 96         return false; 97     } 98            99     public Map<Class<?>, String[]> getIncludes() {100         return includes;101     }102           103     public void setIncludes(Map<Class<?>, String[]> includes) {104         this.includes = includes;105     }106           107     public Map<Class<?>, String[]> getExcludes() {108         return excludes;109     }110           111     public void setExcludes(Map<Class<?>, String[]> excludes) {112         this.excludes = excludes;113     }114           115144 }

测试代码:


 1 package com.example.auth.test.fastjson; 2        3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6        7 import com.alibaba.fastjson.JSON; 8 import com.example.util.fastjson.ComplexPropertyPreFilter; 9       10 public class A {11           12     private Integer aid;13           14     private B b;15           16     private List<C> c = new ArrayList<>();17           18     public A() {19         super();20     }21           22     public static void main(String[] args) {23         A a = new A();24         a.setAid(1);25               26         B b = new B();27         b.setBid(2);28               29         a.setB(b);30         b.setA(a);31               32         C c = new C();33         c.setId(3);34               35         a.getC().add(c);36         b.getC().add(c);37         c.setA(a);38         c.setB(b);39               40         ComplexPropertyPreFilter filter = new ComplexPropertyPreFilter();41               42         filter.setExcludes(new HashMap<Class<?>, String[]>() {43                   44             private static final long serialVersionUID = -8411128674046835592L;45                   46             {47                 put(A.class, new String[] { "aid" });48                 put(B.class, new String[] { "bid", "a" });49                 put(C.class, new String[] { "a", "b" });50             }51         });52                   53         System.out.println(JSON.toJSONString(a, filter));54               55     }56           57     public Integer getAid() {58         return aid;59     }60           61     public void setAid(Integer aid) {62         this.aid = aid;63     }  public B getB() {64         return b;65     }66           67     public void setB(B b) {68         this.b = b;69     }70           71     public List<C> getC() {72         return c;73     }74           75     public void setC(List<C> c) {76         this.c = c;77     }78           79 }

 1 package com.example.auth.test.fastjson; 2        3 import java.util.ArrayList; 4 import java.util.List; 5        6 public class B { 7            8     private Integer bid; 9           10     private A a;11           12     private List<C> c = new ArrayList<>();13           14     public B() {15         super();16     }17           18     public Integer getBid() {19         return bid;20     }21           22     public void setBid(Integer bid) {23         this.bid = bid;24     }25           26     public A getA() {27         return a;28     }29           30     public void setA(A a) {31         this.a = a;32     }33           34     public List<C> getC() {35         return c;36     }37           38     public void setC(List<C> c) {39         this.c = c;40     }41           42 }

 1 package com.example.auth.test.fastjson; 2        3 public class C { 4            5     private Integer id; 6            7     private A a; 8            9     private B b;10           11     public C() {12         super();13     }14           15     public A getA() {16         return a;17     }18           19     public void setA(A a) {20         this.a = a;21     }22           23     public B getB() {24         return b;25     }26           27     public void setB(B b) {28         this.b = b;29     }30           31     public Integer getId() {32         return id;33     }34           35     public void setId(Integer id) {36         this.id = id;37     }38           39 }
原创粉丝点击