简体   繁体   English

为什么作者在 Room 数据库中的某个字段中添加了 @field:SerializedName 注解?

[英]Why does the author add @field:SerializedName annotation to a field in Room database?

I'm learning Room with a sample code at https://github.com/googlecodelabs/android-paging我正在使用https://github.com/googlecodelabs/android-paging的示例代码学习 Room

The Code A is to defind a @Entity of a table, you can see it .代码A是定义一个表的@Entity ,你可以看到

Normally it add the @ColumnInfo annotation to a field, such as @ColumnInfo(name = "first_name") val firstName: String?, ... .通常它会将@ColumnInfo注释添加到字段中,例如@ColumnInfo(name = "first_name") val firstName: String?, ...

Why does the author add @field:SerializedName annotation to a field in Room database?为什么作者在 Room 数据库中的某个字段中添加了@field:SerializedName注解?

Code A代码 A

@Entity(tableName = "repos")
data class Repo(
    @PrimaryKey @field:SerializedName("id") val id: Long,
    @field:SerializedName("name") val name: String,
    @field:SerializedName("full_name") val fullName: String,
    @field:SerializedName("description") val description: String?,
    @field:SerializedName("html_url") val url: String,
    @field:SerializedName("stargazers_count") val stars: Int,
    @field:SerializedName("forks_count") val forks: Int,
    @field:SerializedName("language") val language: String?
)

The @SerializedName annotation is part of Gson and is used to define the names of these properties in Json . @SerializedName注释是Gson 的一部分,用于在Json 中定义这些属性的名称。

The @ColumnInfo annotation is part of Room and is used to define the column names of these properties in SQLite . @ColumnInfo注释是Room 的一部分,用于在SQLite 中定义这些属性的列名。

If neither of these annotation is defined, the property/field name is used as a name for the Json property or column.如果这些注释均未定义,则属性/字段名称将用作Json属性或列的名称。

Why does the author add @field:SerializedName annotation to a field in Room database?为什么作者在 Room 数据库中的某个字段中添加了 @field:SerializedName 注解?

Because the Room entity is reused as a Gson DTO.因为Room实体被重用为Gson DTO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM