当前位置:网站首页>@JsonAdapter注解使用
@JsonAdapter注解使用
2022-07-05 09:41:00 【沙漠一只雕得儿得儿】
接管序列化和反序列化过程
TypeAdapter
接管了序列化和反序列化过程,而JsonSerializer
和JsonDeserializer
则只分别单独接管序列化和反序列化过程。
作用在POJO类上的,接收一个参数,且必须是TypeAdpater,JsonSerializer或JsonDeserializer这三个其中之一。
前面的JsonSerializer
和JsonDeserializer
都要配合GsonBuilder.registerTypeAdapter
使用,但每次使用都要注册也太麻烦了,JsonAdapter
就是为了解决这个痛点的。
使用:
@JsonAdapter(UserTypeAdapter.class)
public final class User {
.....
}
public class UserTypeAdapter implements JsonSerializer<User> {
@Override
public JsonElement serialize(User src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jsonElement = new JsonObject();
jsonElement.addProperty("name","serialize");
return jsonElement;
}
}
User user = new User("我",10);
Log.i("adapter","user:"+new Gson().toJson(user));//直接使用简单的new GSON()即可,无需配置注册
边栏推荐
- Apache DolphinScheduler 系统架构设计
- Single chip microcomputer principle and Interface Technology (esp8266/esp32) machine human draft
- Cerebral Cortex:有向脑连接识别帕金森病中广泛存在的功能网络异常
- Using directive in angualr2 to realize that the picture size changes with the window size
- ThreadLocal source code learning
- How to correctly evaluate video image quality
- The king of pirated Dall · e? 50000 images per day, crowded hugging face server, and openai ordered to change its name
- Swift set pickerview to white on black background
- 【 conseils 】 obtenir les valeurs des axes X et y de la fonction cdfplot dans MATLAB
- 盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名
猜你喜欢
QT timer realizes dynamic display of pictures
Understand the window query function of tdengine in one article
How to get the STW (pause) time of GC (garbage collector)?
Charm of code language
Baidu app's continuous integration practice based on pipeline as code
Design of stepping motor controller based on single chip microcomputer (forward rotation and reverse rotation indicator gear)
ArcGIS Pro 创建要素
Node-RED系列(二九):使用slider与chart节点来实现双折线时间序列图
从“化学家”到开发者,从甲骨文到 TDengine,我人生的两次重要抉择
Is it really reliable for AI to make complex decisions for enterprises? Participate in the live broadcast, Dr. Stanford to share his choice | qubit · viewpoint
随机推荐
Kotlin compose and native nesting
解决Navicat激活、注册时候出现No All Pattern Found的问题
A high density 256 channel electrode cap for dry EEG
Design and exploration of Baidu comment Center
Fluent development: setting method of left and right alignment of child controls in row
MySQL字符类型学习笔记
Viewpager pageradapter notifydatasetchanged invalid problem
B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条...
从“化学家”到开发者,从甲骨文到 TDengine,我人生的两次重要抉择
Wechat applet - simple diet recommendation (2)
C#函数返回多个值方法
Cerebral cortex: directed brain connection recognition widespread functional network abnormalities in Parkinson's disease
Tianlong Babu TLBB series - questions about skill cooling and the number of attack ranges
善用兵者,藏于无形,90 分钟深度讲解最佳推广价值作品
How to get the STW (pause) time of GC (garbage collector)?
程序员搞开源,读什么书最合适?
Single chip microcomputer principle and Interface Technology (esp8266/esp32) machine human draft
【小技巧】獲取matlab中cdfplot函數的x軸,y軸的數值
Oracle combines multiple rows of data into one row of data
搞数据库是不是越老越吃香?