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

com.github.kagkarlsson.examples.kotlin.KotlinSerializer.kt Maven / Gradle / Ivy

The newest version!
package com.github.kagkarlsson.examples.kotlin

import com.github.kagkarlsson.scheduler.serializer.Serializer
import kotlinx.serialization.KSerializer
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
import java.nio.charset.StandardCharsets

class KotlinSerializer : Serializer {
    val CHARSET = StandardCharsets.UTF_8

    override fun serialize(data: Any): ByteArray {
        val serializer = serializer(data.javaClass)
        return Json.encodeToString(serializer, data).toByteArray(CHARSET);
    }

    override fun  deserialize(clazz: Class, serializedData: ByteArray): T {
        // Hackish workaround?
        // https://github.com/Kotlin/kotlinx.serialization/issues/1134
        // https://stackoverflow.com/questions/64284767/replace-jackson-with-kotlinx-serialization-in-javalin-framework/64285478#64285478

        val deserializer = serializer(clazz) as KSerializer
        return Json.decodeFromString(deserializer, String(serializedData, CHARSET))
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy