Is there an elegant way to map from and to string ...
# announcements
i
Is there an elegant way to map from and to string values of an enum, which has to be deserialized by gson?
Copy code
enum class MyEnum {
    @SerializedName("my_foo") FOO // "my_foo" -> FOO (for deserialization)
    ...
}

MyEnum.FOO.string // FOO -> "my_foo"
Making the enum "bidirectional" via field +
Copy code
companion object {
    private val map = Type.values().associateBy(Type::value)
    fun fromInt(type: Int) = map[type]
}
is actually enough. I'll not deserialize it with directly gson but just create a lazy field that converts the string to an enum.