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.
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
@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?
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.