Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
inline fun <reified T> createEnumJsonAdapter(): JsonAdapter<T> where T : Enum<T>, T : IEnumValue {
return object : JsonAdapter<T>() {
@FromJson
override fun fromJson(reader: JsonReader): T? {
return if (reader.peek() != JsonReader.Token.NULL) {
enumValues<T>().firstOrNull { it.value == reader.nextInt() }
} else {
reader.nextNull()
}
}
@ToJson
override fun toJson(writer: JsonWriter, value: T?) {
writer.value(value?.value)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment