|
| 1 | +/* |
| 2 | + * Copyright 2016-2024 JetBrains s.r.o. |
| 3 | + * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file. |
| 4 | + */ |
| 5 | + |
| 6 | +package kotlinx.collections.immutable.serialization |
| 7 | + |
| 8 | +import kotlinx.collections.immutable.ImmutableCollection |
| 9 | +import kotlinx.collections.immutable.ImmutableList |
| 10 | +import kotlinx.collections.immutable.ImmutableSet |
| 11 | +import kotlinx.collections.immutable.PersistentList |
| 12 | +import kotlinx.collections.immutable.PersistentSet |
| 13 | +import kotlinx.collections.immutable.toImmutableList |
| 14 | +import kotlinx.collections.immutable.toImmutableSet |
| 15 | +import kotlinx.collections.immutable.toPersistentHashSet |
| 16 | +import kotlinx.collections.immutable.toPersistentList |
| 17 | +import kotlinx.collections.immutable.toPersistentSet |
| 18 | +import kotlinx.serialization.KSerializer |
| 19 | +import kotlinx.serialization.builtins.ListSerializer |
| 20 | +import kotlinx.serialization.descriptors.SerialDescriptor |
| 21 | +import kotlinx.serialization.encoding.Decoder |
| 22 | +import kotlinx.serialization.encoding.Encoder |
| 23 | + |
| 24 | +internal class DefaultImmutableCollectionSerializer<T, C : ImmutableCollection<T>>( |
| 25 | + serializer: KSerializer<T>, |
| 26 | + private val transform: (Collection<T>) -> C |
| 27 | +) : KSerializer<C> { |
| 28 | + private val listSerializer = ListSerializer(serializer) |
| 29 | + |
| 30 | + override val descriptor: SerialDescriptor = serializer.descriptor |
| 31 | + |
| 32 | + override fun serialize(encoder: Encoder, value: C) { |
| 33 | + return listSerializer.serialize(encoder, value.toList()) |
| 34 | + } |
| 35 | + |
| 36 | + override fun deserialize(decoder: Decoder): C { |
| 37 | + return listSerializer.deserialize(decoder).let(transform) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +public class ImmutableListSerializer<T>( |
| 42 | + serializer: KSerializer<T> |
| 43 | +) : KSerializer<ImmutableList<T>> by DefaultImmutableCollectionSerializer( |
| 44 | + serializer = serializer, |
| 45 | + transform = { decodedList -> decodedList.toImmutableList() } |
| 46 | +) |
| 47 | + |
| 48 | +public class PersistentListSerializer<T>( |
| 49 | + serializer: KSerializer<T> |
| 50 | +) : KSerializer<PersistentList<T>> by DefaultImmutableCollectionSerializer( |
| 51 | + serializer = serializer, |
| 52 | + transform = { decodedList -> decodedList.toPersistentList() } |
| 53 | +) |
| 54 | + |
| 55 | +public class ImmutableSetSerializer<T>( |
| 56 | + serializer: KSerializer<T> |
| 57 | +) : KSerializer<ImmutableSet<T>> by DefaultImmutableCollectionSerializer( |
| 58 | + serializer = serializer, |
| 59 | + transform = { decodedList -> decodedList.toImmutableSet() } |
| 60 | +) |
| 61 | + |
| 62 | +public class PersistentSetSerializer<T>( |
| 63 | + serializer: KSerializer<T> |
| 64 | +) : KSerializer<PersistentSet<T>> by DefaultImmutableCollectionSerializer( |
| 65 | + serializer = serializer, |
| 66 | + transform = { decodedList -> decodedList.toPersistentSet() } |
| 67 | +) |
| 68 | + |
| 69 | +public class PersistentHashSetSerializer<T>( |
| 70 | + serializer: KSerializer<T> |
| 71 | +) : KSerializer<PersistentSet<T>> by DefaultImmutableCollectionSerializer( |
| 72 | + serializer = serializer, |
| 73 | + transform = { decodedList -> decodedList.toPersistentHashSet() } |
| 74 | +) |
0 commit comments