/UserPreferencesWithSerializer.kt Secret
Last active
March 22, 2021 21:48
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
/* 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