fastjson 多級聯屬性過濾


最近使用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           
115     public static void main(String[] args) {
116         // use instanceOf,用來判斷對象是否是類的實例
117         // use isAssignableFrom(),用來判斷類型間是否有繼承關系
118         // use isInstance(),用來判斷對象是否是類的實例
119               
120         Class<?> intClass = Integer.class;
121               
122         // Create various objects.
123         String str = "Hello";
124         Date date = new Date();
125         Integer i = new Integer(10);
126               
127         // Is str an instance of class Integer?
128         boolean check1 = intClass.isInstance(str);
129         System.out.println("str is an Integer? " + check1);
130               
131         // Is date an instance of class Integer?
132         boolean check2 = intClass.isInstance(date);
133         System.out.println("date is an Integer? " + check2);
134               
135         // Is i an instance of class Integer?
136         boolean check3 = intClass.isInstance(i);
137         System.out.println("i is an Integer? " + check3);
138               
139         System.out.println(BaseEntity.class.isInstance(new AuthEmployee()));
140         System.out.println(AuthEmployee.class.isInstance(new AuthMenu()));
141               
142         System.out.println(BaseEntity.class.isAssignableFrom(AuthEmployee.class));
143     }
144 }

測試代碼:

 

 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 }

參考 https://github.com/alibaba/fastjson/wiki/%E5%AE%9A%E5%88%B6%E5%BA%8F%E5%88%97%E5%8C%96 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM