当前位置:网站首页>Using jsonserialize to realize data type conversion gracefully
Using jsonserialize to realize data type conversion gracefully
2022-04-23 03:20:00 【Small planes love traveling】
One 、
Requirements introduction : I hope that through annotation , Batch time format In the expected format .
JsonSerialize brief introduction :com.fasterxml.jackson.databind.annotation Serialization annotation provided by package
What is serialization :
Encode an object into a byte stream (I/O); The opposite operation is called deserialization .
The purpose of serialization is to facilitate the transfer of data and storage on disk ( Put one Java Objects are written to the hard disk or transferred to other computers on the network , At this time, we need to convert the object into a byte stream for network transmission . For this kind of general operation , Serialization appears to unify these formats )
Two 、 actual combat
1、 First, customize a class inheritance JsonSerializer To rewrite serialize Method , It implements the specific data type conversion logic
@Slf4j
public class DataFormatSerializeer extends JsonSerializer<String> {
@Override
public void serialize(String dateStr, JsonGenerator jsonGenerator,
SerializerProvider serializerProvider) throws IOException {
if (StringUtils.isNotBlank(dateStr)) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
Date date = null;
try {
date = dateFormat.parse(dateStr);
Timestamp time = new Timestamp(date.getTime());
jsonGenerator.writeString(DateUtils.convertSqlTimeStampToUtc(time, "UTC-4",
DateUtils.ISO8601_DATE_TIME_FORMAT));
} catch (ParseException e) {
log.error("date convert error");
}
} else {
jsonGenerator.writeString("");
}
}
}
2. application
@JsonSerialize(using = DataFormatSerializeer.class)// Here we refer to our custom class
private String date;
版权声明
本文为[Small planes love traveling]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220623382268.html
边栏推荐
- Course design of Database Principle -- material distribution management system
- [vs Code] solve the problem that the jupyter file displays exceptions in vs code
- Mise en service PID du moteur de codage (anneau de vitesse | anneau de position | suivant)
- . NETCORE sets the API post mode, which can accept parameters directly in parentheses
- Use of ADB command [1]
- 研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
- Find the number of leaf nodes of binary tree
- Why is bi so important to enterprises?
- 2022G2电站锅炉司炉考试题库及在线模拟考试
- ThreadLocal test multithreaded variable instance
猜你喜欢
可以接收多种数据类型参数——可变参数
Idea view history [file history and project history]
Quartz. Www. 18fu Used in net core
ASP. Net 6 middleware series - conditional Middleware
LoadRunner - performance testing tool
2022g2 boiler stoker examination question bank and online simulation examination
Web Course Design - his system
“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
搭建XAMPP时mysql端口被占用
MySql关键字GROUP_CONCAT,组合连接查询
随机推荐
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
oracle 查询外键含有逗号分隔的数据
Node configuration environment CMD does not take effect
js递归树结构计算每个节点的叶子节点的数量并且输出
Log4net is in Net core usage
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
Generate QR code through zxing
A set of combination boxing to create an idea eye protection scheme
Top 9 task management system in 2022
Knowledge of software testing~
一套组合拳,打造一款 IDEA 护眼方案
OLED multi-level menu record
WinForm allows the form form to switch between the front and active states
Swap the left and right of each node in a binary tree
你真的懂hashCode和equals吗???
Use of ADB command [1]
Idea view history [file history and project history]
New ORM framework -- Introduction to beetlsql
搭建XAMPP时mysql端口被占用
可以接收多种数据类型参数——可变参数