【问题标题】:Gson deserialization for Realm list of primitivesRealm 原语列表的 Gson 反序列化
【发布时间】:2015-04-07 13:44:07
【问题描述】:

我正在使用带有 gson 的领域。我有一个模式,它有一个 int 类型字段的列表。 Realm 目前不支持原语列表。为了解决这个问题,有一个解决方案。我创建了我的 RealmInt 类。

import io.realm.RealmObject;

public class RealmInt extends RealmObject {
    private int val;

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }
}

我有一个类似的大模态对象..

public class Product extends RealmObject {
    @PrimaryKey
    private int productID;
    private int priority;
    private boolean isFavourite;
    .....
    .....
    .....
    private RealmList<Document> documents;
    private RealmList<ProductInfoGroup> productInfoGroups;
    private RealmList<RealmInt> categories;

我必须将下面的 json 数组反序列化为产品模式。

[{
        "productID": 776,
        "categories": [
            35
        ],
        "name": "",
        "priority": 3,
        ......
        "status": 2,
        "documents": [
            {
                "documentID": 74,
                "productID": 776,
                "name": null,
                ....
                "isDefault": true
            }
        ],
        "productInfoGroups": [
            {
                "productInfoGroupID": 1575,
                "productID": 776,
                .....
                "productInfos": [
                    {
                        "productInfoID": 2707,
                        "productInfoGroupID": 1575,
                        "title": "",
                        ...
                    },
                    {
                        "productInfoID": 2708,
                        "productInfoGroupID": 1575,
                        ...
                    },
                    {
                        "productInfoID": 2709,
                        .....
                    }
                ]
            }
        ],
        "lastUpdateDate": 130644319676570000,
        "isActive": true
    },....]

有一个解决方案here,但它不适用于大物体。我只需要更改类别数组,其他反序列化必须默认完成 gson 反序列化。

【问题讨论】:

  • 你找到解决办法了吗?

标签: android gson realm


【解决方案1】:

您必须为每个不同于 JSON 表示的变量指定一个自定义类型适配器。所有其他对象都会自动处理。在您的情况下,它只是 categories 变量,因为其余变量应自动映射。

JSON:

[
    { "name"  : "Foo",
      "ints" : [1, 2, 3]
    },
    { "name"  : "Bar",
      "ints" : []
    }
]  

模型类:

public class RealmInt extends RealmObject {
    private int val;

    public RealmInt() {
    }

    public RealmInt(int val) {
        this.val = val;
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }
}

public class Product extends RealmObject {

    private String name;
    private RealmList<RealmInt> ints;

    // Getters and setters
}

GSON 配置:

Gson gson = new GsonBuilder()
        .setExclusionStrategies(new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                return f.getDeclaringClass().equals(RealmObject.class);
            }

            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        })
        .registerTypeAdapter(new TypeToken<RealmList<RealmInt>>() {}.getType(), new TypeAdapter<RealmList<RealmInt>>() {

            @Override
            public void write(JsonWriter out, RealmList<RealmInt> value) throws IOException {
                // Ignore
            }

            @Override
            public RealmList<RealmInt> read(JsonReader in) throws IOException {
                RealmList<RealmInt> list = new RealmList<RealmInt>();
                in.beginArray();
                while (in.hasNext()) {
                    list.add(new RealmInt(in.nextInt()));
                }
                in.endArray();
                return list;
            }
        })
        .create();

JsonElement json = new JsonParser().parse(new InputStreamReader(stream));
List<Product> cities = gson.fromJson(json, new TypeToken<List<Product>>(){}.getType());

如果你有多个像 RealmInt 这样的包装变量,你需要为每个变量定义一个 TypeAdapter。另请注意,上面的 TypeAdapter 总是会遇到一个数组。如果您的 JSON 可能发送 null 而不是 [],您将需要在 TypeAdapter 中进行额外检查。

【讨论】:

  • @christian 我正在使用改造从服务器获取数据。我的对象包含一些字段,包括另一个自定义对象的 RealmList。那么,在这里,如何使用将这样的数据放入 realmList 中?
猜你喜欢
  • 1970-01-01
  • 2011-12-19
  • 1970-01-01
  • 1970-01-01
  • 2017-01-08
  • 2013-08-21
  • 2015-07-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多