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

commonMain.mahjongutils.entry.coder.JsonCoder.kt Maven / Gradle / Ivy

The newest version!
@file:OptIn(ExperimentalSerializationApi::class)

package mahjongutils.entry.coder

import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.json.Json
import kotlinx.serialization.serializer
import mahjongutils.entry.Result
import kotlin.reflect.KType


private val json = Json { ignoreUnknownKeys = true }

@Suppress("UNCHECKED_CAST")
internal object JsonParamsDecoder : ParamsDecoder {
    override fun  decodeParams(rawParams: String, paramsType: KType): PARAMS {
        return json.decodeFromString(serializer(paramsType), rawParams) as PARAMS
    }
}

internal object JsonResultEncoder : ResultEncoder {
    override fun  encodeData(result: DATA, dataType: KType): String {
        return json.encodeToString(serializer(dataType), result)
    }

    override fun  encodeResult(result: Result, dataType: KType): String {
        return json.encodeToString(
            serializer(Result::class, listOf(serializer(dataType)), false),
            result
        )
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy