当前位置:网站首页>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 .
边栏推荐
- Hal learning notes - Advanced timer of 7 timer
- AES 双向加密解密工具
- Charging pile charging technology new energy charging pile development
- BiSeNet v2
- Stm32ff030 replaces domestic MCU dp32g030
- Data warehouse layered design and data synchronization,, 220728,,,,
- Normal visualization
- Thrift installation manual
- MFC integration QT verification and problem handling
- 2022 spsspro certification cup mathematical modeling problem B phase II scheme and post game summary
猜你喜欢

User identity identification and account system practice

Day5: PHP simple syntax and usage

Normal visualization

Inclination monitoring solution of Internet of things

TCP - sliding window

Google browser cross domain configuration free

Clickhouse learning (II) Clickhouse stand-alone installation

Back up Google or other browser plug-ins

Cv520 domestic replacement of ci521 13.56MHz contactless reader chip

Chrony 时间同步
随机推荐
Simulation of four way responder based on 51 single chip microcomputer
7.1-default-arguments
分段分页以及段页结合
6.3 references
Day6: use PHP to write file upload page
Alibaba political commissar system - Chapter III, Alibaba political commissar and cultural docking
6.2 function-parameters
Application of explosion-proof inclination sensor in safe operation of LNG
Ar virtual augmentation and reality
Thrift installation manual
Reading of false news detection papers (3): semi supervised content-based detection of misinformation via tensor embeddings
Low cost 2.4GHz wireless transceiver chip -- ci24r1
Selenium actual combat case crawling JS encrypted data
ROS common instructions
[from_bilibili_dr_can][[advanced control theory] 9_ State observer design] [learning record]
pnpm install出现:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies
New energy shared charging pile management and operation platform
ML.NET相关资源整理
OSG advanced sequence
Charging pile charging technology new energy charging pile development