博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JsonUtils工具整理
阅读量:7041 次
发布时间:2019-06-28

本文共 2136 字,大约阅读时间需要 7 分钟。

import java.lang.reflect.Type;import org.apache.commons.pool2.impl.GenericObjectPool;import org.apache.commons.pool2.impl.GenericObjectPoolConfig;import com.google.gson.Gson;public class JsonPoolUtils {    private static ThreadLocal
instance = new ThreadLocal
(); protected static GenericObjectPool
pool; static { if (instance.get() == null || pool == null) { GenericObjectPoolConfig conf = new GenericObjectPoolConfig(); conf.setMaxTotal(32); conf.setMaxIdle(4); pool = new GenericObjectPool
(new PooledGsonFactory(), conf); } } public final static
T fromJson(String json, Type typeOfT) { if (json == null) { return null; } Gson gson = null; try { gson = pool.borrowObject(); } catch (Exception e) { e.printStackTrace(); } T t = gson.fromJson(json, typeOfT); pool.returnObject(gson); return t; } public final static String toJson(Object src) { if (src == null) { return null; } Gson gson = null; try { gson = pool.borrowObject(); } catch (Exception e) { e.printStackTrace(); } String string = gson.toJson(src); pool.returnObject(gson); return string; }}

 

 

import java.sql.Timestamp;import org.apache.commons.pool2.BasePooledObjectFactory;import org.apache.commons.pool2.PooledObject;import org.apache.commons.pool2.impl.DefaultPooledObject;import com.google.gson.Gson;import com.google.gson.GsonBuilder;public class PooledGsonFactory extends BasePooledObjectFactory
{ @Override public Gson create() throws Exception { return new GsonBuilder().registerTypeAdapter(Timestamp.class, new TimestampTypeAdapter()).create(); } @Override public PooledObject
wrap(Gson obj) { return new DefaultPooledObject
(obj); }}

 

转载于:https://www.cnblogs.com/light-zhang/p/8745126.html

你可能感兴趣的文章
【整理】网站返回顶部代码
查看>>
使用Cloud-Config
查看>>
sqlite3-第五章 API-核心API
查看>>
linux svn服务器权限配置
查看>>
Objective-C语法property详解
查看>>
通过Kickstart 制作引导镜像
查看>>
ubuntu安装salt-ssh
查看>>
从浏览器打开网址到请求到网页内容超细原理过程详解(免费)
查看>>
windows内存泄漏检测
查看>>
TF-IDF
查看>>
Hbase日常运维
查看>>
我是架构师-常用类型-容器-不再推荐的容器
查看>>
【一天一个shell命令】文本操作系列-tree
查看>>
【一天一个shell命令】文本操作系列-tac,rev
查看>>
链表中环的入口结点
查看>>
CNN-Pooling layer&strided convolution
查看>>
join联表中on,where后面跟条件的区别
查看>>
利用AWS boto实现EC2 存储卷的自动快照
查看>>
微软私有云解决方案专家认证之路
查看>>
曾经的痛啊 关于 becomeFirstResponder
查看>>