Slide 10
Slide 10 text
4FSJBMJ[FSΛ࡞
class PersonSerializer(
private val stringFormat: StringFormat = Json {
ignoreUnknownKeys = true
encodeDefaults = true
},
) : Serializer {
override val defaultValue: Person
get() = Person()
override suspend fun readFrom(input: InputStream): Person {
try {
val bytes = input.readBytes()
val string = bytes.decodeToString()
return stringFormat.decodeFromString(string) //ɹPersonΫϥεʹσίʔυͯ͠ಡΈऔΔ
} catch (e: SerializationException) {
throw CorruptionException("Cannot read stored data", e)
}
}
override suspend fun writeTo(t: Person, output: OutputStream) {
val string = stringFormat.encodeToString(t)
val bytes = string.encodeToByteArray() // PersonΫϥε͔ΒΤϯίʔυͯ͠ॻ͖ࠐΉ
withContext(Dispatchers.IO) {
output.write(bytes)
}
}
}