diff options
author | Linnea Gräf <nea@nea.moe> | 2024-05-18 17:33:19 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-05-18 17:33:19 +0200 |
commit | 8c66016bb3041793961375178d470546519e0e69 (patch) | |
tree | d8b65a2727619766856f7a865665303cccc444a2 /src/main/kotlin/moe/nea/firmament/util | |
parent | a39f6e9bec3318b2be251570ea7aea29d58b49cb (diff) | |
download | firmament-8c66016bb3041793961375178d470546519e0e69.tar.gz firmament-8c66016bb3041793961375178d470546519e0e69.tar.bz2 firmament-8c66016bb3041793961375178d470546519e0e69.zip |
Add more screens at once
[no changelog]
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/util')
-rw-r--r-- | src/main/kotlin/moe/nea/firmament/util/json/SingletonSerializableList.kt | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/util/json/SingletonSerializableList.kt b/src/main/kotlin/moe/nea/firmament/util/json/SingletonSerializableList.kt new file mode 100644 index 0000000..5474b35 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/util/json/SingletonSerializableList.kt @@ -0,0 +1,36 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.util.json + +import kotlinx.serialization.KSerializer +import kotlinx.serialization.builtins.ListSerializer +import kotlinx.serialization.descriptors.SerialDescriptor +import kotlinx.serialization.encoding.Decoder +import kotlinx.serialization.encoding.Encoder +import kotlinx.serialization.json.JsonArray +import kotlinx.serialization.json.JsonDecoder +import kotlinx.serialization.json.JsonElement + +class SingletonSerializableList<T>(val child: KSerializer<T>) : KSerializer<List<T>> { + override val descriptor: SerialDescriptor + get() = JsonElement.serializer().descriptor + + override fun deserialize(decoder: Decoder): List<T> { + decoder as JsonDecoder + val list = JsonElement.serializer().deserialize(decoder) + if (list is JsonArray) { + return list.map { + decoder.json.decodeFromJsonElement(child, it) + } + } + return listOf(decoder.json.decodeFromJsonElement(child, list)) + } + + override fun serialize(encoder: Encoder, value: List<T>) { + ListSerializer(child).serialize(encoder, value) + } +} |