Skip to content
This repository was archived by the owner on Nov 30, 2025. It is now read-only.

Commit e843342

Browse files
authored
Merge pull request #248 from FloEdelmann/patch-1
Add generic `decodeFromStream`/`encodeToStream` extension functions
2 parents da211ee + 0758565 commit e843342

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/jvmMain/kotlin/com/charleskorn/kaml/Yaml.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import kotlinx.serialization.SerializationStrategy
2424
import kotlinx.serialization.StringFormat
2525
import kotlinx.serialization.modules.EmptySerializersModule
2626
import kotlinx.serialization.modules.SerializersModule
27+
import kotlinx.serialization.serializer
2728
import org.snakeyaml.engine.v2.api.StreamDataWriter
2829
import org.snakeyaml.engine.v2.api.YamlOutputStreamWriter
2930
import 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+
}

src/jvmTest/kotlin/com/charleskorn/kaml/JvmYamlReadingTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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
})

src/jvmTest/kotlin/com/charleskorn/kaml/JvmYamlWritingTest.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff 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
})

0 commit comments

Comments
 (0)