Skip to content

Commit

Permalink
Fix deprecation warnings and issue revealed by upgrade to snakeyaml-e…
Browse files Browse the repository at this point in the history
…ngine 2.3.
  • Loading branch information
charleskorn committed Apr 25, 2021
1 parent 2318134 commit ec3fb4b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/jvmMain/kotlin/com/charleskorn/kaml/Yaml.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion src/jvmMain/kotlin/com/charleskorn/kaml/YamlOutput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ 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
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

Expand All @@ -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)
Expand Down Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions src/jvmMain/kotlin/com/charleskorn/kaml/YamlParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ec3fb4b

Please sign in to comment.