Skip to content

Instantly share code, notes, and snippets.

@rohitsat13
Last active March 22, 2021 21:48
Show Gist options
  • Save rohitsat13/137270aac38bf01d788d3ffccdd60f28 to your computer and use it in GitHub Desktop.
Save rohitsat13/137270aac38bf01d788d3ffccdd60f28 to your computer and use it in GitHub Desktop.
/* Copyright 2021 Google LLC.
SPDX-License-Identifier: Apache-2.0 */
@Serializable
data class UserPreferences(
val showCompleted: Boolean = false,
val sortOrder: SortOrder = SortOrder.None
)
object UserPreferencesSerializer : Serializer<UserPreferences> {
override val defaultValue = UserPreferences()
override suspend fun readFrom(input: InputStream): UserPreferences {
try {
return Json.decodeFromString(
UserPreferences.serializer(), input.readBytes().decodeToString())
} catch (serialization: SerializationException) {
throw CorruptionException("Unable to read UserPrefs", serialization)
}
}
override suspend fun writeTo(t: UserPreferences, output: OutputStream) {
output.write(Json.encodeToString(UserPreferences.serializer(), t).encodeToByteArray())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment