使用JSON反序列化器,如何更改超类字段的映射?

时间:2021-12-01 03:45:06

I'm managing a database using an abstract class which has a bunch of handy convenience methods. In particular, it has insert(Context), update(Context) and delete(Context) which means you can create a bunch of different objects that inherit this class, all of which have their own database mappings, and just update or insert them one by one.

我正在使用一个抽象类管理一个数据库,它有很多方便的方法。特别是,它有insert(Context)、update(Context)和delete(Context),这意味着您可以创建一堆继承这个类的不同对象,这些对象都有自己的数据库映射,并逐个更新或插入它们。

Another developer has added GSON deserialization markup to this structure. This is working fine, except that the update function relies on comparing objects' unique identifiers; the particular thing that's used as an identifier varies from object to object. This means that while the superclass, which has the update(...) method in, has

另一个开发人员向这个结构添加了GSON反序列化标记。除了更新函数依赖于比较对象的唯一标识符之外,这一功能运行良好;作为标识符使用的特定对象因对象而异。这意味着,当超类有更新(…)方法时,它有。

@SerializedName("id")
protected String mUUID;

the subclass would need a different mapping.

子类需要不同的映射。

Currently I'm solving this by overriding the update(...) method in the subclass to compare using a different identifying field, but this is pretty unsatisfying. Is it possible to map a different serialized name to a superclass field in GSON, and if so, how do you do it?

目前,我正在通过重写子类中的update(…)方法来解决这个问题,以便比较使用不同的标识字段,但这是非常不令人满意的。是否有可能将不同的序列化名称映射到GSON中的超类字段,如果有,如何实现?

To clarify, the class structure is roughly

要澄清,类结构大致是这样的。

public abstract class DatabasePersistent {
    //...
    @SerializedName("id")
    protected String mUUID;
    //...
    public int update(Context context) {
        return context.getContentResolver().update(
                getContentProviderURI(), //abstract, allows objects to update the right table 
                toContentValues(), //abstract, converts the object to a ContentValues
                UUID_EQUALS, //this is a where clause string
                new String[]{mUUID}
        );
    }
}

I want to make a

我想做一个

public class Thing extends DatabasePersistent {
    //all the abstract implementations
    //and so on
}

which somehow changes the mapping of mUUID so I can use the same primary ID machinery as in the superclass (eg the update method) but have different fields in the incoming JSON identify different objects.

它以某种方式改变了mUUID的映射,因此我可以使用与超类中相同的主ID机制(如update方法),但是在传入的JSON中有不同的字段来标识不同的对象。

1 个解决方案

#1


1  

I take it Thing will have attributes, like String customID, String foo, String bar that are mapped from the incoming JSON?

它会有属性,比如字符串customID,字符串foo,字符串bar这些都是从传入JSON中映射出来的?

If you could somehow override the setter for each of those Thing member vars - perhaps a lifecycle event, or perhaps if you provide a setCustomID(String customID) in your class GSON will use it vs. the annotation? then you can simply call super.setId(customID).

如果您能够以某种方式覆盖每个成员vars的setter—可能是一个生命周期事件,或者如果您在您的类中提供一个setCustomID(String customID),那么GSON将使用它与注释?然后您可以简单地调用super.setId(customID)。

This is like a "dynamic" or "calculated" field.

这就像一个“动态”或“计算”字段。

This post suggests GSON are unlikely to support property set/get though.

这篇文章暗示GSON不太可能支持属性设置/get。

Why does GSON use fields and not getters/setters?

为什么GSON使用字段而不是getter /setter ?

#1


1  

I take it Thing will have attributes, like String customID, String foo, String bar that are mapped from the incoming JSON?

它会有属性,比如字符串customID,字符串foo,字符串bar这些都是从传入JSON中映射出来的?

If you could somehow override the setter for each of those Thing member vars - perhaps a lifecycle event, or perhaps if you provide a setCustomID(String customID) in your class GSON will use it vs. the annotation? then you can simply call super.setId(customID).

如果您能够以某种方式覆盖每个成员vars的setter—可能是一个生命周期事件,或者如果您在您的类中提供一个setCustomID(String customID),那么GSON将使用它与注释?然后您可以简单地调用super.setId(customID)。

This is like a "dynamic" or "calculated" field.

这就像一个“动态”或“计算”字段。

This post suggests GSON are unlikely to support property set/get though.

这篇文章暗示GSON不太可能支持属性设置/get。

Why does GSON use fields and not getters/setters?

为什么GSON使用字段而不是getter /setter ?