diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt index 596e413fa21..367c267ac77 100644 --- a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/DiffUtils.kt @@ -2,6 +2,8 @@ package dev.inmo.micro_utils.common +import kotlinx.serialization.Serializable + private inline fun getObject( additional: MutableList, iterator: Iterator @@ -24,13 +26,14 @@ private inline fun getObject( * * @see calculateDiff */ +@Serializable data class Diff internal constructor( - val removed: List>, + val removed: List<@Serializable(IndexedValueSerializer::class) IndexedValue>, /** * Old-New values pairs */ - val replaced: List, IndexedValue>>, - val added: List> + val replaced: List, @Serializable(IndexedValueSerializer::class) IndexedValue>>, + val added: List<@Serializable(IndexedValueSerializer::class) IndexedValue> ) private inline fun performChanges( diff --git a/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/IndexedValueSerializer.kt b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/IndexedValueSerializer.kt new file mode 100644 index 00000000000..780972f4756 --- /dev/null +++ b/common/src/commonMain/kotlin/dev/inmo/micro_utils/common/IndexedValueSerializer.kt @@ -0,0 +1,30 @@ +package dev.inmo.micro_utils.common + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.Serializer +import kotlinx.serialization.builtins.PairSerializer +import kotlinx.serialization.builtins.serializer +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +class IndexedValueSerializer(private val subSerializer: KSerializer) : KSerializer> { + private val originalSerializer = PairSerializer(Int.serializer(), subSerializer) + override val descriptor: SerialDescriptor + get() = originalSerializer.descriptor + + override fun deserialize(decoder: Decoder): IndexedValue { + val pair = originalSerializer.deserialize(decoder) + return IndexedValue( + pair.first, + pair.second + ) + } + + override fun serialize(encoder: Encoder, value: IndexedValue) { + originalSerializer.serialize( + encoder, + Pair(value.index, value.value) + ) + } +}