All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.Extensions.kt Maven / Gradle / Ivy

There is a newer version: 0.22.2
Show newest version
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)