当前位置:网站首页>搞懂fastjson 对泛型的反序列化原理
搞懂fastjson 对泛型的反序列化原理
2022-07-07 00:34:00 【香菜聊游戏】
fastjson 是现在非常常用的一个json 的序列化和反序列化工具,是阿里发布的,虽然最近一直在暴雷,各种漏洞,但是这不影响我们学习他。
fastjson的使用
加入依赖
在pom.xml中直接增加fastjson的依赖就行了,向下面这样
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
常规使用
public static void main(String[] args) {
TreeNode treeNode = new TreeNode(10);
//转为字符串
String jsonStr = JSON.toJSONString(treeNode);
// 字符串转为对象
TreeNode treeNode1 = JSON.parseObject(jsonStr, TreeNode.class);
System.out.println();
}
泛型的使用
看下我们常用的反序列化
List<TreeNode> list = JSON.parseObject("",new TypeReference<List<TreeNode>>(){});
先解释下: TypeReference 是一个泛型类,持有了当前的类型
这里的泛型的反序列化,到底是如何实现的呐? 到底在运行时怎么知道需要序列化的泛型是什么呐? 今天学习下知识点,也是今天主要想记录的。
源码
想懂原理还得扒源码,看懂了也没什么神奇了
知识点1 匿名类
new TypeReference<List>(){}
这行代码就是创建一个匿名类,等价于创建一个类实现TypeReference ,然后进行实例化。 千万不要迷糊,因为平常的时候我们一般是对接口使用,比如 Runnable,不熟悉的话可以复习下
知识点2 重点
直接点进去TypeReference 这个类,可以看到下面的构造函数
protected TypeReference(){
Type superClass = getClass().getGenericSuperclass();
// 这里是重点,看样子可以获取到泛型的信息
Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
Type cachedType = classTypeCache.get(type);
if (cachedType == null) {
classTypeCache.putIfAbsent(type, type);
cachedType = classTypeCache.get(type);
}
this.type = cachedType;
}
Type type = ((ParameterizedType) superClass).getActualTypeArguments()[0];
这个函数开发中是极少使用,即使作为老司机我也没有用过这个函数,你用过吗?
这句一定要记住,也是泛型的实现最重要的方式,一定是superclass,还做了一次强转。然后转为自己要用的类型 注意这里
总结
最后总结下,fastjson的泛型的反序列化底层原理就是根据type 进行反序列化, 知识点就是 匿名类 和 getActualTypeArguments 最后留个作业,你可以模拟一个这样的场景,如果你在开发中需要在运行时知道泛型的类型, 可以使用这种方法
边栏推荐
- 随机生成session_id
- Web authentication API compatible version information
- Interview questions and salary and welfare of Shanghai byte
- Industrial Finance 3.0: financial technology of "dredging blood vessels"
- 高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
- How does mapbox switch markup languages?
- Reptile exercises (III)
- Modes of optical fiber - single mode and multimode
- [云原生]微服务架构是什么?
- Message queue: how to deal with message backlog?
猜你喜欢
Three level menu data implementation, nested three-level menu data
什么是消息队列?
Common skills and understanding of SQL optimization
Red Hat安装内核头文件
Dynamic memory management
消息队列:消息积压如何处理?
SQLSTATE[HY000][1130] Host ‘host. docker. internal‘ is not allowed to connect to this MySQL server
Opensergo is about to release v1alpha1, which will enrich the service governance capabilities of the full link heterogeneous architecture
ForkJoin最全详解(从原理设计到使用图解)
pytorch_ 01 automatic derivation mechanism
随机推荐
Interview skills of software testing
Paper reading [open book video captioning with retrieve copy generate network]
WEB架构设计过程
【日常训练--腾讯精选50】292. Nim 游戏
Educational Codeforces Round 22 B. The Golden Age
盘点国内有哪些EDA公司?
Forkjoin is the most comprehensive and detailed explanation (from principle design to use diagram)
Things about data storage 2
【日常训练--腾讯精选50】235. 二叉搜索树的最近公共祖先
【Shell】清理nohup.out文件
pytorch_ 01 automatic derivation mechanism
目标检测中的损失函数与正负样本分配:RetinaNet与Focal loss
TCC of distributed transaction solutions
微信小程序蓝牙连接硬件设备并进行通讯,小程序蓝牙因距离异常断开自动重连,js实现crc校验位
[云原生]微服务架构是什么?
Industrial Finance 3.0: financial technology of "dredging blood vessels"
Input of native applet switches between text and password types
分布式事务介绍
Introduction to distributed transactions
Taobao store release API interface (New), Taobao oauth2.0 store commodity API interface, Taobao commodity release API interface, Taobao commodity launch API interface, a complete set of launch store i