Skip to content

Commit

Permalink
Add convenience reified functions for de-/encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
sschuberth committed Aug 30, 2024
1 parent 7459b5f commit 47c8163
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/commonMain/kotlin/com/charleskorn/kaml/Yaml.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public class Yaml(
public val default: Yaml = Yaml()
}

public inline fun <reified T> decodeFromYamlNode(node: YamlNode): T =
decodeFromYamlNode(serializer<T>(), node)

public fun <T> decodeFromYamlNode(
deserializer: DeserializationStrategy<T>,
node: YamlNode,
Expand All @@ -48,13 +51,19 @@ public class Yaml(
return input.decodeSerializableValue(deserializer)
}

public inline fun <reified T> decodeFromString(string: String): T =
decodeFromString(serializersModule.serializer<T>(), string)

override fun <T> decodeFromString(
deserializer: DeserializationStrategy<T>,
string: String,
): T {
return decodeFromSource(deserializer, string.bufferedSource())
}

public inline fun <reified T> decodeFromSource(source: Source): T =
decodeFromSource(serializersModule.serializer<T>(), source)

public fun <T> decodeFromSource(
deserializer: DeserializationStrategy<T>,
source: Source,
Expand All @@ -77,6 +86,9 @@ public class Yaml(
return node
}

public inline fun <reified T> encodeToSink(value: T, sink: Sink) =
encodeToSink(serializersModule.serializer<T>(), value, sink)

public fun <T> encodeToSink(
serializer: SerializationStrategy<T>,
value: T,
Expand All @@ -85,6 +97,9 @@ public class Yaml(
encodeToBufferedSink(serializer, value, sink.buffer())
}

public inline fun <reified T> encodeToString(value: T): String =
encodeToString(serializersModule.serializer<T>(), value)

override fun <T> encodeToString(
serializer: SerializationStrategy<T>,
value: T,
Expand All @@ -94,6 +109,9 @@ public class Yaml(
return buffer.readUtf8().trimEnd()
}

public inline fun <reified T> encodeToBufferedSink(value: T, sink: BufferedSink) =
encodeToBufferedSink(serializersModule.serializer<T>(), value, sink)

private fun <T> encodeToBufferedSink(
serializer: SerializationStrategy<T>,
value: T,
Expand Down

0 comments on commit 47c8163

Please sign in to comment.