当我在字段中使用@Json时,序列化不能正常发生,但是它在更改为@field:Json后开始工作.

I came through this change after reading some bug thread and I think this is specific to kotlin. I would like to know what difference does @field:Json bring and is it really specific to kotlin?

推荐答案

Whatever you put between @ and : in your annotation specifies the exact target for your Annotation.

When using Kotlin with JVM there is a substantial number of things generated, therefore your Annotation could be put in many places. If you don't specify a target you're letting the Kotlin compiler choose where the Annotation should be put. When you specify the target -> you're in charge.

为了更好地了解差异,您应该在IntelliJ/Android Studio中判断Kotlin字节码的反编译Java代码.


Example kotlin code:

class Example {

    @ExampleAnnotation
    val a: String = TODO()

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

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

Decompiled Java code:

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));
   }
}

For more info go to 100.

Kotlin相关问答推荐

如何使用具有名称冲突的限定这个?

Kotlin是否针对范围和进度优化sum()?

Kotlin 海峡没有结束

Kotlin中一个接口的实现问题

如何修改muableStateMapOf的值?

在 Kotlin 中将两个字节转换为 UIn16

使用调度程序运行异步 Kotlin 代码

如何在 Jetpack Compose 中启动和停止动画

PRDownloader 即使在实现库后也无法工作.未知参考:Android Studio 中的 PRDownloader

为空数组添加值

错误:cannot find symbol import com.gourav.news.databinding.ActivityDetailBindingImpl;

Saripaar formvalidation 在 kotlin 中第二次不起作用

Kotlin 无法找到或加载主类

`this@classname` 在 Kotlin 中是什么意思?

什么是开放式property?为什么我不能将其设置器设为private私有?

Android Jetpack导航,另一个主机片段中的主机片段

(kotlin的Moshi)@Json vs@field:Json

Gradle:无法连接到 Windows 上的 Kotlin 守护程序

如何根据ArrayList的对象属性值从中获取最小/最大值?

从 java 活动 *.java 启动 kotlin 活动 *.kt?