我使用Google Room Persistence Library来保存数据库中的数据 . 在Room中有一个注释(@Embedded):
您可以使用@Embedded批注来表示您想要分解到表中子字段的对象 . 然后,您可以像查询其他单个列一样查询嵌入字段
@Entity
public class MyObject {
// nested class
public class GeneralInfo {
public String ownerName;
@PrimaryKey
public long wellId;
}
@Embedded
public GeneralInfo generalInfo;
public long objectId;
// other fields
}
我使用Gson从REST API反序列化json字符串,我希望Gson直接将 GeneralInfo
字段反序列化为 MyObject
字段 . 我怎样才能做到这一点?
我希望Gson像这样反序列化 MyObject
:
{
objectId : 1
wellId : 1
ownerName : "Me"
}
NOT 这个
{
generalInfo : {
wellId : 1
ownerName : "Me"
}
objectId : 1
}
除了使用 JsonAdapter
之外还有什么方法吗?我可以写自己的 convertToJson
和 convertFromJson
,但我想使用Gson,最好使用注释来告诉Gson "don't deserialize this embeddede object to a jsonObject, insert its field in its parent json fields"