commonMain.KspoonFormat.kt Maven / Gradle / Ivy
package dev.burnoo.kspoon
import dev.burnoo.kspoon.serializer.kspoonEncodeError
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.StringFormat
import kotlinx.serialization.modules.SerializersModule
internal class KspoonFormat(
private val kspoon: Kspoon,
override val serializersModule: SerializersModule,
private val encodeStringFormatDelegate: StringFormat?,
) : StringFormat {
override fun decodeFromString(deserializer: DeserializationStrategy, string: String): T {
return kspoon.parse(deserializer, string)
}
override fun encodeToString(serializer: SerializationStrategy, value: T): String {
return encodeStringFormatDelegate?.encodeToString(serializer, value)
?: kspoonEncodeError()
}
}