zoukankan      html  css  js  c++  java
  • FastJson前置属性过滤器

    FastJson前置属性过滤器

      1 /**
      2  * <html>
      3  * <body>
      4  *  <P> Copyright 1994 JsonInternational</p>
      5  *  <p> All rights reserved.</p>
      6  *  <p> Created on 19941115</p>
      7  *  <p> Created by Jason</p>
      8  *  </body>
      9  * </html>
     10  */
     11 package cn.ucaner.alpaca.framework.utils.properties;
     12 
     13 import java.util.HashMap;
     14 import java.util.Map;
     15 
     16 import com.alibaba.fastjson.JSON;
     17 import com.alibaba.fastjson.serializer.JSONSerializer;
     18 import com.alibaba.fastjson.serializer.PropertyPreFilter;
     19 import com.alibaba.fastjson.serializer.SerializerFeature;
     20 
     21 /**
     22 * @Package:cn.ucaner.common.utils   
     23 * @ClassName:ComplexPropertyPreUtils   
     24 * @Description:  <p> 前置属性过滤器</p>
     25 * @Author: - Jason 
     26 * @CreatTime:2017年10月24日 上午10:49:03   
     27 * @Modify By:   
     28 * @ModifyTime:  
     29 * @Modify marker:   
     30 * @version    V1.0
     31  */
     32 public class ComplexPropertyPreUtils implements PropertyPreFilter {
     33 
     34     private Map<Class<?>, String[]> includes = new HashMap<>();
     35     private Map<Class<?>, String[]> excludes = new HashMap<>();
     36 
     37     static {
     38         JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.DisableCircularReferenceDetect.getMask();
     39     }
     40 
     41     public ComplexPropertyPreUtils() {
     42 
     43     }
     44 
     45     public ComplexPropertyPreUtils(Map<Class<?>, String[]> includes) {
     46         super();
     47         this.includes = includes;
     48     }
     49 
     50     public boolean apply(JSONSerializer serializer, Object source, String name) {
     51 
     52         //对象为空。直接放行
     53         if (source == null) {
     54             return true;
     55         }
     56 
     57         // 获取当前需要序列化的对象的类对象
     58         Class<?> clazz = source.getClass();
     59 
     60         // 无需序列的对象、寻找需要过滤的对象,可以提高查找层级
     61         // 找到不需要的序列化的类型
     62         for (Map.Entry<Class<?>, String[]> item : this.excludes.entrySet()) {
     63             // isAssignableFrom(),用来判断类型间是否有继承关系
     64             if (item.getKey().isAssignableFrom(clazz)) {
     65                 String[] strs = item.getValue();
     66 
     67                 // 该类型下 此 name 值无需序列化
     68                 if (isHave(strs, name)) {
     69                     return true;
     70                 } else {
     71                     return false;
     72                 }
     73             }
     74         }
     75 
     76         // 需要序列的对象集合为空 表示 全部需要序列化
     77         if (this.includes.isEmpty()) {
     78             return true;
     79         }
     80 
     81         // 需要序列的对象
     82         // 找到不需要的序列化的类型
     83         for (Map.Entry<Class<?>, String[]> item : this.includes.entrySet()) {
     84             // isAssignableFrom(),用来判断类型间是否有继承关系
     85             if (item.getKey().isAssignableFrom(clazz)) {
     86                 String[] strs = item.getValue();
     87                 // 该类型下 此 name 值无需序列化
     88                 if (isHave(strs, name)) {
     89                     return true;
     90                 }
     91             }
     92         }
     93 
     94         return false;
     95     }
     96 
     97     /*
     98      * 此方法有两个参数,第一个是要查找的字符串数组,第二个是要查找的字符或字符串
     99      */
    100     public static boolean isHave(String[] strs, String s) {
    101 
    102         for (int i = 0; i < strs.length; i++) {
    103             // 循环查找字符串数组中的每个字符串中是否包含所有查找的内容
    104             if (strs[i].equals(s)) {
    105                 // 查找到了就返回真,不在继续查询
    106                 return true;
    107             }
    108         }
    109 
    110         // 没找到返回false
    111         return false;
    112     }
    113 
    114     public Map<Class<?>, String[]> getIncludes() {
    115         return includes;
    116     }
    117 
    118     public void setIncludes(Map<Class<?>, String[]> includes) {
    119         this.includes = includes;
    120     }
    121 
    122     public Map<Class<?>, String[]> getExcludes() {
    123         return excludes;
    124     }
    125 
    126     public void setExcludes(Map<Class<?>, String[]> excludes) {
    127         this.excludes = excludes;
    128     }
    129 
    130 }
  • 相关阅读:
    虚拟机docker开启服务,本地无法进行访问
    make编译提示:make cc Command not found 解决办法
    yum -y install git 无法安装...提示There are no enabled repos.
    linux 安装mysql
    linux 配置环境变量
    HTML5第三天 无序有序列表、相对绝对路径
    JavaScript第一天
    HTML第二天
    mysql流程控制语句
    mysql存储过程和函数
  • 原文地址:https://www.cnblogs.com/jasonandy/p/9184831.html
Copyright © 2011-2022 走看看