I use Google Room Persistence Library for saving data in database. in Room there is an annotation (@Embedded) :
you can use the @Embedded annotation to represent an object that you'd like to decompose into its subfields within a table. You can then query the embedded fields just as you would for other individual columns
@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
}
I use Gson to deserialize json string from REST API, I want Gson to deserialize GeneralInfo
fields into MyObject
fields directly. How can I do this?
I want Gson to deserialize MyObject
s like this:
{
objectId : 1
wellId : 1
ownerName : "Me"
}
NOT this
{
generalInfo : {
wellId : 1
ownerName : "Me"
}
objectId : 1
}
Is there any way other than using JsonAdapter
? I can write my own convertToJson
and convertFromJson
but I want to use Gson and it's better to use annotation to tell Gson "don't deserialize this embeddede object to a jsonObject, insert its field in its parent json fields"