From 47c8163e51540515e858740dac353d636bae1837 Mon Sep 17 00:00:00 2001 From: Sebastian Schuberth Date: Sun, 30 Jun 2024 22:10:42 +0200 Subject: [PATCH] Add convenience `reified` functions for de-/encoding --- .../kotlin/com/charleskorn/kaml/Yaml.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/commonMain/kotlin/com/charleskorn/kaml/Yaml.kt b/src/commonMain/kotlin/com/charleskorn/kaml/Yaml.kt index 785ca490..0dfe3ee9 100644 --- a/src/commonMain/kotlin/com/charleskorn/kaml/Yaml.kt +++ b/src/commonMain/kotlin/com/charleskorn/kaml/Yaml.kt @@ -40,6 +40,9 @@ public class Yaml( public val default: Yaml = Yaml() } + public inline fun decodeFromYamlNode(node: YamlNode): T = + decodeFromYamlNode(serializer(), node) + public fun decodeFromYamlNode( deserializer: DeserializationStrategy, node: YamlNode, @@ -48,6 +51,9 @@ public class Yaml( return input.decodeSerializableValue(deserializer) } + public inline fun decodeFromString(string: String): T = + decodeFromString(serializersModule.serializer(), string) + override fun decodeFromString( deserializer: DeserializationStrategy, string: String, @@ -55,6 +61,9 @@ public class Yaml( return decodeFromSource(deserializer, string.bufferedSource()) } + public inline fun decodeFromSource(source: Source): T = + decodeFromSource(serializersModule.serializer(), source) + public fun decodeFromSource( deserializer: DeserializationStrategy, source: Source, @@ -77,6 +86,9 @@ public class Yaml( return node } + public inline fun encodeToSink(value: T, sink: Sink) = + encodeToSink(serializersModule.serializer(), value, sink) + public fun encodeToSink( serializer: SerializationStrategy, value: T, @@ -85,6 +97,9 @@ public class Yaml( encodeToBufferedSink(serializer, value, sink.buffer()) } + public inline fun encodeToString(value: T): String = + encodeToString(serializersModule.serializer(), value) + override fun encodeToString( serializer: SerializationStrategy, value: T, @@ -94,6 +109,9 @@ public class Yaml( return buffer.readUtf8().trimEnd() } + public inline fun encodeToBufferedSink(value: T, sink: BufferedSink) = + encodeToBufferedSink(serializersModule.serializer(), value, sink) + private fun encodeToBufferedSink( serializer: SerializationStrategy, value: T,