diff --git a/build.gradle.kts b/build.gradle.kts index e303c6a..36afbba 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -10,7 +10,7 @@ val kmathVersion: String by extra("0.3.1-dev-10") allprojects { group = "center.sciprog" - version = "0.2.2-dev-1" + version = "0.2.2-dev-3" repositories { mavenLocal() diff --git a/maps-kt-core/src/commonMain/kotlin/center/sciprog/maps/coordinates/MercatorProjection.kt b/maps-kt-core/src/commonMain/kotlin/center/sciprog/maps/coordinates/MercatorProjection.kt index d09d06d..f7d97d6 100644 --- a/maps-kt-core/src/commonMain/kotlin/center/sciprog/maps/coordinates/MercatorProjection.kt +++ b/maps-kt-core/src/commonMain/kotlin/center/sciprog/maps/coordinates/MercatorProjection.kt @@ -70,7 +70,6 @@ public open class MercatorProjection( ) ) } - } public companion object { diff --git a/maps-kt-features/build.gradle.kts b/maps-kt-features/build.gradle.kts index c0e7700..0ca6d61 100644 --- a/maps-kt-features/build.gradle.kts +++ b/maps-kt-features/build.gradle.kts @@ -21,4 +21,8 @@ kscience{ useSerialization{ json() } + + useSerialization(sourceSet = space.kscience.gradle.DependencySourceSet.TEST){ + protobuf() + } } \ No newline at end of file diff --git a/maps-kt-features/src/commonMain/kotlin/center.sciprog.attributes/Attribute.kt b/maps-kt-features/src/commonMain/kotlin/center.sciprog.attributes/Attribute.kt index 48cce56..492b74f 100644 --- a/maps-kt-features/src/commonMain/kotlin/center.sciprog.attributes/Attribute.kt +++ b/maps-kt-features/src/commonMain/kotlin/center.sciprog.attributes/Attribute.kt @@ -8,7 +8,9 @@ public interface Attribute public abstract class SerializableAttribute( public val serialId: String, public val serializer: KSerializer, -) : Attribute +) : Attribute { + override fun toString(): String = serialId +} public interface AttributeWithDefault : Attribute { public val default: T 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 a438293..690f923 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 @@ -34,8 +34,8 @@ public class AttributesSerializer( override fun serialize(encoder: Encoder, value: Attributes) { val json = buildJsonObject { - 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") + value.content.forEach { (key: Attribute<*>, value: Any) -> + 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) { 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 c438ba8..9c4c84e 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 @@ -1,36 +1,86 @@ package center.sciprog.attributes +import kotlinx.serialization.* import kotlinx.serialization.json.Json -import kotlinx.serialization.serializer +import kotlinx.serialization.modules.SerializersModule +import kotlinx.serialization.modules.contextual +import kotlinx.serialization.protobuf.ProtoBuf +import kotlin.test.Ignore import kotlin.test.Test import kotlin.test.assertEquals internal class AttributesSerializationTest { - internal class TestAttributeContainer(val attributes: Attributes) + @Serializable + internal class Container(@Contextual val attributes: Attributes) { + override fun equals(other: Any?): Boolean = (other as? Container)?.attributes?.equals(attributes) ?: false + override fun hashCode(): Int = attributes.hashCode() -// internal object TestContainerAttribute: SerializableAttribute("container", se) + override fun toString(): String = attributes.toString() + } - internal object TestAttribute : SerializableAttribute>("test", serializer()) + internal object ContainerAttribute : SerializableAttribute("container", serializer()) { + override fun toString(): String = "container" + + } + + internal object TestAttribute : SerializableAttribute>("test", serializer()) { + override fun toString(): String = "test" + } @Test - fun restore() { -// -// val serializersModule = SerializersModule { -// contextual(AttributesSerializer(setOf())) -// } - val serializer = AttributesSerializer(setOf(NameAttribute, TestAttribute)) - + fun restoreFromJson() { + val json = Json { + serializersModule = SerializersModule { + contextual(AttributesSerializer(setOf(NameAttribute, TestAttribute, ContainerAttribute))) + } + } val attributes = Attributes { NameAttribute("myTest") TestAttribute(mapOf("a" to "aa", "b" to "bb")) + ContainerAttribute( + Container( + Attributes { + TestAttribute(mapOf("a" to "aa", "b" to "bb")) + } + ) + ) } - val serialized = Json.encodeToString(serializer, attributes) + val serialized: String = json.encodeToString(attributes) println(serialized) - val restored = Json.decodeFromString(serializer, serialized) + val restored: Attributes = json.decodeFromString(serialized) + + assertEquals(attributes, restored) + } + + @OptIn(ExperimentalSerializationApi::class) + @Test + @Ignore + fun restoreFromProtoBuf() { + val protoBuf = ProtoBuf { + serializersModule = SerializersModule { + contextual(AttributesSerializer(setOf(NameAttribute, TestAttribute, ContainerAttribute))) + } + } + + val attributes = Attributes { + NameAttribute("myTest") + TestAttribute(mapOf("a" to "aa", "b" to "bb")) + ContainerAttribute( + Container( + Attributes { + TestAttribute(mapOf("a" to "aa", "b" to "bb")) + } + ) + ) + } + + val serialized = protoBuf.encodeToByteArray(attributes) + + val restored: Attributes = protoBuf.decodeFromByteArray(serialized) assertEquals(attributes, restored) }