当前位置:网站首页>@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()即可,无需配置注册边栏推荐
- 历史上的今天:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生...
- Pagoda panel MySQL cannot be started
- To bring Euler's innovation to the world, SUSE should be the guide
- Wechat applet - simple diet recommendation (3)
- Unity粒子特效系列-毒液喷射预制体做好了,unitypackage包直接用 - 上
- Android SQLite database encryption
- Small program startup performance optimization practice
- How to get the STW (pause) time of GC (garbage collector)?
- [NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
- Develop and implement movie recommendation applet based on wechat cloud
猜你喜欢

【小技巧】獲取matlab中cdfplot函數的x軸,y軸的數值

Solve liquibase – waiting for changelog lock Cause database deadlock

剪掉ImageNet 20%数据量,模型性能不下降!Meta斯坦福等提出新方法,用知识蒸馏给数据集瘦身...

Analysis on the wallet system architecture of Baidu trading platform

一种用于干式脑电图的高密度256通道电极帽

TDengine 已经支持工业英特尔 边缘洞见软件包

Tdengine already supports the industrial Intel edge insight package

盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名

Those who are good at using soldiers, hide in the invisible, and explain the best promotional value works in depth in 90 minutes

(1) Complete the new construction of station in Niagara vykon N4 supervisor 4.8 software
随机推荐
La voie de l'évolution du système intelligent d'inspection et d'ordonnancement des petites procédures de Baidu
Matrix processing practice
oracle 多行数据合并成一行数据
Tongweb set gzip
解决Navicat激活、注册时候出现No All Pattern Found的问题
Online chain offline integrated chain store e-commerce solution
百度智能小程序巡檢調度方案演進之路
MySQL数字类型学习笔记
Tutorial on building a framework for middle office business system
Baidu app's continuous integration practice based on pipeline as code
90%的人都不懂的泛型,泛型的缺陷和应用场景
RMS TO EAP通过MQTT简单实现
From "chemist" to developer, from Oracle to tdengine, two important choices in my life
Single chip microcomputer principle and Interface Technology (esp8266/esp32) machine human draft
【系统设计】指标监控和告警系统
Tdengine can read and write through dataX, a data synchronization tool
TDengine 已经支持工业英特尔 边缘洞见软件包
H. 265 introduction to coding principles
【小技巧】獲取matlab中cdfplot函數的x軸,y軸的數值
Comparison of batch merge between Oracle and MySQL