当前位置:网站首页>@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()即可,无需配置注册
边栏推荐
- La voie de l'évolution du système intelligent d'inspection et d'ordonnancement des petites procédures de Baidu
- 90%的人都不懂的泛型,泛型的缺陷和应用场景
- Implementation of smart home project
- Tianlong Babu TLBB series - about items dropped from packages
- Mobile heterogeneous computing technology GPU OpenCL programming (Advanced)
- Wechat applet - simple diet recommendation (4)
- [NTIRE 2022]Residual Local Feature Network for Efficient Super-Resolution
- Are databases more popular as they get older?
- 天龙八部TLBB系列 - 关于包裹掉落的物品
- 美图炒币半年亏了3个亿,华为被曝在俄罗斯扩招,AlphaGo的同类又刷爆一种棋,今日更多大新闻在此...
猜你喜欢
Unity particle special effects series - the poison spray preform is ready, and the unitypackage package is directly used - on
【技术直播】如何用 VSCode 从 0 到 1 改写 TDengine 代码
Hard core, have you ever seen robots play "escape from the secret room"? (code attached)
RMS TO EAP通过MQTT简单实现
【C语言】动态内存开辟的使用『malloc』
Cross process communication Aidl
Application of data modeling based on wide table
The comparison of every() and some() in JS uses a power storage plan
【 conseils 】 obtenir les valeurs des axes X et y de la fonction cdfplot dans MATLAB
oracle 多行数据合并成一行数据
随机推荐
Why don't you recommend using products like mongodb to replace time series databases?
【 conseils 】 obtenir les valeurs des axes X et y de la fonction cdfplot dans MATLAB
Kotlin compose and native nesting
Those who are good at using soldiers, hide in the invisible, and explain the best promotional value works in depth in 90 minutes
QT VT100 parser
How to correctly evaluate video image quality
Node-RED系列(二九):使用slider与chart节点来实现双折线时间序列图
C#函数返回多个值方法
盗版DALL·E成梗图之王?日产5万张图像,挤爆抱抱脸服务器,OpenAI勒令改名
硬核,你见过机器人玩“密室逃脱”吗?(附代码)
Solve liquibase – waiting for changelog lock Cause database deadlock
Tdengine connector goes online Google Data Studio app store
RMS TO EAP通过MQTT简单实现
天龙八部TLBB系列 - 单体技能群伤
让AI替企业做复杂决策真的靠谱吗?参与直播,斯坦福博士来分享他的选择|量子位·视点...
Fluent development: setting method of left and right alignment of child controls in row
Tianlong Babu TLBB series - about items dropped from packages
Comparison of batch merge between Oracle and MySQL
Android SQLite database encryption
[app packaging error] to proceed, either fix the issues identified by lint, or modify your build script as follow