当前位置:网站首页>Understand the deserialization principle of fastjson for generics
Understand the deserialization principle of fastjson for generics
2022-07-07 05:57:00 【Coriander Chat Game】

fastjson It is a very common one now json Serialization and deserialization tools , It was released by Alibaba , Although it has been thundering recently , Various vulnerabilities , But this does not affect our learning from him .
fastjson Use
Join the rely on
stay pom.xml Direct increase in fastjson Just rely on , This way down
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
Regular use
public static void main(String[] args) {
TreeNode treeNode = new TreeNode(10);
// To string
String jsonStr = JSON.toJSONString(treeNode);
// String to object
TreeNode treeNode1 = JSON.parseObject(jsonStr, TreeNode.class);
System.out.println();
}
The use of generics
Take a look at our commonly used deserialization
List<TreeNode> list = JSON.parseObject("",new TypeReference<List<TreeNode>>(){});
First explain : TypeReference Is a generic class , Holds the current type
The deserialization of generics here , How to achieve it ? How do you know what generic types need to be serialized at runtime ? Learn some knowledge today , It's also what I want to record today .
Source code
If you want to understand the principle, you have to dig the source code , It's no wonder to understand
Knowledge point 1 An anonymous class
new TypeReference<List>(){}
This line of code is to create an anonymous class , Equivalent to creating a class implementation TypeReference , Then you instantiate it . Don't get confused , Because we usually use interfaces , such as Runnable, If you are not familiar with it, you can review
Knowledge point 2 a key
Just click in TypeReference This class , You can see the following constructor
protected TypeReference(){
Type superClass = getClass().getGenericSuperclass();
// Here's the point , It seems that you can get generic information
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];
This function is rarely used in development , Even as an old driver, I haven't used this function , Have you used it ?
This sentence must be remembered , It is also the most important way to implement generics , It must be superclass, Also made a strong turn . Then switch to the type you want to use Note that there
summary
So just to conclude ,fastjson The underlying principle of deserialization of generics is based on type deserialize , The knowledge point is An anonymous class and getActualTypeArguments Finally, leave an assignment , You can simulate such a scenario , If you need to know the type of generics at runtime in development , This method can be used
边栏推荐
- Polynomial locus of order 5
- Question 102: sequence traversal of binary tree
- Explication contextuelle du langage Go
- STM32 key state machine 2 - state simplification and long press function addition
- PowerPivot - DAX (function)
- Randomly generate session_ id
- [daily training -- Tencent selected 50] 292 Nim games
- make makefile cmake qmake都是什么,有什么区别?
- Flask1.1.4 Werkzeug1.0.1 源码分析:启动流程
- @pathvariable 和 @Requestparam的详细区别
猜你喜欢

404 not found service cannot be reached in SAP WebService test

PowerPivot——DAX(函数)

软件测试面试技巧

Forkjoin is the most comprehensive and detailed explanation (from principle design to use diagram)

PTA ladder game exercise set l2-004 search tree judgment

搞懂fastjson 对泛型的反序列化原理

Distributed global ID generation scheme

ML之shap:基于adult人口普查收入二分类预测数据集(预测年收入是否超过50k)利用shap决策图结合LightGBM模型实现异常值检测案例之详细攻略
![[cloud native] what is the microservice architecture?](/img/84/a0ec68646083f3539aa39ad9d98749.png)
[cloud native] what is the microservice architecture?
![[云原生]微服务架构是什么?](/img/84/a0ec68646083f3539aa39ad9d98749.png)
[云原生]微服务架构是什么?
随机推荐
关于服装ERP,你知道多少?
毕业之后才知道的——知网查重原理以及降重举例
Reading notes of Clickhouse principle analysis and Application Practice (6)
力扣102题:二叉树的层序遍历
Web architecture design process
[云原生]微服务架构是什么?
MySQL-CentOS7通过YUM安装MySQL
[shell] clean up nohup Out file
微信小程序蓝牙连接硬件设备并进行通讯,小程序蓝牙因距离异常断开自动重连,js实现crc校验位
Flinksql 读写pgsql
爬虫练习题(三)
判断文件是否为DICOM文件
Go 语言的 Context 详解
pytorch_ 01 automatic derivation mechanism
产业金融3.0:“疏通血管”的金融科技
【已解决】记一次EasyExcel的报错【读取xls文件时全表读不报错,指定sheet名读取报错】
Ten stages of becoming a Senior IC Design Engineer. What stage are you in now?
Nvisual network visualization
zabbix_get测试数据库失败
SAP ABAP BDC(批量数据通信)-018