Moshi ignores the field in Kotlin

I want to know how to ignore a Kotlin class field when using Moshi.

I found this answer for Java ( Moshi ignore field ), which indicates the use of the transient keyword as follows

private transient String your_variable_name;

But I can’t find the right way to do this in Kotlin.

+11
source share
2 answers

Use annotation @Transient.

@Transient
private val your_variable_name: String

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

+14
source

Kotlin + Retrofit + Moshi

In case you want to conditionally ignore fields, you can set it to zero.

data class  User(var id: String,  var name: string?)

val user = User()
user.id = "some id"
user.name = null

Generated Json will be

user{
"id": "some id"
}
0
source

Source: https://habr.com/ru/post/1689505/


All Articles