Хранение вложенного JSON объекта в Android Persistence Library - PullRequest
1 голос
/ 19 апреля 2020

из моей локальной службы Django Rest Framework Я получаю следующий вывод JSON:

{
    "count": 5,
    "next": null,
    "previous": null,
    "results": [
        {
            "id": 1,
            "created": "2020-04-18T16:00:16.060915Z",
            "name": "Germany",
            "groups": [
                {
                    "id": 1,
                    "created": "2020-04-18T16:03:11.138661Z",
                    "name": "MyGroup1",
                    "owner_id": 1
                },
                {
                    "id": 2,
                    "created": "2020-04-18T16:03:20.701660Z",
                    "name": "MyGroup2",
                    "owner_id": 1
                }, 
                ... 

Каждый Country может иметь много Group с. Для этого я создал следующие классы данных в своем проекте Android App:

@JsonClass(generateAdapter = true)
data class NetworkCountryContainer(
    val count: Long,
    val next: String?,
    val previous: String?,
    val results: List<Country>
)

@Entity(tableName = "country_table")
@JsonClass(generateAdapter = true)
data class Country(
    @PrimaryKey
    @Json(name="id")
    val countryId : Int,
    @Json(name="name")
    val countryName: String,
    @Json(name="groups")
    val groupList: List<Group>       // <--- this field causes the ERROR
)

@Entity(tableName = "group_table")
@JsonClass(generateAdapter = true)
data class Group(
    @PrimaryKey
    @Json(name="id")
    val groupId : Int,
    @Json(name="name")
    val groupName: String,
    @Json(name="owner_id")
    val ownerId: Int
)

Android Studio сообщает мне следующее:

Cannot figure out how to save this field into database. You can consider adding a type converter for it.

Зачем мне нужен TypeConverter? И как я могу построить один?

...