Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* 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