android - 是否可以使用 gson 中的注释设置默认值?
问题描述
是否可以使用注释为 Gson 属性设置默认值?我想在反序列化/序列化之后使用这个默认值
@SerializedName(key = "a", defaultValue = "hi")
public String a;
@SerializedName("b")
public String b;
解决方案
如果变量为 null,您可以在 setter 方法中设置默认值
@SerializedName("yourVariable")
public String yourVariable;
public String getYourVariable(){
if(yourVariable==null){
return defaultValue;
}
return yourVariable;
}
推荐阅读
- excel - 使用 Excel 或 VBA 在 excel 中比较 2 个列表
- node.js - MERN 部署到 Heroku
- azure - 通过 azure 管道单阶段将包部署到多个 webapp 服务
- java - 如何确保正在阅读我的 else 语句?-Java 的全新
- flutter - 在小部件 Flutter 中预加载以前保存的图像
- flutter - NoSuchMethodError:在 null 上调用了方法“[]”。颤动本地json文件
- python - UnboundLocalError: local variable.... 赋值前引用
- java - Android Studio TextView 文本选择处理程序图标位置
- c# - 如何同步调用或等待异步回调完成?
- python - 获取 python 退出状态并在 bash 命令中使用它