diff --git a/serialization/mapper/src/commonMain/kotlin/Extensions.kt b/serialization/mapper/src/commonMain/kotlin/Extensions.kt new file mode 100644 index 00000000000..ca0f5b2a096 --- /dev/null +++ b/serialization/mapper/src/commonMain/kotlin/Extensions.kt @@ -0,0 +1,99 @@ +package dev.inmo.micro_utils.serialization.mapper + +import kotlinx.serialization.DeserializationStrategy +import kotlinx.serialization.InternalSerializationApi +import kotlinx.serialization.KSerializer +import kotlinx.serialization.SerializationStrategy +import kotlinx.serialization.serializer +import kotlin.reflect.KClass + +/** + * Will create [MapperSerializationStrategy] to allow you to map [O] to [I] using [serialize] lambda during + * serialization process + */ +inline fun SerializationStrategy.mapSerialization( + noinline serialize: (O) -> I +) = MapperSerializationStrategy( + this, + serialize +) + +/** + * Will create [MapperDeserializationStrategy] to allow you to map [I] to [O] using [deserialize] lambda during + * deserialization process + */ +inline fun DeserializationStrategy.mapDeserialization( + noinline deserialize: (I) -> O +) = MapperDeserializationStrategy( + this, + deserialize +) + +/** + * Will create [MapperSerializer] to allow you to map [O] to [I] and vice verse using [serialize]/[deserialize] lambda during + * serialization/deserialization process + */ +inline fun KSerializer.mapFullSerialization( + noinline serialize: (O) -> I, + noinline deserialize: (I) -> O +) = MapperSerializer( + this, + serialize, + deserialize +) + +/** + * Will create [MapperSerializationStrategy] to allow you to map [O] to [I] using [serialize] lambda during + * serialization process + */ +@OptIn(InternalSerializationApi::class) +inline fun KClass.mapSerialization( + serializer: SerializationStrategy = serializer(), + noinline serialize: (O) -> I +) = serializer.mapSerialization(serialize) + +/** + * Will create [MapperDeserializationStrategy] to allow you to map [I] to [O] using [deserialize] lambda during + * deserialization process + */ +@OptIn(InternalSerializationApi::class) +inline fun KClass.mapDeserialization( + serializer: DeserializationStrategy = serializer(), + noinline deserialize: (I) -> O +) = serializer.mapDeserialization(deserialize) + +/** + * Will create [MapperSerializer] to allow you to map [O] to [I] and vice verse using [serialize]/[deserialize] lambda during + * serialization/deserialization process + */ +@OptIn(InternalSerializationApi::class) +inline fun KClass.mapFullSerialization( + serializer: KSerializer = serializer(), + noinline serialize: (O) -> I, + noinline deserialize: (I) -> O +) = serializer.mapFullSerialization(serialize, deserialize) + +/** + * Will create [MapperSerializationStrategy] to allow you to map [O] to [I] using [serialize] lambda during + * serialization process + */ +inline fun mappedSerializationStrategy( + noinline serialize: (O) -> I, +) = serializer().mapSerialization(serialize) + +/** + * Will create [MapperDeserializationStrategy] to allow you to map [I] to [O] using [deserialize] lambda during + * deserialization process + */ +inline fun mappedDeserializationStrategy( + noinline deserialize: (I) -> O +) = serializer().mapDeserialization(deserialize) + +/** + * Will create [MapperSerializer] to allow you to map [O] to [I] and vice verse using [serialize]/[deserialize] lambda during + * serialization/deserialization process + */ +inline fun mappedSerializer( + noinline serialize: (O) -> I, + noinline deserialize: (I) -> O +) = serializer().mapFullSerialization(serialize, deserialize) diff --git a/serialization/mapper/src/commonMain/kotlin/MapperDeserializationStrategy.kt b/serialization/mapper/src/commonMain/kotlin/MapperDeserializationStrategy.kt new file mode 100644 index 00000000000..bf36ef9fe4b --- /dev/null +++ b/serialization/mapper/src/commonMain/kotlin/MapperDeserializationStrategy.kt @@ -0,0 +1,26 @@ +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: (I) -> O +) : DeserializationStrategy { + override val descriptor: SerialDescriptor = base.descriptor + + override fun deserialize(decoder: Decoder): O { + return deserialize(base.deserialize(decoder)) + } +} diff --git a/serialization/mapper/src/commonMain/kotlin/MapperSerializationStrategy.kt b/serialization/mapper/src/commonMain/kotlin/MapperSerializationStrategy.kt new file mode 100644 index 00000000000..4772932915c --- /dev/null +++ b/serialization/mapper/src/commonMain/kotlin/MapperSerializationStrategy.kt @@ -0,0 +1,25 @@ +package dev.inmo.micro_utils.serialization.mapper + +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 serializable type [I] and want to map it to some [O] in process of + * serialization + * + * @param base Serializer for [I] + * @param serialize Will be used in [serialize] method to convert incoming [O] to [I] and serialize with [base] + */ +open class MapperSerializationStrategy( + private val base: SerializationStrategy, + private val serialize: (O) -> I +) : SerializationStrategy { + override val descriptor: SerialDescriptor = base.descriptor + + override fun serialize(encoder: Encoder, value: O) { + base.serialize(encoder, serialize(value)) + } +} diff --git a/serialization/mapper/src/commonMain/kotlin/MapperSerializer.kt b/serialization/mapper/src/commonMain/kotlin/MapperSerializer.kt index c18343deb82..2df9a1ba334 100644 --- a/serialization/mapper/src/commonMain/kotlin/MapperSerializer.kt +++ b/serialization/mapper/src/commonMain/kotlin/MapperSerializer.kt @@ -5,7 +5,15 @@ import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Encoder -class MapperSerializer( +/** + * Use this serializer when you have serializable type [I] and want to map it to some [O] in process of + * serialization/deserialization + * + * @param base Serializer for [I] + * @param serialize Will be used in [serialize] method to convert incoming [O] to [I] and serialize with [base] + * @param deserialize Will be used in [deserialize] method to convert deserialized by [base] [I] to [O] + */ +open class MapperSerializer( private val base: KSerializer, private val serialize: (O) -> I, private val deserialize: (I) -> O