当前位置:网站首页>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
边栏推荐
- 解决pod install报错:ffi is an incompatible architecture
- 《HarmonyOS实战—入门到开发,浅析原子化服务》
- PTA 天梯赛练习题集 L2-002 链表去重
- C nullable type
- 毕业之后才知道的——知网查重原理以及降重举例
- 980. 不同路径 III DFS
- Bbox regression loss function in target detection -l2, smooth L1, IOU, giou, Diou, ciou, focal eiou, alpha IOU, Siou
- Realize GDB remote debugging function between different network segments
- zabbix_get测试数据库失败
- 产业金融3.0:“疏通血管”的金融科技
猜你喜欢
Loss function and positive and negative sample allocation in target detection: retinanet and focal loss
R language [logic control] [mathematical operation]
Go语学习笔记 - gorm使用 - gorm处理错误 | Web框架Gin(十)
Différenciation et introduction des services groupés, distribués et microservices
Get the way to optimize the one-stop worktable of customer service
目标检测中的BBox 回归损失函数-L2,smooth L1,IoU,GIoU,DIoU,CIoU,Focal-EIoU,Alpha-IoU,SIoU
nVisual网络可视化
Message queuing: how to ensure that messages are not lost
Hcip seventh operation
Realize GDB remote debugging function between different network segments
随机推荐
【日常训练--腾讯精选50】235. 二叉搜索树的最近公共祖先
Web architecture design process
Wechat applet Bluetooth connects hardware devices and communicates. Applet Bluetooth automatically reconnects due to abnormal distance. JS realizes CRC check bit
产业金融3.0:“疏通血管”的金融科技
《HarmonyOS实战—入门到开发,浅析原子化服务》
Classic questions about data storage
【已解决】记一次EasyExcel的报错【读取xls文件时全表读不报错,指定sheet名读取报错】
Get the way to optimize the one-stop worktable of customer service
成为资深IC设计工程师的十个阶段,现在的你在哪个阶段 ?
On the difference between FPGA and ASIC
Interview skills of software testing
How to get free traffic in pinduoduo new store and what links need to be optimized in order to effectively improve the free traffic in the store
【FPGA教程案例13】基于vivado核的CIC滤波器设计与实现
关于服装ERP,你知道多少?
Web authentication API compatible version information
Distributed global ID generation scheme
yarn入门(一篇就够了)
[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]
Mac version PHP installed Xdebug environment (M1 version)
【日常训练--腾讯精选50】292. Nim 游戏