FastJson前置属性过滤器
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前置属性过滤器的更多相关文章
- fastjson格式化bean的简易属性过滤器
fastjson的bean属性过滤器 有的时候,我们在接口开发时,一个完整的bean会包含很多属性,但是前端接口只需要其中的某几个属性时,应该在对json的返回要进行精简.下面直接看代码 packag ...
- grootJs 属性过滤器
index10.html <html><head> <title>属性过滤器</title> <script src="jquery-1 ...
- HCNP Routing&Switching之BGP团体属性和团体属性过滤器
前文我们了解了BGP的路由过滤已经as-path过滤器的使用相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15542559.html:今天我们来聊一聊 ...
- @JsonInclude注解,RestTemplate传输值为null的属性,利用FastJson将属性中有空值null的对象转化成Json字符串
一个pojo类: import lombok.Data; @Data public class Friend { private String name; private int age; priva ...
- Jsonlib 属性过滤器
/** * @title JSON转换属性过滤器 * @description 用于JSON lib的JSON转换 * @author maohuidong * @date 2017-04-06 */ ...
- fastjson的值过滤器ValueFilter
https://blog.csdn.net/linyifan_/article/details/83060408 原创林天乐 发布于2018-10-15 16:20:25 阅读数 1462 收藏 展 ...
- fastJson + lombok + 属性名命名 踩坑点
JavaBean属性名要求:前两个字母要么都大写,要么都小写 package com.jdyh.worker.project.controller; import com.alibaba.fastjs ...
- jquery属性过滤器
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- jQuery表单对象属性过滤器再探究(原创)
上面例子的总结: 1.”+n+”千万不要把前面或者后面的+漏掉了.否则不会出现正确结果 2.$(“:checkbox”).click(countChecked)注意写法,不是click(functio ...
随机推荐
- 您使用的私钥格式错误,请检查RSA私钥配置,charset = utf-8 密钥集不存在
支付宝突然报异常 您使用的私钥格式错误,请检查RSA私钥配置,charset = utf-8 经排查:系统日志 System.Security.Cryptography.CryptographicEx ...
- AttributeError: module 'pytest' has no attribute 'allure'
解决 pip3 uninstall pytest-allure-adaptor pip3 install allure-pytest 参考: https://www.cnblogs.com/lansa ...
- php如何实现三级分销
Q: 项目要实现三级分销;对于数据库的设计和用户注册后给所有上级(最多三级)返利 但是一点头绪都没有,请大神帮忙给个思路! 如果是直接注册给奖励20元如果是通过二维码或者链接进入的注册页面 找到上级 ...
- python操作mysql基础一
python操作mysql基础一 使用Python操作MySQL的一些基本方法 前奏 为了能操作数据库, 首先我们要有一个数据库, 所以要首先安装Mysql, 然后创建一个测试数据库python_te ...
- 【大数据】Spark On Yarn
Spark在YARN中有yarn-cluster和yarn-client两种运行模式: I. Yarn client 在yarn-client模式下,Driver运行在Client上,通过Applic ...
- QT5 编译提示 cannot find GL/gl.h ubuntu16.04
安装软件: sudo apt-get install mesa-common-dev sudo apt-get install libgl1-mesa-dev libglu1-mesa-dev
- Automl基于超大数据下的数据分发方案探讨
先定义几个关键字: 任务:用户一次上传的数据集并发起的automl任务,比如一次ocr任务,一次图像分类任务. 模型:一次任务中,需要运行的多个模型,比如ocr任务,需要ctpn模型,需要crnn模型 ...
- php mysql 根据经纬度计算距离和排序
#.两点距离(1.4142135623730951) ,),point(,)); ORDER BY distance; mysql 5.6 添加 #.两点球面距离(.0357231545m) ,),p ...
- SpringMVC基本
SpringMVC和Struts2的区别 SpringMVC与Struts2区别 对比项目 SrpingMVC Struts2 优势 国内市场情况 有大量用户,一般新项目启动都会选用springmvc ...
- rem js相关
!function(n){ var e=n.document, t=e.documentElement, i=720, d=i/100, o="orientationchange" ...