commonMain.mahjongutils.entry.coder.JsonCoder.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mahjong-utils-entry-jvm Show documentation
Show all versions of mahjong-utils-entry-jvm Show documentation
Provider a union entry for Mahjong Utils (for Japanese Riichi Mahjong)
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
)
}
}