Is there any way to use the gson SerializedName annotation in a Kotlin interface?
interface User {
@SerializedName("user_id")
val userId: String?
}
data class MainUser(override val userId: String? = null) : User
I know the annotation can be in the child class, but that's not what I'm looking for as there are many classes extending that interface.
Note 1: Using abstract or open class as a parent of a data class will solve this, but the new problem would be the overrided equals() method in the parent never get called (mainUser1 == mainUser2 never check the equls() method in abstract or open class User )
I think, that interfaces it's not a good place to have properties (or state). Interfaces very good fits to establish behavior (functions). I'd rather suggest you to use abstract class for your purpose and it will work as you expected.
You can annotate property getter with @get
use-site target like this:
interface User {
@get:SerializedName("user_id")
val userId: String?
}
data class MainUser(override val userId: String? = null) : User
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