当前位置:网站首页>Fastjson's tojsonstring() source code analysis for special processing of time classes - "deepnova developer community"
Fastjson's tojsonstring() source code analysis for special processing of time classes - "deepnova developer community"
2022-07-29 08:30:00 【Dipu Technology】
author : Hezijiang
- Background introduction
This article is in the process of project iteration , Found an interesting bug Caused by the source code analysis , Here with a little face , Look at the fastjson Source code - case analysis
2.1 The process of finding problems
In project use , Find out about Timestamp The type of toJSONString() When the method is called , The output structure is not displayed as expected , Later, it will be dismantled separately demo To do research
public static void main(String[] args) {
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
JSONObject jsonObject = new JSONObject();
jsonObject.put(“timestamp”,timestamp);
System.out.println(jsonObject.toJSONString());
}
expect : Output Timestamp Class tostring Information
actual : The output is Timestamp The timestamp long value
2.2 debug Process and code analysis
When we meet people who think differently from us , We need to see how the source code is implemented . My first reaction here is fastjson Of bug, Then we need to verify that this is fastjson Of bug Or is it its own strategy
Call stack trace
public String toJSONString() {
SerializeWriter out = new SerializeWriter();
String var2;
try {
(new JSONSerializer(out)).write(this);
var2 = out.toString();
} finally {
out.close();
}
return var2;
}
It's direct here new A serialization writer Object to write operation , Click in and continue to see
public final void write(Object object) {
if (object == null) {
this.out.writeNull();
} else {
Class<?> clazz = object.getClass();
ObjectSerializer writer = this.getObjectWriter(clazz);
try {
writer.write(this, object, (Object)null, (Type)null, 0);
} catch (IOException var5) {
throw new JSONException(var5.getMessage(), var5);
}
}
}
Here's one ObjectSerializer Object is more important , Here is also the beginning to enter the core method
You can find it here ObjectSerializer Interfaces have many implementation classes , after debug Find us Timestamp The corresponding serialization class is DateCodec
Click to continue to watch , It is obvious to find some real implementation logic
Here are some if else To realize some inheritance Date The serialization operation of some classes of class , among WriteDateUseDateFormat WriteClassName UseISO8601DateFormat these SerializerFeature Enumeration classes play a very important role , At this time, we finally found that fastjson about date The implementation class of has special serialization operations , Here we need to make some special configurations to complete toJSONString The implementation of the .
2.3 Solution Research
1. encounter date Implementation class of , Own business use related tostring Method , Do not use native types , Use string replace
2. configuration-dependent SerializerFeature Use fastjson Class of
- The comparison of the two schemes
3.1 encounter date Implementation class of , Own business use related tostring Method , Do not use native types , Use string replace
Execution results :
The elapsed time : 98ms
3.2 configuration-dependent SerializerFeature Use fastjson Class of
Execution results :
The elapsed time :113ms
- Conclusion
fastjson There is no advantage in dealing with time types , We are using fastjson Pay attention to when printing time type , In the course of business , If you have requirements for time type, you'd better convert it yourself , In this way, the output is intuitive and easy to control , Time zone processing is also more flexible , The threshold is still low . Of course, this is only my personal opinion , There are different opinions to exchange .
边栏推荐
- Collation of ml.net related resources
- Simple calculator wechat applet project source code
- Unity shader learning (VI) achieving radar scanning effect
- Low cost 2.4GHz wireless transceiver chip -- ci24r1
- Detailed steps of installing MySQL 5.7 for windows
- 110道 MySQL面试题及答案 (持续更新)
- Alibaba political commissar system - Chapter III, Alibaba political commissar and cultural docking
- Proteus simulation based on msp430f2491 (realize water lamp)
- node:文件写入数据(readFile、writeFile),覆盖与增量两种模式
- User identity identification and account system practice
猜你喜欢
分段分页以及段页结合
01-01-osg GL3 environment setup
TCP - sliding window
node:文件写入数据(readFile、writeFile),覆盖与增量两种模式
Ga-rpn: recommended area network for guiding anchors
Solve the problem of MSVC2017 compiler with yellow exclamation mark in kits component of QT
Data warehouse layered design and data synchronization,, 220728,,,,
pnpm install出现:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies
Day15: the file contains the vulnerability range manual (self use file include range)
QT learning: use non TS files such as json/xml to realize multilingual internationalization
随机推荐
Txt plain text operation
Application scheme of charging pile
Second week of postgraduate freshman training: convolutional neural network foundation
Simple calculator wechat applet project source code
用户身份标识与账号体系实践
Day5: PHP simple syntax and usage
Unity shader learning (VI) achieving radar scanning effect
Windows 安装 MySQL 5.7详细步骤
数字人民币时代隐私更安全
谷歌浏览器免跨域配置
2022 spsspro certification cup mathematical modeling problem B phase II scheme and post game summary
Cs5340 domestic alternative dp5340 multi bit audio a/d converter
Lora opens a new era of Internet of things -asr6500s, asr6501/6502, asr6505, asr6601
Osgsimplegl3 combined with renderdoc tool
Qpalette learning notes
GBase 8s数据库有哪些备份恢复方式
ROS common instructions
Proteus simulation based on msp430f2491
Normal visualization
Time function in MySQL