aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/inventory/storageoverlay/StorageData.kt3
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/SortedMapSerializer.kt23
2 files changed, 26 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/features/inventory/storageoverlay/StorageData.kt b/src/main/kotlin/moe/nea/firmament/features/inventory/storageoverlay/StorageData.kt
index 1c2ac08..00714ab 100644
--- a/src/main/kotlin/moe/nea/firmament/features/inventory/storageoverlay/StorageData.kt
+++ b/src/main/kotlin/moe/nea/firmament/features/inventory/storageoverlay/StorageData.kt
@@ -1,7 +1,10 @@
+@file:UseSerializers(SortedMapSerializer::class)
package moe.nea.firmament.features.inventory.storageoverlay
import java.util.SortedMap
import kotlinx.serialization.Serializable
+import kotlinx.serialization.UseSerializers
+import moe.nea.firmament.util.SortedMapSerializer
@Serializable
data class StorageData(
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)
+ }
+}