From bd96c67788e54efa9415ce355136e34f176c5a77 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Sat, 24 Sep 2022 21:54:50 +0200 Subject: [PATCH] 2.18 serialization issues fix. Fixes #61. --- .../readerforselfossv2/model/SelfossModel.kt | 45 ++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt index e392786..2543979 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt @@ -2,7 +2,14 @@ package bou.amine.apps.readerforselfossv2.model import bou.amine.apps.readerforselfossv2.utils.DateUtils import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded +import kotlinx.serialization.KSerializer import kotlinx.serialization.Serializable +import kotlinx.serialization.descriptors.PrimitiveKind +import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.* class SelfossModel { @@ -50,6 +57,7 @@ class SelfossModel { data class Source( val id: Int, val title: String, + @Serializable(with = TagsListSerializer::class) val tags: List, val spout: String, val error: String, @@ -62,12 +70,15 @@ class SelfossModel { val datetime: String, val title: String, val content: String, + @Serializable(with = BooleanSerializer::class) var unread: Boolean, + @Serializable(with = BooleanSerializer::class) var starred: Boolean, val thumbnail: String?, val icon: String?, val link: String, val sourcetitle: String, + @Serializable(with = TagsListSerializer::class) val tags: List ) { // TODO: maybe find a better way to handle these kind of urls @@ -106,6 +117,38 @@ class SelfossModel { } } + // TODO: this seems to be super slow. + object TagsListSerializer : KSerializer> { + override fun deserialize(decoder: Decoder): List { + return when(val json = ((decoder as JsonDecoder).decodeJsonElement())) { + is JsonArray -> json.toList().map { it.toString() } + else -> json.toString().split(",") + } + + } + + override val descriptor: SerialDescriptor + get() = PrimitiveSerialDescriptor("tags", PrimitiveKind.STRING) + + override fun serialize(encoder: Encoder, value: List) { + TODO("Not yet implemented") + } + } + + object BooleanSerializer : KSerializer { + override fun deserialize(decoder: Decoder): Boolean { + val json = ((decoder as JsonDecoder).decodeJsonElement()).jsonPrimitive + return json.booleanOrNull ?: json.int == 1 + } + + override val descriptor: SerialDescriptor + get() = PrimitiveSerialDescriptor("b", PrimitiveKind.BOOLEAN) + + override fun serialize(encoder: Encoder, value: Boolean) { + TODO("Not yet implemented") + } + } + class StatusAndData(val success: Boolean, val data: T? = null) { companion object { fun succes(d: T): StatusAndData { @@ -117,4 +160,4 @@ class SelfossModel { } } } -} \ No newline at end of file +}