莫西在科特林无视领域

hei*_*sen 14 json ignore serializer kotlin moshi

我想知道在使用Moshi时如何忽略Kotlin类字段.

我找到了这个Java(Moshi忽略字段)的答案,它表明使用关键字transient如下

private transient String your_variable_name;
Run Code Online (Sandbox Code Playgroud)

但我找不到在Kotlin完成这项工作的正确方法.

mar*_*ran 22

使用@Transient注释.

@Transient
private val your_variable_name: String
Run Code Online (Sandbox Code Playgroud)

Doc在这里:https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-transient/index.html

  • 当预期的字段没有出现在 JSON 响应中时,这不起作用。 (2认同)
  • 此外,请确保提供默认值。否则,它可能会抛出错误。 (2认同)