mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2024-12-19 07:07:16 +00:00
upgrade MapperSerializer
This commit is contained in:
parent
bb466ce66c
commit
c382423d77
@ -2,6 +2,10 @@
|
||||
|
||||
## 0.20.24
|
||||
|
||||
* `Serialization`:
|
||||
* `Mapper`:
|
||||
* `Mapper` pass decoder into callback of deserialization strategy
|
||||
|
||||
## 0.20.23
|
||||
|
||||
* `Versions`:
|
||||
|
@ -16,11 +16,16 @@ import kotlinx.serialization.encoding.Encoder
|
||||
*/
|
||||
open class MapperDeserializationStrategy<I, O>(
|
||||
private val base: DeserializationStrategy<I>,
|
||||
private val deserialize: (I) -> O
|
||||
private val deserialize: (Decoder, I) -> O
|
||||
) : DeserializationStrategy<O> {
|
||||
override val descriptor: SerialDescriptor = base.descriptor
|
||||
|
||||
constructor(
|
||||
base: DeserializationStrategy<I>,
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
@ -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<I, O>(
|
||||
private val base: KSerializer<I>,
|
||||
private val serialize: (O) -> I,
|
||||
private val deserialize: (I) -> O
|
||||
) : KSerializer<O> {
|
||||
private val deserialize: (Decoder, I) -> 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 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<I>,
|
||||
serialize: (O) -> I,
|
||||
deserialize: (I) -> O
|
||||
) : this(base, serialize, { _, i -> deserialize(i) })
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user