FastJson前置属性过滤器

 /**
* <html>
* <body>
* <P> Copyright 1994 JsonInternational</p>
* <p> All rights reserved.</p>
* <p> Created on 19941115</p>
* <p> Created by Jason</p>
* </body>
* </html>
*/
package cn.ucaner.alpaca.framework.utils.properties; import java.util.HashMap;
import java.util.Map; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.PropertyPreFilter;
import com.alibaba.fastjson.serializer.SerializerFeature; /**
* @Package:cn.ucaner.common.utils
* @ClassName:ComplexPropertyPreUtils
* @Description: <p> 前置属性过滤器</p>
* @Author: - Jason
* @CreatTime:2017年10月24日 上午10:49:03
* @Modify By:
* @ModifyTime:
* @Modify marker:
* @version V1.0
*/
public class ComplexPropertyPreUtils implements PropertyPreFilter { private Map<Class<?>, String[]> includes = new HashMap<>();
private Map<Class<?>, String[]> excludes = new HashMap<>(); static {
JSON.DEFAULT_GENERATE_FEATURE |= SerializerFeature.DisableCircularReferenceDetect.getMask();
} public ComplexPropertyPreUtils() { } public ComplexPropertyPreUtils(Map<Class<?>, String[]> includes) {
super();
this.includes = includes;
} public boolean apply(JSONSerializer serializer, Object source, String name) { //对象为空。直接放行
if (source == null) {
return true;
} // 获取当前需要序列化的对象的类对象
Class<?> clazz = source.getClass(); // 无需序列的对象、寻找需要过滤的对象,可以提高查找层级
// 找到不需要的序列化的类型
for (Map.Entry<Class<?>, String[]> item : this.excludes.entrySet()) {
// isAssignableFrom(),用来判断类型间是否有继承关系
if (item.getKey().isAssignableFrom(clazz)) {
String[] strs = item.getValue(); // 该类型下 此 name 值无需序列化
if (isHave(strs, name)) {
return true;
} else {
return false;
}
}
} // 需要序列的对象集合为空 表示 全部需要序列化
if (this.includes.isEmpty()) {
return true;
} // 需要序列的对象
// 找到不需要的序列化的类型
for (Map.Entry<Class<?>, String[]> item : this.includes.entrySet()) {
// isAssignableFrom(),用来判断类型间是否有继承关系
if (item.getKey().isAssignableFrom(clazz)) {
String[] strs = item.getValue();
// 该类型下 此 name 值无需序列化
if (isHave(strs, name)) {
return true;
}
}
} return false;
} /*
* 此方法有两个参数,第一个是要查找的字符串数组,第二个是要查找的字符或字符串
*/
public static boolean isHave(String[] strs, String s) { for (int i = 0; i < strs.length; i++) {
// 循环查找字符串数组中的每个字符串中是否包含所有查找的内容
if (strs[i].equals(s)) {
// 查找到了就返回真,不在继续查询
return true;
}
} // 没找到返回false
return false;
} public Map<Class<?>, String[]> getIncludes() {
return includes;
} public void setIncludes(Map<Class<?>, String[]> includes) {
this.includes = includes;
} public Map<Class<?>, String[]> getExcludes() {
return excludes;
} public void setExcludes(Map<Class<?>, String[]> excludes) {
this.excludes = excludes;
} }

FastJson前置属性过滤器的更多相关文章

  1. fastjson格式化bean的简易属性过滤器

    fastjson的bean属性过滤器 有的时候,我们在接口开发时,一个完整的bean会包含很多属性,但是前端接口只需要其中的某几个属性时,应该在对json的返回要进行精简.下面直接看代码 packag ...

  2. grootJs 属性过滤器

    index10.html <html><head> <title>属性过滤器</title> <script src="jquery-1 ...

  3. HCNP Routing&Switching之BGP团体属性和团体属性过滤器

    前文我们了解了BGP的路由过滤已经as-path过滤器的使用相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15542559.html:今天我们来聊一聊 ...

  4. @JsonInclude注解,RestTemplate传输值为null的属性,利用FastJson将属性中有空值null的对象转化成Json字符串

    一个pojo类: import lombok.Data; @Data public class Friend { private String name; private int age; priva ...

  5. Jsonlib 属性过滤器

    /** * @title JSON转换属性过滤器 * @description 用于JSON lib的JSON转换 * @author maohuidong * @date 2017-04-06 */ ...

  6. fastjson的值过滤器ValueFilter

    https://blog.csdn.net/linyifan_/article/details/83060408 原创林天乐 发布于2018-10-15 16:20:25 阅读数 1462  收藏 展 ...

  7. fastJson + lombok + 属性名命名 踩坑点

    JavaBean属性名要求:前两个字母要么都大写,要么都小写 package com.jdyh.worker.project.controller; import com.alibaba.fastjs ...

  8. jquery属性过滤器

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. jQuery表单对象属性过滤器再探究(原创)

    上面例子的总结: 1.”+n+”千万不要把前面或者后面的+漏掉了.否则不会出现正确结果 2.$(“:checkbox”).click(countChecked)注意写法,不是click(functio ...

随机推荐

  1. SQL Mode

    SQL Mode简介 在MySQL中,SQL Mode常常用来解决以下问题: 1.通过设置SQL Mode,可以完成不同严格程度的数据校验,有效保证数据准确性. 2.通过设置SQL Mode为ANSI ...

  2. (转) 中断处理程序&中断服务例程

             关于中断处理程序和中断服务例程ISR的区别及联系,之前一直搞混,今天抽时间将两者关系弄弄清楚.ok,下面进入主题.       首先中断处理程序(Interrupt Handler) ...

  3. 【技术博客】Django+uginx+uwsgi框架的服务器部署

    1.登录服务器 使用ssh来直接登录到服务器terminal进行操作,推荐使用XShell和XFtp来进行远程登录和文件传输. 2.运行环境准备 本组获得的华为云服务器为ubuntu16.04版本,先 ...

  4. 基于cesium的GIS洪水淹没三维模拟系统

    简介: “FloodFreeth3D”是一款对Mike11软件计算的洪水演进结果使用cesium进行淹没演进三维模拟的软件产品.   技术参数: 1. B/S架构,支持多Web浏览器(ie.chrom ...

  5. 解析生效测试方法 执行命令 ping 域名 得不到 IP 主要有如下几个原因:

    https://help.aliyun.com/knowledge_detail/39834.html dig https://cloud.tencent.com/document/product/3 ...

  6. JMH java基准测试

    Measure, don’t guess! JMH适用场景 JMH只适合细粒度的方法测试 原理 编译时会生成一些测试代码,一般都会继承你的类 maven依赖 <dependencies> ...

  7. OpenStack(一)——OpenStack的相关概念

    (1).OpenStack概述 OpenStack是一个由NASA(美国国家航空航天局)和Rackspace合作研发并发起的,以Apache许可证授权的自由软件和开放源代码项目. OpenStack是 ...

  8. Python - Django - 中间件 process_template_response

    process_template_response(self, request, response) 有两个参数,response 是 TemplateResponse 对象(由视图函数或者中间件产生 ...

  9. Flask自动刷新前端页面(方便调试)livereload

    是不是每次调整模板文件,就要停止flask服务器,重启flask服务器,再去浏览器刷新页面? 有没有办法自动完成这3步呢? 安装livereload即可, 仅仅把app.run() 改为下面的例子就可 ...

  10. 【SQL骚操作】SqlServer数据库表生成C# Model实体类SQL语句

    已知现有表T1 想快速获取cs类结构 /// <summary> /// T1 /// </summary> public class T1 { /// <summary ...