Jsonlib 属性过滤器
/**
* @title JSON转换属性过滤器
* @description 用于JSON lib的JSON转换
* @author maohuidong
* @date 2017-04-06
*/
public class JsonConvertPropertyFilter implements PropertyFilter {
/**
* @function apply
* @param obj:待转换的对象 s: 对象的属性 value:对象的属性值
* @description JSON转换属性过滤器(Hibernate)
* @return true:过滤 false:不过滤
* @author maohuidong
* @date 2017-04-06
*/
@Override
public boolean apply(Object obj, String s, Object value) {
// hibernate代理对象未初始化,则过滤掉
if (value instanceof HibernateProxy) {
LazyInitializer initializer = ((HibernateProxy) value)
.getHibernateLazyInitializer();
if (initializer.isUninitialized()) {
return true;
}
}
// Hibernate持久化集合为初始化,则过滤掉(实体关联一对多)
if (value instanceof PersistentCollection) {
PersistentCollection collection = (PersistentCollection) value;
if (!collection.wasInitialized()) {
return true;
}
}
return false;
}
}
/**
* @function objStreamOutput
* @param 无
* @description 输出流封装
* @return 无
* @author maohuidong
* @date 2017-04-09
*/
protected void objStreamOutput(Object object,List<String> fieldFilter) throws IOException{
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
PrintWriter pWriter = response.getWriter();
//Json过滤
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setIgnoreDefaultExcludes(false);
if(fieldFilter == null){
fieldFilter = new ArrayList<String>();
}
//Json转换过滤没用的字段
fieldFilter.add("hibernateLazyInitializer");
String[] filter = new String[fieldFilter.size()];
filter = fieldFilter.toArray(filter);
jsonConfig.setExcludes(filter);
//Json转换过滤未初始化的数据
jsonConfig.setJsonPropertyFilter(new JsonConvertPropertyFilter());
//结果转换为json字符串
String str = "";
//结果集能实例化为List
if(object instanceof List){
JSONArray jsonArray = JSONArray.fromObject(object, jsonConfig);
str = jsonArray.toString();
}else if(object instanceof String){
str = (String) object;
}else if(object instanceof Number){
str = object.toString();
}
else{
JSONObject jsonObject = JSONObject.fromObject(object, jsonConfig);
str = jsonObject.toString();
}
pWriter.print(str);
pWriter.flush();
pWriter.close();
}
Jsonlib 属性过滤器的更多相关文章
- grootJs 属性过滤器
index10.html <html><head> <title>属性过滤器</title> <script src="jquery-1 ...
- fastjson格式化bean的简易属性过滤器
fastjson的bean属性过滤器 有的时候,我们在接口开发时,一个完整的bean会包含很多属性,但是前端接口只需要其中的某几个属性时,应该在对json的返回要进行精简.下面直接看代码 packag ...
- FastJson前置属性过滤器
FastJson前置属性过滤器 /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p ...
- HCNP Routing&Switching之BGP团体属性和团体属性过滤器
前文我们了解了BGP的路由过滤已经as-path过滤器的使用相关话题,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/15542559.html:今天我们来聊一聊 ...
- jquery属性过滤器
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- jQuery表单对象属性过滤器再探究(原创)
上面例子的总结: 1.”+n+”千万不要把前面或者后面的+漏掉了.否则不会出现正确结果 2.$(“:checkbox”).click(countChecked)注意写法,不是click(functio ...
- 前端MVC Vue2学习总结(三)——模板语法、过滤器、计算属性、观察者、Class 与 Style 绑定
Vue.js 使用了基于 HTML 的模版语法,允许开发者声明式地将 DOM 绑定至底层 Vue 实例的数据.所有 Vue.js 的模板都是合法的 HTML ,所以能被遵循规范的浏览器和 HTML 解 ...
- 第一章 JacksonUtil 序列化与反序列化属性总结
1.json-lib与Jackson 关于json-lib与Jackson对比总结如下: 1).性能方面,Jackson的处理能力高出Json-lib10倍左右. 2).json-lib已经停止更新, ...
- Jquery最全过滤器总结
不管什么时候,总是有这么些时候:当我们使用jQuery的各种过滤器时,总是有那么几个记不牢,还要搜索一下或者翻翻手册!多少次想总结一下,最终都没总结,现在网上找到一篇总结的不错的,但是排版有点乱,本人 ...
随机推荐
- pthread中errors.h的代码
#ifndef __errors_h #define __errors_h #include <unistd.h> #include <errno.h> #include &l ...
- yii2 rbac权限管理学习笔记
下面介绍一个 yii2 的 Rbac 权限管理设置,闲话少说,直接上代码, 1.首先我们要在组件里面配置一下 Rbac ,如下所示(common/config/main-local.php或者main ...
- Visual Studio2010不能安装Silverlight4_Tools,提示语言不一致
天在装Silverlight4_Tools时出现“必须先安装与 Silverlight Tools 4 语言版本相一致的 Visual Studio 2010.Visual Web Developer ...
- elastic-job 分布式定时任务框架 在 SpringBoot 中如何使用(二)动态添加任务需求
之前一篇用过了如何在使用创建最简单的任务:比如每天定时清空系统的缓存 这篇文章主要讲解:如何运用elastic-job-lite做灵活的细粒度任务,比如: 如何定时取消某个订单在下订单后30分钟未支付 ...
- datacolumn 表达式 除数为0
dt.columns.add("avgp",typeof(decimal),"IIF(qty=0,0,price/qty)")
- bzoj4471 bzoj4490 随机数生成器Ⅱ
Description 继NOI2014后,小H又发现了一种新的生成随机数的方法.首先,给定三个随机种子P,C1,C2(C1≤C2)生成一个序列{xi},{xi}满足对于任意的i≥0,满足以下递推式X ...
- DDA画线算法
#include<stdio.h> #include"graphics.h" #include<math.h> #include<stdlib.h&g ...
- appium+python自动化42-微信公众号
前言 本篇介绍如何在微信公众号上自动化测试,以操作我的个人公众号:yoyoketang为例,没关注的,先微信关注了,再跟着操作 环境准备: python 3.6 appium 1.7以上版本 微信6. ...
- 学习笔记之Bitbucket
Bitbucket | The Git solution for professional teams https://bitbucket.org/ Git Tutorials and Trainin ...
- [转]C#调用Excel VBA宏
[转载自]http://www.shangxueba.com/jingyan/95031.html 附上一段原创常用代码 计算列标题字符串 Function CalcColumn(ByVal c As ...