commonMain.io.ks3.test.TestHelpers.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ks3-test-jvm Show documentation
Show all versions of ks3-test-jvm Show documentation
KotlinX Serialization standard serializers
The newest version!
package io.ks3.test
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.internal.decodeStringToJsonTree
import kotlinx.serialization.json.internal.readJson
import kotlinx.serialization.json.okio.decodeFromBufferedSource
import kotlinx.serialization.json.okio.encodeToBufferedSink
import okio.Buffer
typealias Encoder = T.() -> String
typealias Decoder = String.() -> T
data class Encoders(
val encoders: List>,
val decoders: List>,
)
inline fun Json.generateEncoders(serializer: KSerializer) =
Encoders(
encoders(serializer),
decoders(serializer),
)
@OptIn(ExperimentalSerializationApi::class)
inline fun Json.encoders(serializer: KSerializer): List> =
listOf(
{ encodeToString(serializer, this) },
{ encodeToString(encodeToJsonElement(serializer, this)) },
{
val buffer = Buffer()
encodeToBufferedSink(serializer, this, buffer)
buffer.readUtf8()
},
)
@OptIn(ExperimentalSerializationApi::class, InternalSerializationApi::class)
inline fun Json.decoders(serializer: KSerializer): List> =
listOf(
{ decodeFromString(serializer, this) },
{
val tree = decodeStringToJsonTree(this@decoders, serializer, this)
readJson(this@decoders, tree, serializer)
},
{
val buffer = Buffer()
buffer.writeUtf8(this)
decodeFromBufferedSource(serializer, buffer)
},
)
© 2015 - 2025 Weber Informatics LLC | Privacy Policy