commonMain.MapperDeserializationStrategy.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micro_utils.serialization.mapper-jvm Show documentation
Show all versions of micro_utils.serialization.mapper-jvm Show documentation
It is set of projects with micro tools for avoiding of routines coding
package dev.inmo.micro_utils.serialization.mapper
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
/**
* Use this serializer when you have deserializable type [I] and want to map it to some [O] in process of
* deserialization
*
* @param base Serializer for [I]
* @param deserialize Will be used in [deserialize] method to convert deserialized by [base] [I] to [O]
*/
open class MapperDeserializationStrategy(
private val base: DeserializationStrategy,
private val deserialize: (Decoder, I) -> O
) : DeserializationStrategy {
override val descriptor: SerialDescriptor = base.descriptor
constructor(
base: DeserializationStrategy,
deserialize: (I) -> O
) : this(base, { _, i -> deserialize(i) })
override fun deserialize(decoder: Decoder): O {
return deserialize(decoder, base.deserialize(decoder))
}
}