Skip to content

Instantly share code, notes, and snippets.

@shardul
shardul / AppExtensions.kt
Created June 3, 2019 10:10
Save Serializables in Shared Preferences with Kotlin and GSON [Android]
@Throws(JsonIOException::class)
fun Serializable.toJson(): String {
return Gson().toJson(this)
}
@Throws(JsonSyntaxException::class)
fun <T> String.to(type: Class<T>): T where T : Serializable {
return Gson().fromJson(this, type)
}