Skip to content

Commit

Permalink
Delegate sealed class decoding to a new StringMapToObjectDecoder inst…
Browse files Browse the repository at this point in the history
…ance
  • Loading branch information
janseeger committed Apr 2, 2024
1 parent 1453a23 commit cd19365
Showing 1 changed file with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.sipgate.federmappe.common

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerializationException
import kotlinx.serialization.descriptors.PolymorphicKind
Expand Down Expand Up @@ -55,6 +56,24 @@ class StringMapToObjectDecoder(
override fun decodeByte(): Byte = (decodeValue() as Long).toByte()
override fun decodeFloat(): Float = (decodeValue() as Double).toFloat()

override fun <T> decodeSerializableElement(
descriptor: SerialDescriptor,
index: Int,
deserializer: DeserializationStrategy<T>,
previousValue: T?
): T = if (descriptor.kind is PolymorphicKind) {
@Suppress("UNCHECKED_CAST")
deserializer.deserialize(
StringMapToObjectDecoder(
(data[key] as? Map<String, Any?>) ?: data,
serializersModule,
ignoreUnknownProperties
)
)
} else {
decodeSerializableValue(deserializer, previousValue)
}

@Suppress("UNCHECKED_CAST")
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {
if (key == null) {
Expand All @@ -71,7 +90,7 @@ class StringMapToObjectDecoder(

when (valueDescriptor) {
StructureKind.CLASS -> return StringMapToObjectDecoder(
data = (value as? Map<String, Any>) ?: data,
data = value as Map<String, Any>,
ignoreUnknownProperties = ignoreUnknownProperties,
serializersModule = this.serializersModule,
)
Expand Down

0 comments on commit cd19365

Please sign in to comment.