当前位置:网站首页>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
边栏推荐
- Mysql-centos7 install MySQL through yum
- MySQL performance_ Schema common performance diagnosis query
- I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
- Go language learning notes - Gorm use - Gorm processing errors | web framework gin (10)
- Message queue: how to deal with message backlog?
- yarn入门(一篇就够了)
- Three level menu data implementation, nested three-level menu data
- Classic questions about data storage
- Différenciation et introduction des services groupés, distribués et microservices
- 原生小程序 之 input切换 text与password类型
猜你喜欢

力扣102题:二叉树的层序遍历

【FPGA教程案例14】基于vivado核的FIR滤波器设计与实现

AI face editor makes Lena smile

驱动开发中platform设备驱动架构详解

Introduction to distributed transactions

苹果cms V10模板/MXone Pro自适应影视电影网站模板

I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction

Distributed global ID generation scheme
![[InstallShield] Introduction](/img/df/4522d06510ff918d00659b8358368f.jpg)
[InstallShield] Introduction

2pc of distributed transaction solution
随机推荐
随机生成session_id
Interview skills of software testing
Introduction to distributed transactions
[solved] record an error in easyexcel [when reading the XLS file, no error will be reported when reading the whole table, and an error will be reported when reading the specified sheet name]
目标检测中的损失函数与正负样本分配:RetinaNet与Focal loss
Bbox regression loss function in target detection -l2, smooth L1, IOU, giou, Diou, ciou, focal eiou, alpha IOU, Siou
yarn入门(一篇就够了)
Personal imitation SSM framework
Go 语言的 Context 详解
Bat instruction processing details
【FPGA教程案例13】基于vivado核的CIC滤波器设计与实现
AI face editor makes Lena smile
Win configuration PM2 boot auto start node project
How much do you know about clothing ERP?
SQL Server 2008 各种DateTime的取值范围
Randomly generate session_ id
How to improve website weight
【日常训练--腾讯精选50】235. 二叉搜索树的最近公共祖先
Flask 1.1.4 werkzeug1.0.1 analyse du code source: processus de démarrage
What is dependency injection (DI)