diff options
Diffstat (limited to 'src/main/kotlin/util/SortedMapSerializer.kt')
-rw-r--r-- | src/main/kotlin/util/SortedMapSerializer.kt | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/main/kotlin/util/SortedMapSerializer.kt b/src/main/kotlin/util/SortedMapSerializer.kt new file mode 100644 index 0000000..baa10ad --- /dev/null +++ b/src/main/kotlin/util/SortedMapSerializer.kt @@ -0,0 +1,25 @@ + + +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) + } +} |