Hello, I was wandering how to make Kotlin Enum cla...
# announcements
h
Hello, I was wandering how to make Kotlin Enum class serializable the good Way. I see some people using kotlinx.serializable with annotation @Serializable, somme others could use implementation avec the enum class like
Copy code
enum class Content(val value: String) : Serializable {
In java, Enum is serializable by design but not in Kotlin. How would you do it ?
d
Annotations!
d
Depends on what the enum represents and the purpose of the serialization.
h
@David Eriksson thankx for reply but can you be more explicit, sorry I don’t catch up your thought 😞
d
Example 1: if I have an enum with colours and I have enums red, green, blue then I would serialize to the RGB value
Example 2: if I have an enum class with colours and it contains Background colour, foreground colour, accent colour, then I would store a string representation,e.g. "BACKGROUND_COLOR"
Example 3: If I was serializing between two parts of the same running application, I could just use the ordinal of the enum, as it will be binary compatible. For example between a service on Android and the main application.
h
@David Eriksson thanks a lot for this detail answer 😄
🙇 1