From 2343f37655bb4001354309da7eb7e847aae29d21 Mon Sep 17 00:00:00 2001 From: darksnake Date: Sat, 11 Feb 2023 17:14:00 +0300 Subject: [PATCH] Fix attribute serializer --- .../AttributesSerializer.kt | 21 ++++++++++++++----- .../attributes/AttributesSerializationTest.kt | 8 +++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/maps-kt-features/src/commonMain/kotlin/center.sciprog.attributes/AttributesSerializer.kt b/maps-kt-features/src/commonMain/kotlin/center.sciprog.attributes/AttributesSerializer.kt index b811f7f..a438293 100644 --- a/maps-kt-features/src/commonMain/kotlin/center.sciprog.attributes/AttributesSerializer.kt +++ b/maps-kt-features/src/commonMain/kotlin/center.sciprog.attributes/AttributesSerializer.kt @@ -6,9 +6,7 @@ import kotlinx.serialization.KSerializer import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonObject -import kotlinx.serialization.json.buildJsonObject +import kotlinx.serialization.json.* public class AttributesSerializer( private val serializableAttributes: Set>, @@ -21,7 +19,13 @@ public class AttributesSerializer( val attributeMap: Map, Any> = jsonElement.entries.associate { (key, element) -> val attr = serializableAttributes.find { it.serialId == key } ?: error("Attribute serializer for key $key not found") - val value = Json.decodeFromJsonElement(attr.serializer, element) ?: error("Null values are not allowed") + + val json = if (decoder is JsonDecoder) { + decoder.json + } else { + Json { serializersModule = decoder.serializersModule } + } + val value = json.decodeFromJsonElement(attr.serializer, element) ?: error("Null values are not allowed") attr to value } @@ -33,9 +37,16 @@ public class AttributesSerializer( value.content.forEach { (key, value) -> if (key !in serializableAttributes) error("An attribute key $key is not in the list of allowed attributes for this serializer") val serializableKey = key as SerializableAttribute + + val json = if (encoder is JsonEncoder) { + encoder.json + } else { + Json { serializersModule = encoder.serializersModule } + } + put( serializableKey.serialId, - Json.encodeToJsonElement(serializableKey.serializer as KSerializer, value) + json.encodeToJsonElement(serializableKey.serializer as KSerializer, value) ) } } diff --git a/maps-kt-features/src/commonTest/kotlin/center/sciprog/attributes/AttributesSerializationTest.kt b/maps-kt-features/src/commonTest/kotlin/center/sciprog/attributes/AttributesSerializationTest.kt index d26a769..c438ba8 100644 --- a/maps-kt-features/src/commonTest/kotlin/center/sciprog/attributes/AttributesSerializationTest.kt +++ b/maps-kt-features/src/commonTest/kotlin/center/sciprog/attributes/AttributesSerializationTest.kt @@ -7,10 +7,18 @@ import kotlin.test.assertEquals internal class AttributesSerializationTest { + internal class TestAttributeContainer(val attributes: Attributes) + +// internal object TestContainerAttribute: SerializableAttribute("container", se) + internal object TestAttribute : SerializableAttribute>("test", serializer()) @Test fun restore() { +// +// val serializersModule = SerializersModule { +// contextual(AttributesSerializer(setOf())) +// } val serializer = AttributesSerializer(setOf(NameAttribute, TestAttribute))