Suppose I have a service that returns some JSON where I know before hand some keys, but not the others.
I want to store the data in my room database such that the known properties on the JSON are properties on my entity, but the entire payload of unknown keys is also stored.
I can do it like so:
Model
@Entity
data class Whatever(
@PrimaryKey
val id: String,
val somethingElse: JsonObject?)
Usage
val gson = GsonBuilder()
.disableHtmlEscaping()
.create()
val json = "{\"id\":\"42\",\"extra\":[{\"z\":\"banana\"}]}"
val raw = gson.fromJson(this, JsonObject::class.java)
val model = gson.fromJson(this, Whatever::class.java)
model.somethingElse = raw
// save to database here
This works but it seems plenty inefficient to me (I need to do this to hundreds of thousands of records).
Is there a way to streamline it such that I only have to make one gson.fromJson() call?
Or, better yet, is there some other and better way to store my arbitrary data in Room.