upgrade MapperSerializer

This commit is contained in:
InsanusMokrassar 2023-12-31 13:44:33 +06:00
parent bb466ce66c
commit c382423d77
3 changed files with 22 additions and 11 deletions

View File

@ -2,6 +2,10 @@
## 0.20.24 ## 0.20.24
* `Serialization`:
* `Mapper`:
* `Mapper` pass decoder into callback of deserialization strategy
## 0.20.23 ## 0.20.23
* `Versions`: * `Versions`:

View File

@ -16,11 +16,16 @@ import kotlinx.serialization.encoding.Encoder
*/ */
open class MapperDeserializationStrategy<I, O>( open class MapperDeserializationStrategy<I, O>(
private val base: DeserializationStrategy<I>, private val base: DeserializationStrategy<I>,
private val deserialize: (I) -> O private val deserialize: (Decoder, I) -> O
) : DeserializationStrategy<O> { ) : DeserializationStrategy<O> {
override val descriptor: SerialDescriptor = base.descriptor override val descriptor: SerialDescriptor = base.descriptor
constructor(
base: DeserializationStrategy<I>,
deserialize: (I) -> O
) : this(base, { _, i -> deserialize(i) })
override fun deserialize(decoder: Decoder): O { override fun deserialize(decoder: Decoder): O {
return deserialize(base.deserialize(decoder)) return deserialize(decoder, base.deserialize(decoder))
} }
} }

View File

@ -1,6 +1,8 @@
package dev.inmo.micro_utils.serialization.mapper package dev.inmo.micro_utils.serialization.mapper
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
@ -16,15 +18,15 @@ import kotlinx.serialization.encoding.Encoder
open class MapperSerializer<I, O>( open class MapperSerializer<I, O>(
private val base: KSerializer<I>, private val base: KSerializer<I>,
private val serialize: (O) -> I, private val serialize: (O) -> I,
private val deserialize: (I) -> O private val deserialize: (Decoder, I) -> O
) : KSerializer<O> { ) : KSerializer<O>,
DeserializationStrategy<O> by MapperDeserializationStrategy<I, O>(base, deserialize),
SerializationStrategy<O> by MapperSerializationStrategy<I, O>(base, serialize) {
override val descriptor: SerialDescriptor = base.descriptor override val descriptor: SerialDescriptor = base.descriptor
override fun deserialize(decoder: Decoder): O { constructor(
return deserialize(base.deserialize(decoder)) base: KSerializer<I>,
} serialize: (O) -> I,
deserialize: (I) -> O
override fun serialize(encoder: Encoder, value: O) { ) : this(base, serialize, { _, i -> deserialize(i) })
base.serialize(encoder, serialize(value))
}
} }