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. ex1

    #include <stdio.h> int main() { int x; printf("输入一个整数: \n"); scanf("%d",&a ...

  2. 如何在Docker容器之间拷贝数据

    [编者的话]在容器之间拷贝数据是Docker一个重要而且基本的功能.拷贝数据到其他容器是一个经常使用到的场景,如当服务器遇到不可预见的“灾难”(注:断电,宕机)时,起到备份数据的作用.本文作者详细介绍 ...

  3. Android Studio 之 LiveData 的配合使用,底层数据变化,自动通知界面

    Android Studio 之 LiveData 的配合使用,底层数据变化,自动通知界面 viewModelWithLikeData.getLikeNumber().observe(this, ne ...

  4. Characters with Hash[签到题]

    目录 题目地址 题干 代码和解释 参考 题目地址 Characters with Hash(ACM-ICPC 2018 徐州赛区网络预赛) 题干 代码和解释 本题很好理解,通过一个seed,将输入的定 ...

  5. [BUAA软工]beta阶段贡献分

    团队成员在Beta阶段的角色和具体贡献: 名字 角色 具体的可衡量的可验证的贡献 zpj 前段+ 前后端对接 博客X1 20+ commits ui 设计与实现 bug fixed: 2 推广:10 ...

  6. 声源定位之3精读《The Generalized Correlation Method for Estimation of Time Delay》

    2.2.1 标题(2019年9月5日) 估计时间差的广义互相关方法 互相关可以表示两个信号的相似程度. 计算:两个信号循环移位相乘再相加,得到的一组互相关值. 相关函数: 何为广义?通用的框架,可以设 ...

  7. 韦东山视频第3课第1节_JNI_P【学习笔记】

    一.android系统java调用C方法的大概的流程图如下: 二.下面写一个JNI的程序,java的hello方法在加载native库之后能够调用C方法. 2.1 JNIDemo.java 文件内容如 ...

  8. sql为什么要用where 1=1?

    这个1=1常用于应用程序根据用户选择项的不同拼凑where条件时用的.例如:查询用户的信息,where默认为1=1,这样用户即使不选择任何条件,sql查询也不会出错.如果用户选择了姓名,那么where ...

  9. 使用leaflet绘制geoJson中国边界

    绘制中国边界 代码如下: function drawChina() { //设置样式 var myStyle = { "color": "#00f", &quo ...

  10. git解决error: The following untracked working tree files would be overwritten by checkout

    在IDEA中进行分支切换时,出现如此错误,导致无法正常切换:error: The following untracked working tree files would be overwritten ...