(科特琳的莫希)@Json vs @field:Json [英] (Moshi in kotlin) @Json vs @field:Json

查看:91
本文介绍了(科特琳的莫希)@Json vs @field:Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Serialization does not happen properly when I use @Json in the fields but it started working after changing to @field:Json.

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

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?

推荐答案

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

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

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

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代码.

To better see the difference you should inspect the decompiled Java code of the Kotlin Bytecode in IntelliJ/Android Studio.

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


有关更多信息,请访问科林文档.


For more info go to Kotlin docs.

这篇关于(科特琳的莫希)@Json vs @field:Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆