3

So, I have this JSON Object

{ 
"name": "News",
"infos":[
   { "title":"hello",
     "content":"this is the content",
     "recent":true
   },
   { "title":"Tax",
     "content":"The European Commission is considering possible tax benefits",
     "recent":true
   },
   { "title":"Tax",
     "content":"The European Commission is considering possible tax benefits",
   }
]

I need to persist this object with Room(Android) using moshi to deserialize this object into my data classes which are

@JsonClass(generateAdapter = true)
@Entity(
    tableName = "news"
)

data class News(
    @ColumnInfo(name = "name")
    val name:String = "News",

    val infos:List<Info>,

    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    val id: Int){
}

@JsonClass(generateAdapter = true)
@Entity(tableName = "info",
    foreignKeys = [
        ForeignKey(entity = News::class, parentColumns = ["id"], childColumns = ["info_id"])
    ],
    indices = [Index("info_id")])
data class Info(
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "id")
    var id: Int,
    val title : String,
    val content: String,
    val recent: Boolean?,
    @ColumnInfo(name = "info_id")
    val infoId: Int) {

}

I have also created the Roomd Database and my @dao class @Insert fun. But I get this error :

error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
    private final java.util.List<com.example.data.Info> infos = null;
Ape04
  • 53
  • 7

1 Answers1

1

You have TypeConverters missing. Room cannot save complex field types like list, date, arrays etc.

Please refer : https://developer.android.com/training/data-storage/room/referencing-data

tipsyBit
  • 29
  • 4
  • Welcome to SO! Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) Please consider adding code to make your answer clearer – Tomer Shetah Oct 13 '20 at 10:10
  • sure, thanks. There are lot of answers available on this topic. this question should be closed, i guess. – tipsyBit Oct 13 '20 at 19:02
  • If you find a duplicate, you can flag this question which will vote to close as a duplicate. – Tomer Shetah Oct 13 '20 at 19:05