gpt4 book ai didi

kotlin - 使用@field :SerializedName annotation instead of @SerializedName?的目的是什么

转载 作者:行者123 更新时间:2023-12-02 12:16:00 27 4
gpt4 key购买 nike

通常,我使用@SerializedName批注来映射JSON字段。但是,在Google Architecture Component示例项目中,我看到它们使用了@field:SerializedName批注,并且找不到任何地方可以读取使用该@field批注的目的。
我是Kotlin的新手,有人可以解释或共享要阅读的ref链接吗?谢谢你。

@Entity(
indices = [
Index("id"),
Index("owner_login")],
primaryKeys = ["name", "owner_login"]
)
data class Repo(
val id: Int,
@field:SerializedName("name")
val name: String,
@field:SerializedName("full_name")
val fullName: String,
@field:SerializedName("description")
val description: String?,
@field:SerializedName("owner")
@field:Embedded(prefix = "owner_")
val owner: Owner,
@field:SerializedName("stargazers_count")
val stars: Int
) {...

最佳答案

为简单起见,假设您有:

data class Repo(
@field:SerializedName("name")
var name: String
)
这是在声明 data class,并在 properties中声明了类的 primary constructor。 Kotlin属性被编译为字段,getter方法以及(如果是可变的(即 var))setter方法。由于该属性是在主要构造函数中声明的,因此它也将被编译为构造函数的参数。这意味着在此上下文中可以放置注释的位置很多:Kotlin属性,支持字段,getter方法,构造函数参数,setter方法或setter参数。 field:明确指出将注释应用于属性的 backing field。从 the documentation:

#Annotation Use-site Targets

When you're annotating a property or a primary constructor parameter, there are multiple Java elements which are generated from the corresponding Kotlin element, and therefore multiple possible locations for the annotation in the generated Java bytecode. To specify how exactly the annotation should be generated, use the following syntax:

class Example(@field:Ann val foo,    // annotate Java field
@get:Ann val bar, // annotate Java getter
@param:Ann val quux) // annotate Java constructor parameter

The same syntax can be used to annotate the entire file. To do this, put an annotation with the target file at the top level of a file, before the package directive or before all imports if the file is in the default package:

@file:JvmName("Foo")
package org.jetbrains.demo

If you have multiple annotations with the same target, you can avoid repeating the target by adding brackets after the target and putting all the annotations inside the brackets:

class Example {
@set:[Inject VisibleForTesting]
var collaborator: Collaborator
}

The full list of supported use-site targets is:

  • file;
  • property (annotations with this target are not visible to Java);
  • field;
  • get (property getter);
  • set (property setter);
  • receiver (receiver parameter of an extension function or property);
  • param (constructor parameter);
  • setparam (property setter parameter);
  • delegate (the field storing the delegate instance for a delegated property).

To annotate the receiver parameter of an extension function, use the following syntax:

fun @receiver:Fancy String.myExtension() { ... }

If you don't specify a use-site target, the target is chosen according to the @Target annotation of the annotation being used. If there are multiple applicable targets, the first applicable target from the following list is used:

  • param;
  • property;
  • field.

关于kotlin - 使用@field :SerializedName annotation instead of @SerializedName?的目的是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59925099/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com