Created
November 17, 2022 10:27
-
-
Save steliosfran/18a7721a0246a771c6564b77a4cad818 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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