diff --git a/CHANGELOG.md b/CHANGELOG.md index e60d92cc8a4..50f48d119e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## 0.20.24 +* `Serialization`: + * `Mapper`: + * `Mapper` pass decoder into callback of deserialization strategy + ## 0.20.23 * `Versions`: diff --git a/serialization/mapper/src/commonMain/kotlin/MapperDeserializationStrategy.kt b/serialization/mapper/src/commonMain/kotlin/MapperDeserializationStrategy.kt index bf36ef9fe4b..7bb4b2690b8 100644 --- a/serialization/mapper/src/commonMain/kotlin/MapperDeserializationStrategy.kt +++ b/serialization/mapper/src/commonMain/kotlin/MapperDeserializationStrategy.kt @@ -16,11 +16,16 @@ import kotlinx.serialization.encoding.Encoder */ open class MapperDeserializationStrategy( private val base: DeserializationStrategy, - private val deserialize: (I) -> O + 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(base.deserialize(decoder)) + return deserialize(decoder, base.deserialize(decoder)) } } diff --git a/serialization/mapper/src/commonMain/kotlin/MapperSerializer.kt b/serialization/mapper/src/commonMain/kotlin/MapperSerializer.kt index 2df9a1ba334..acc8cbb178a 100644 --- a/serialization/mapper/src/commonMain/kotlin/MapperSerializer.kt +++ b/serialization/mapper/src/commonMain/kotlin/MapperSerializer.kt @@ -1,6 +1,8 @@ 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 @@ -16,15 +18,15 @@ import kotlinx.serialization.encoding.Encoder open class MapperSerializer( private val base: KSerializer, private val serialize: (O) -> I, - private val deserialize: (I) -> O -) : KSerializer { + private val deserialize: (Decoder, I) -> O +) : KSerializer, + DeserializationStrategy by MapperDeserializationStrategy(base, deserialize), + SerializationStrategy by MapperSerializationStrategy(base, serialize) { override val descriptor: SerialDescriptor = base.descriptor - override fun deserialize(decoder: Decoder): O { - return deserialize(base.deserialize(decoder)) - } - - override fun serialize(encoder: Encoder, value: O) { - base.serialize(encoder, serialize(value)) - } + constructor( + base: KSerializer, + serialize: (O) -> I, + deserialize: (I) -> O + ) : this(base, serialize, { _, i -> deserialize(i) }) }