当前位置:网站首页>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
边栏推荐
- How much do you know about clothing ERP?
- Dynamic memory management
- 毕业之后才知道的——知网查重原理以及降重举例
- An example of multi module collaboration based on NCF
- 软件测试面试技巧
- What EDA companies are there in China?
- 原生小程序 之 input切换 text与password类型
- Forkjoin is the most comprehensive and detailed explanation (from principle design to use diagram)
- Check Point:企业部署零信任网络(ZTNA)的核心要素
- PowerPivot——DAX(函数)
猜你喜欢
404 not found service cannot be reached in SAP WebService test
Loss function and positive and negative sample allocation in target detection: retinanet and focal loss
毕业之后才知道的——知网查重原理以及降重举例
Digital IC interview summary (interview experience sharing of large manufacturers)
如何提高网站权重
绕过open_basedir
Sidecar mode
Three level menu data implementation, nested three-level menu data
Pytorch builds neural network to predict temperature
老板总问我进展,是不信任我吗?(你觉得呢)
随机推荐
PowerPivot——DAX(函数)
EMMC print cqhci: timeout for tag 10 prompt analysis and solution
Forkjoin is the most comprehensive and detailed explanation (from principle design to use diagram)
C nullable type
关于服装ERP,你知道多少?
SAP ABAP BDC(批量数据通信)-018
I didn't know it until I graduated -- the principle of HowNet duplication check and examples of weight reduction
谈fpga和asic的区别
pytorch_ 01 automatic derivation mechanism
PTA TIANTI game exercise set l2-003 moon cake test point 2, test point 3 Analysis
OpenSergo 即将发布 v1alpha1,丰富全链路异构架构的服务治理能力
Red hat install kernel header file
PTA 天梯赛练习题集 L2-004 搜索树判断
Type de texte de commutation d'entrée et de mot de passe de l'applet natif
得物客服一站式工作台卡顿优化之路
[云原生]微服务架构是什么?
Reptile exercises (III)
PTA 天梯赛练习题集 L2-003 月饼 测试点2,测试点3分析
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
Flask1.1.4 Werkzeug1.0.1 源码分析:启动流程