I am trying to implement a custom Moshi adapter, only for serialization, that serializes the enum constant instead of the enum type itself.
interface ApiType {
val id: Int
}
enum class Gender: ApiType {
MALE {
override val id: Int = 1
},
FEMALE {
override val id: Int = 2
}
}
And the adapter:
class ApiTypeAdapter {
@ToJson
fun toJson(apiType: ApiType): Int {
return apiType.id
}
}
On the adapter, if I replace the interface type ApiType
with Gender
it works as expected. The thing is that i am gonna have many types that should be serialized as such and I want to generalize the adapter.
How can I make the adapter work on all the enums that implement the ApiType
?