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.
Use the @Transient
annotation.
@Transient
private val your_variable_name: String
Doc here: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-transient/index.html
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With