This repository was archived by the owner on Nov 30, 2025. It is now read-only.
File tree Expand file tree Collapse file tree
jvmMain/kotlin/com/charleskorn/kaml
jvmTest/kotlin/com/charleskorn/kaml Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ import kotlinx.serialization.SerializationStrategy
2424import kotlinx.serialization.StringFormat
2525import kotlinx.serialization.modules.EmptySerializersModule
2626import kotlinx.serialization.modules.SerializersModule
27+ import kotlinx.serialization.serializer
2728import org.snakeyaml.engine.v2.api.StreamDataWriter
2829import org.snakeyaml.engine.v2.api.YamlOutputStreamWriter
2930import java.io.IOException
@@ -89,3 +90,20 @@ public actual class Yaml(
8990 public actual val default: Yaml = Yaml ()
9091 }
9192}
93+
94+ /* *
95+ * Decodes and deserializes from the given [stream] to the value of type [T] using the
96+ * deserializer retrieved from the reified type parameter.
97+ */
98+ @OptIn(ExperimentalSerializationApi ::class )
99+ public inline fun <reified T > Yaml.decodeFromStream (stream : InputStream ): T =
100+ decodeFromStream(serializersModule.serializer(), stream)
101+
102+ /* *
103+ * Serializes and encodes the given [value] to the given [stream] using the serializer
104+ * retrieved from the reified type parameter.
105+ */
106+ @OptIn(ExperimentalSerializationApi ::class )
107+ public inline fun <reified T > Yaml.encodeToStream (value : T , stream : OutputStream ) {
108+ encodeToStream(serializersModule.serializer(), value, stream)
109+ }
Original file line number Diff line number Diff line change @@ -35,5 +35,14 @@ object JvmYamlReadingTest : Spek({
3535 expect(result).toEqual(123)
3636 }
3737 }
38+
39+ describe("parsing from a stream via generic extension function") {
40+ val input = " 123"
41+ val result = Yaml .default.decodeFromStream<Int >(ByteArrayInputStream (input.toByteArray(Charsets .UTF_8 )))
42+
43+ it("successfully deserializes values from a stream") {
44+ expect(result).toEqual(123)
45+ }
46+ }
3847 }
3948})
Original file line number Diff line number Diff line change @@ -110,5 +110,14 @@ object JvmYamlWritingTest : Spek({
110110 }
111111 }
112112 }
113+
114+ describe("writing to a stream via generic extension function") {
115+ val output = ByteArrayOutputStream ()
116+ Yaml .default.encodeToStream<String >("hello world", output)
117+
118+ it("returns the value serialized in the expected YAML form") {
119+ expect(output.toString(Charsets .UTF_8 )).toEqual("\"hello world\"\n")
120+ }
121+ }
113122 }
114123})
You can’t perform that action at this time.
0 commit comments