From d06c5577cbf9a8f0370b24a9b98e69352d5828b2 Mon Sep 17 00:00:00 2001 From: Paul de Vrieze Date: Fri, 1 Oct 2021 22:23:37 +0100 Subject: [PATCH] Fix merging for maplikeSerializer when the map is not empty (by using the actual size * 2). The index in the conceptual array is twice the amount of elements in the list. This way the reading is consistent between single and multipass. Also decoders can thus use even/odd to determine whether to read the key or value. --- .../kotlinx/serialization/internal/CollectionSerializers.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/commonMain/src/kotlinx/serialization/internal/CollectionSerializers.kt b/core/commonMain/src/kotlinx/serialization/internal/CollectionSerializers.kt index c3d913fa2..b507a4987 100644 --- a/core/commonMain/src/kotlinx/serialization/internal/CollectionSerializers.kt +++ b/core/commonMain/src/kotlinx/serialization/internal/CollectionSerializers.kt @@ -257,7 +257,7 @@ internal class LinkedHashMapSerializer( override fun Map.collectionSize(): Int = size override fun Map.collectionIterator(): Iterator> = iterator() override fun builder(): LinkedHashMap = LinkedHashMap() - override fun LinkedHashMap.builderSize(): Int = size + override fun LinkedHashMap.builderSize(): Int = size * 2 override fun LinkedHashMap.toResult(): Map = this override fun Map.toBuilder(): LinkedHashMap = this as? LinkedHashMap ?: LinkedHashMap(this) override fun LinkedHashMap.checkCapacity(size: Int) {} @@ -273,7 +273,7 @@ internal class HashMapSerializer( override fun Map.collectionSize(): Int = size override fun Map.collectionIterator(): Iterator> = iterator() override fun builder(): HashMap = HashMap() - override fun HashMap.builderSize(): Int = size + override fun HashMap.builderSize(): Int = size * 2 override fun HashMap.toResult(): Map = this override fun Map.toBuilder(): HashMap = this as? HashMap ?: HashMap(this) override fun HashMap.checkCapacity(size: Int) {}