Wordpress Rest Api kotlin 数据中的类别序列化 class

Wordpress Rest Api categories serialization in kotlin data class

我有一个带有 android 应用程序的项目,可以从 wordpress rest api 中获取 post 我参加的一些领域就像这个列表。

[
  {
    "id": 43600,
    "date": "2020-09-07T19:52:47",
    "title": {
      "rendered": "Video: .... "
    },
    "content": {
      "rendered": "<div class=\"wp-block-embed__wrapper\"></div>",
      "protected": false
    },
    "author": 31,
    "featured_media": 43601,
    "categories": [
      788,
      2760
    ]
  }
]

已读:

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

最接近的答案,但主要在转换器中

https://medium.com/@gilesjeremydev/room-through-a-complete-example-ce5c9ed417ba

我试图将它保存到本地存储中,并在单个实体中留出空间。但是基于 google 文档,它会分成 2 个与注释 @relation

相关联的实体
@Entity(tableName = "post")
data class SomePost(

    @PrimaryKey
    @field:SerializedName("id")
    val id: Int,
    @field:SerializedName("date")
    val date: String,
    @Embedded
    @field:SerializedName("title")
    val title: PostTitle,
    @Embedded
    @field:SerializedName("content")
    val content: PostContent,

    @field:SerializedName("featured_media")
    val imageId: Int,
    @field:SerializedName("author")
    val author: Int

)

@Entity
data class PostCategories(

    @PrimaryKey(autoGenerate = true)
    val id: Int,
    @field:SerializedName("categories")
    val postCategories: Int

)

data class SomePostRelationship (
    @Embedded
    var post: SomePost? = null,

    @Relation(
        parentColumn = "id",
        entityColumn = "categories"
    )
    var categories: List<PostCategories>? = null
)

interface PostService {

    companion object {
        const val ENDPOINT = "https://example.com/wp-json/"
    }

    // Posts
    @GET("wp/v2/posts/")
    suspend fun getPostAll(
        @Query("page") page: Int? = null,
        @Query("per_page") perPage: Int? = null,
        @Query("search") search: String? = null,
        @Query("order") order: String? = null
    ): Response<List<somePost>>

问题是数据 class PostCategories。

我的问题是如何将 json 数组序列化为 android 房间类别的实体数据 class。

如果已经有答案或相同的问题希望可以link。

尝试了一些变化之后。

我决定暂时使用转换器并将其合并到数据类中。

@Entity(tableName = "post")
data class SomePost(

...

    @field:SerializedName("categories")
    var categories: ArrayList<Int>? = null,
...
)


class Converters {

...
    @TypeConverter
    fun listToInt(value: ArrayList<Int>?): String? {
        return Gson().toJson(value)
    }

    @TypeConverter
    fun intToList(value: String?): ArrayList<Int>? {
        val type = object : TypeToken<ArrayList<Int>?>() {}.type
        return Gson().fromJson(value, type)
    }

}