From ec3fb4b2af2127580036f65aebac059a0ac05dc1 Mon Sep 17 00:00:00 2001 From: Charles Korn Date: Sun, 25 Apr 2021 10:09:36 +1000 Subject: [PATCH] Fix deprecation warnings and issue revealed by upgrade to snakeyaml-engine 2.3. --- src/jvmMain/kotlin/com/charleskorn/kaml/Yaml.kt | 7 ++++--- src/jvmMain/kotlin/com/charleskorn/kaml/YamlOutput.kt | 9 ++++++++- src/jvmMain/kotlin/com/charleskorn/kaml/YamlParser.kt | 4 ++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/jvmMain/kotlin/com/charleskorn/kaml/Yaml.kt b/src/jvmMain/kotlin/com/charleskorn/kaml/Yaml.kt index 55ee8f23..166f6488 100644 --- a/src/jvmMain/kotlin/com/charleskorn/kaml/Yaml.kt +++ b/src/jvmMain/kotlin/com/charleskorn/kaml/Yaml.kt @@ -47,10 +47,11 @@ public actual class Yaml( override fun flush() { } } - val output = YamlOutput(writer, serializersModule, configuration) - output.encodeSerializableValue(serializer, value) + YamlOutput(writer, serializersModule, configuration).use { output -> + output.encodeSerializableValue(serializer, value) + } - return writer.toString() + return writer.toString().trimEnd() } public actual companion object { diff --git a/src/jvmMain/kotlin/com/charleskorn/kaml/YamlOutput.kt b/src/jvmMain/kotlin/com/charleskorn/kaml/YamlOutput.kt index 9acd1204..142da8f9 100644 --- a/src/jvmMain/kotlin/com/charleskorn/kaml/YamlOutput.kt +++ b/src/jvmMain/kotlin/com/charleskorn/kaml/YamlOutput.kt @@ -30,6 +30,7 @@ import org.snakeyaml.engine.v2.api.StreamDataWriter import org.snakeyaml.engine.v2.common.FlowStyle import org.snakeyaml.engine.v2.common.ScalarStyle import org.snakeyaml.engine.v2.emitter.Emitter +import org.snakeyaml.engine.v2.events.DocumentEndEvent import org.snakeyaml.engine.v2.events.DocumentStartEvent import org.snakeyaml.engine.v2.events.ImplicitTuple import org.snakeyaml.engine.v2.events.MappingEndEvent @@ -37,6 +38,7 @@ import org.snakeyaml.engine.v2.events.MappingStartEvent import org.snakeyaml.engine.v2.events.ScalarEvent import org.snakeyaml.engine.v2.events.SequenceEndEvent import org.snakeyaml.engine.v2.events.SequenceStartEvent +import org.snakeyaml.engine.v2.events.StreamEndEvent import org.snakeyaml.engine.v2.events.StreamStartEvent import java.util.Optional @@ -45,7 +47,7 @@ internal class YamlOutput( writer: StreamDataWriter, override val serializersModule: SerializersModule, private val configuration: YamlConfiguration -) : AbstractEncoder() { +) : AbstractEncoder(), AutoCloseable { private val settings = DumpSettings.builder() .setIndent(configuration.encodingIndentationSize) .setWidth(configuration.breakScalarsAt) @@ -126,6 +128,11 @@ internal class YamlOutput( } } + override fun close() { + emitter.emit(DocumentEndEvent(false)) + emitter.emit(StreamEndEvent()) + } + private fun emitScalar(value: String, style: ScalarStyle) { val tag = getAndClearTypeName() diff --git a/src/jvmMain/kotlin/com/charleskorn/kaml/YamlParser.kt b/src/jvmMain/kotlin/com/charleskorn/kaml/YamlParser.kt index bcc118c7..68153e10 100644 --- a/src/jvmMain/kotlin/com/charleskorn/kaml/YamlParser.kt +++ b/src/jvmMain/kotlin/com/charleskorn/kaml/YamlParser.kt @@ -28,8 +28,8 @@ import java.io.StringReader internal class YamlParser(yamlSource: String) { private val dummyFileName = "DUMMY_FILE_NAME" private val loadSettings = LoadSettings.builder().setLabel(dummyFileName).build() - private val streamReader = StreamReader(StringReader(yamlSource), loadSettings) - private val events = ParserImpl(streamReader, loadSettings) + private val streamReader = StreamReader(loadSettings, StringReader(yamlSource)) + private val events = ParserImpl(loadSettings, streamReader) init { consumeEventOfType(Event.ID.StreamStart, YamlPath.root)