Moshi ignore field in Kotlin

Viewed 12900

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

I've found this answer for Java (Moshi ignore field), that indicates to use the keyword transient as follows

private transient String your_variable_name;

But I can't find the right way to get this done in Kotlin.

3 Answers

Kotlin + Retrofit + Moshi

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

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

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

The Json generated would be

user{
"id": "some id"
}

Here is another way

@field:Json(ignore = true)
val your_variable_name: String
Related