使用gson进行反序列化时,将默认值设置为变量 [英] Setting Default value to a variable when deserializing using gson

查看:1055
本文介绍了使用gson进行反序列化时,将默认值设置为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将 JSON 转换为Java对象。当一对值的某个值为 null 时,它应该设置一些默认值。



这是我的 POJO

 公共班级学生{
String rollNo;
字符串名称;
字符串联系人;
字符串学校;

public String getRollNo(){
return rollNo;
}
public void setRollNo(String rollNo){
this.rollNo = rollNo;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getSchool(){
return school;
}
public void setSchool(String school){
this.school = school;


示例JSON对象:

  {
rollNo:123,name:Tony,school:null
}

所以如果学校 null ,我应该做这个转换为默认值,例如school:XXX。如何在反序列化对象时使用 Gson 进行配置?

解决方案

如果 null 在JSON中,Gson将会覆盖您可能在POJO中设置的任何默认值。您可能无法创建 custom deserializer ,但在这种情况下这可能是矫枉过正的。



我认为最简单的(并且可以说最适合您的用例)相当于延迟加载。例如:

  private static final String DEFAULT_SCHOOL =ABC Elementary; 
public String getSchool(){
if(school == null)school == DEFAULT_SCHOOL;
返校;
}
public void setSchool(String school){
if(school == null)this.school = DEFAULT_SCHOOL;
else this.school = school;

code $


$ p $注意: 这个解决方案的一个大问题是,为了更改默认值,您必须更改代码。如果您希望默认值可以自定义,那么您应该使用上面链接的自定义解串器。


I am trying to convert JSON to Java object. When a certain value of a pair is null, it should be set with some default value.

Here is my POJO:

public class Student {      
    String rollNo;
    String name;
    String contact;
    String school;

    public String getRollNo() {
        return rollNo;
    }
    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSchool() {
        return school;
    }
    public void setSchool(String school) {
        this.school = school;
    }
}

Example JSON object:

{
  "rollNo":"123", "name":"Tony", "school":null
}

So if school is null, I should make this into a default value, such as "school":"XXX". How can I configure this with Gson while deserializing the objects?

解决方案

If the null is in the JSON, Gson is going to override any defaults you might set in the POJO. You could go to the trouble of creating a custom deserializer, but that might be overkill in this case.

I think the easiest (and, arguably best given your use case) thing to do is the equivalent of Lazy Loading. For example:

private static final String DEFAULT_SCHOOL = "ABC Elementary";
public String getSchool() {
    if (school == null) school == DEFAULT_SCHOOL;
    return school;
}
public void setSchool(String school) {
    if (school == null) this.school = DEFAULT_SCHOOL;
    else this.school = school;
}


Note: The big problem with this solution is that in order to change the Defaults, you have to change the code. If you want the default value to be customizable, you should go with the custom deserializer as linked above.

这篇关于使用gson进行反序列化时,将默认值设置为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆