Gson don't use default field value for objects (not primitives)

本小妞迷上赌 提交于 2021-01-27 19:27:34

问题


I use gson in android project and got problem. I want to set default value for list object if Api give me null value for this field. I don't modify this field any more so i don't want to check it every time for null value

So if be more detailed

I have for example this class

class Model {
    @Expose private int test = 1;
    @Expose private int primitive = 1;
    @Expose private List<String> strings = new ArrayList<>();
}

and json from Api

{
"test" : 12
}

so i expect what gson will generate object with fields:

Model obj = gson.fromJson(json, Model.class);
// obj.test == 12;
// obj.primitive = 1;
// obj.strings = [] empty string array

but gson override non primitive fields

Model obj = gson.fromJson(json, Model.class);
// obj.test == 12;
// obj.primitive = 1; // int is primitive and gson use default value
// obj.strings = null // here error

I checked source code and found what ReflectiveTypeAdapterFactory.BoundField use default value only for primitives.

@Override void read(JsonReader reader, Object value) throws IOException, IllegalAccessException {
    Object fieldValue = typeAdapter.read(reader);
    if (fieldValue != null || !isPrimitive) {
        field.set(value, fieldValue);
    }
}

so i'm asking if somebody know how fix this problem, and make gson return non null values for non primitives fields


回答1:


I created simple test which shows that Gson sets List<String> property to null only when is set to null in JSON payload. Below test app:

import com.google.gson.Gson;
import com.google.gson.annotations.Expose;

import java.util.ArrayList;
import java.util.List;

public class GsonApp {

    public static void main(String[] args) {
        Gson gson = new Gson();

        System.out.println(gson.fromJson("{}", Model.class));
        System.out.println(gson.fromJson("{\"strings\":[]}", Model.class));
        System.out.println(gson.fromJson("{\"strings\":[1,2]}", Model.class));
        System.out.println(gson.fromJson("{\"strings\":null}", Model.class));
        System.out.println(gson.fromJson("{\"test\":12}", Model.class));
    }
}

class Model {

    @Expose
    private int test = 1;

    @Expose
    private int primitive = 1;

    @Expose
    private List<String> strings = new ArrayList<>();

    @Override
    public String toString() {
        return "Model{" +
                "test=" + test +
                ", primitive=" + primitive +
                ", strings=" + strings +
                '}';
    }
}

prints:

Model{test=1, primitive=1, strings=[]}
Model{test=1, primitive=1, strings=[]}
Model{test=1, primitive=1, strings=[1, 2]}
Model{test=1, primitive=1, strings=null}
Model{test=12, primitive=1, strings=[]}

See also:

  • Gson uses TypeAdapter or Json Deserializer to convert data from an error list to an empty list


来源:https://stackoverflow.com/questions/55244705/gson-dont-use-default-field-value-for-objects-not-primitives

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!