diff options
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util')
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/util/SortedMapSerializer.kt | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/util/SortedMapSerializer.kt b/src/main/kotlin/moe/nea/firmament/util/SortedMapSerializer.kt new file mode 100644 index 0000000..6f4ff2e --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/util/SortedMapSerializer.kt @@ -0,0 +1,23 @@ +package moe.nea.firmament.util + +import java.util.SortedMap +import kotlinx.serialization.KSerializer +import kotlinx.serialization.builtins.MapSerializer +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder + +class SortedMapSerializer<K : Comparable<K>, V>(val keyDelegate: KSerializer<K>, val valueDelegate: KSerializer<V>) : + KSerializer<SortedMap<K, V>> { + val mapSerializer = MapSerializer(keyDelegate, valueDelegate) + override val descriptor: SerialDescriptor + get() = mapSerializer.descriptor + + override fun deserialize(decoder: Decoder): SortedMap<K, V> { + return (mapSerializer.deserialize(decoder).toSortedMap(Comparator.naturalOrder())) + } + + override fun serialize(encoder: Encoder, value: SortedMap<K, V>) { + mapSerializer.serialize(encoder, value) + } +} |