-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
177 additions
and
1 deletion.
There are no files selected for viewing
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
91 changes: 91 additions & 0 deletions
91
...e-keeper/src/commonMain/kotlin/com/arkivanov/essenty/statekeeper/PolymorphicSerializer.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.arkivanov.essenty.statekeeper | ||
|
||
import com.arkivanov.essenty.utils.internal.ExperimentalEssentyApi | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.descriptors.SerialDescriptor | ||
import kotlinx.serialization.descriptors.SerialKind | ||
import kotlinx.serialization.descriptors.buildClassSerialDescriptor | ||
import kotlinx.serialization.descriptors.element | ||
import kotlinx.serialization.encoding.CompositeDecoder | ||
import kotlinx.serialization.encoding.Decoder | ||
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.encoding.decodeStructure | ||
import kotlinx.serialization.encoding.encodeStructure | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlin.reflect.KClass | ||
|
||
@ExperimentalEssentyApi | ||
@ExperimentalSerializationApi | ||
inline fun <reified T : Any> polymorphicSerializer( | ||
module: SerializersModule | ||
) : KSerializer<T> = | ||
polymorphicSerializer(baseClass = T::class, module = module) | ||
|
||
@ExperimentalEssentyApi | ||
@ExperimentalSerializationApi | ||
fun <T : Any> polymorphicSerializer( | ||
baseClass: KClass<T>, | ||
module: SerializersModule | ||
) : KSerializer<T> = | ||
PolymorphicSerializer(baseClass = baseClass, module = module) | ||
|
||
@ExperimentalSerializationApi | ||
private class PolymorphicSerializer<T : Any>( | ||
private val baseClass: KClass<T>, | ||
private val module: SerializersModule, | ||
) : KSerializer<T> { | ||
override val descriptor: SerialDescriptor = | ||
buildClassSerialDescriptor("PolymorphicSerializer") { | ||
element<String>("type") | ||
element("value", ContextualSerialDescriptor) | ||
} | ||
|
||
override fun serialize(encoder: Encoder, value: T) { | ||
val serializer = requireNotNull(module.getPolymorphic(baseClass, value)) | ||
encoder.encodeStructure(descriptor) { | ||
encodeStringElement(descriptor, 0, serializer.descriptor.serialName) | ||
encodeSerializableElement(descriptor, 1, serializer, value) | ||
} | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): T = | ||
decoder.decodeStructure(descriptor) { | ||
var className: String? = null | ||
var value: T? = null | ||
|
||
while (true) { | ||
when (val index = decodeElementIndex(descriptor)) { | ||
0 -> className = decodeStringElement(descriptor, index) | ||
|
||
1 -> { | ||
val actualClassName = requireNotNull(className) | ||
val serializer = requireNotNull(module.getPolymorphic(baseClass, actualClassName)) | ||
value = decodeSerializableElement(descriptor, 1, serializer) | ||
} | ||
|
||
CompositeDecoder.DECODE_DONE -> break | ||
|
||
else -> error("Unsupported index: $index") | ||
} | ||
} | ||
|
||
requireNotNull(value) | ||
} | ||
|
||
private object ContextualSerialDescriptor : SerialDescriptor { | ||
override val elementsCount: Int = 0 | ||
override val kind: SerialKind = SerialKind.CONTEXTUAL | ||
override val serialName: String = "Value" | ||
|
||
override fun getElementAnnotations(index: Int): List<Annotation> = elementNotFoundError(index) | ||
override fun getElementDescriptor(index: Int): SerialDescriptor = elementNotFoundError(index) | ||
override fun getElementIndex(name: String): Int = CompositeDecoder.UNKNOWN_NAME | ||
override fun getElementName(index: Int): String = elementNotFoundError(index) | ||
override fun isElementOptional(index: Int): Boolean = elementNotFoundError(index) | ||
|
||
private fun elementNotFoundError(index: Int): Nothing { | ||
throw IndexOutOfBoundsException("Element at index $index not found") | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...eper/src/commonTest/kotlin/com/arkivanov/essenty/statekeeper/PolymorphicSerializerTest.kt
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.arkivanov.essenty.statekeeper | ||
|
||
import com.arkivanov.essenty.utils.internal.ExperimentalEssentyApi | ||
import kotlinx.serialization.ExperimentalSerializationApi | ||
import kotlinx.serialization.KSerializer | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.builtins.ListSerializer | ||
import kotlinx.serialization.modules.SerializersModule | ||
import kotlinx.serialization.modules.polymorphic | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class PolymorphicSerializerTest { | ||
|
||
@Test | ||
fun serialize_and_deserialize() { | ||
val someListSerializer = ListSerializer(SomeSerializer) | ||
val originalSome = listOf(Some1(1), Some2("2")) | ||
|
||
val newSome = originalSome.serialize(someListSerializer).deserialize(someListSerializer) | ||
|
||
assertEquals(originalSome, newSome) | ||
} | ||
|
||
private interface Some | ||
|
||
@Serializable | ||
private data class Some1(val value: Int) : Some | ||
|
||
@Serializable | ||
private data class Some2(val value: String) : Some | ||
|
||
@OptIn(ExperimentalEssentyApi::class, ExperimentalSerializationApi::class) | ||
private object SomeSerializer : KSerializer<Some> by polymorphicSerializer( | ||
SerializersModule { | ||
polymorphic(Some::class) { | ||
subclass(Some1::class, Some1.serializer()) | ||
subclass(Some2::class, Some2.serializer()) | ||
} | ||
} | ||
) | ||
} |