(科特琳的莫西)@杰森vs @field:杰森

时间:2019-07-10 18:29:30

标签: json kotlin moshi

当我在字段中使用 @Json 时,

串行化无法正确进行,但是在更改为 @field:Json 后,它才开始工作。

我在阅读了一些bug线程后经历了这一更改,我认为这是kotlin特有的。我想知道@field:Json带来了什么不同,它真的是Kotlin特有的吗?

1 个答案:

答案 0 :(得分:1)

无论您在注释中的@:之间放置什么,都将为您的注释指定确切的target

将Kotlin与JVM一起使用时,会生成大量东西,因此您的注释可以放在很多地方。如果您未指定target,则让Kotlin编译器选择应在何处放置注释。指定target->由您负责。

要更好地了解它们之间的区别,您应该在IntelliJ / Android Studio中检查Kotlin字节码的反编译Java代码。


kotlin代码示例:

class Example {

    @ExampleAnnotation
    val a: String = TODO()

    @get:ExampleAnnotation
    val b: String = TODO()

    @field:ExampleAnnotation
    val c: String = TODO()
}

反编译的Java代码:

public final class Example {
   @NotNull
   private final String a;
   @NotNull
   private final String b;
   @ExampleAnnotation
   @NotNull
   private final String c;

   /** @deprecated */
   // $FF: synthetic method
   @ExampleAnnotation
   public static void a$annotations() {
   }

   @NotNull
   public final String getA() {
      return this.a;
   }

   @ExampleAnnotation
   @NotNull
   public final String getB() {
      return this.b;
   }

   @NotNull
   public final String getC() {
      return this.c;
   }

   public Example() {
      boolean var1 = false;
      throw (Throwable)(new NotImplementedError((String)null, 1, (DefaultConstructorMarker)null));
   }
}

有关更多信息,请访问Kotlin docs