Skip to content

Commit

Permalink
Upgrade to Kotlin 1.4 and kotlinx.serialization 1.0.0-RC.
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskorn committed Aug 18, 2020
1 parent 8ace48b commit 938d250
Show file tree
Hide file tree
Showing 8 changed files with 382 additions and 374 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ val input = """
- Dan
""".trimIndent()

val result = Yaml.default.parse(Team.serializer(), input)
val result = Yaml.default.decodeFromString(Team.serializer(), input)

println(result)
```
Expand All @@ -46,7 +46,7 @@ data class Team(

val input = Team("Amy", listOf("Bob", "Cindy", "Dan"))

val result = Yaml.default.stringify(Team.serializer(), input)
val result = Yaml.default.encodeToString(Team.serializer(), input)

println(result)
```
Expand Down Expand Up @@ -108,7 +108,7 @@ println(result)
Backend("db-1")
))
val result = Yaml.default.stringify(Config.serializer(), config)
val result = Yaml.default.encodeToString(Config.serializer(), config)
println(result)
```
Expand Down
8 changes: 4 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ buildscript {
}

dependencies {
classpath("org.jetbrains.kotlin:kotlin-serialization:1.3.72")
classpath(kotlin("serialization", version = "1.4.0"))
}
}

plugins {
kotlin("jvm") version "1.3.72"
kotlin("jvm") version "1.4.0"

apply { id("com.github.ben-manes.versions") version "0.29.0" }
}
Expand All @@ -56,7 +56,7 @@ dependencies {

implementation(kotlin("stdlib-jdk8"))
implementation(group = "org.snakeyaml", name = "snakeyaml-engine", version = "2.1")
implementation(group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-runtime", version = "0.20.0")
implementation(group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-core", version = "1.0.0-RC")

val spekVersion = "2.0.12"

Expand All @@ -68,7 +68,7 @@ dependencies {

tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.freeCompilerArgs = listOf("-Xuse-experimental=kotlin.Experimental")
kotlinOptions.freeCompilerArgs = listOf("-Xuse-experimental=kotlin.RequiresOptIn")
}

configureAssemble()
Expand Down
25 changes: 12 additions & 13 deletions src/main/kotlin/com/charleskorn/kaml/Yaml.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,37 +19,36 @@
package com.charleskorn.kaml

import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerialFormat
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerializationStrategy
import kotlinx.serialization.StringFormat
import kotlinx.serialization.decode
import kotlinx.serialization.encode
import kotlinx.serialization.modules.EmptyModule
import kotlinx.serialization.modules.SerialModule
import kotlinx.serialization.modules.EmptySerializersModule
import kotlinx.serialization.modules.SerializersModule
import org.snakeyaml.engine.v2.api.StreamDataWriter
import java.io.StringWriter

@OptIn(ExperimentalSerializationApi::class)
class Yaml(
override val context: SerialModule = EmptyModule,
override val serializersModule: SerializersModule = EmptySerializersModule,
val configuration: YamlConfiguration = YamlConfiguration()
) : SerialFormat, StringFormat {
override fun <T> parse(deserializer: DeserializationStrategy<T>, string: String): T {
) : StringFormat {
override fun <T> decodeFromString(deserializer: DeserializationStrategy<T>, string: String): T {
val parser = YamlParser(string)
val reader = YamlNodeReader(parser, configuration.extensionDefinitionPrefix)
val rootNode = reader.read()
parser.ensureEndOfStreamReached()

val input = YamlInput.createFor(rootNode, context, configuration, deserializer.descriptor)
return input.decode(deserializer)
val input = YamlInput.createFor(rootNode, serializersModule, configuration, deserializer.descriptor)
return input.decodeSerializableValue(deserializer)
}

override fun <T> stringify(serializer: SerializationStrategy<T>, value: T): String {
override fun <T> encodeToString(serializer: SerializationStrategy<T>, value: T): String {
val writer = object : StringWriter(), StreamDataWriter {
override fun flush() { }
}

val output = YamlOutput(writer, context, configuration)
output.encode(serializer, value)
val output = YamlOutput(writer, serializersModule, configuration)
output.encodeSerializableValue(serializer, value)

return writer.toString()
}
Expand Down
136 changes: 70 additions & 66 deletions src/main/kotlin/com/charleskorn/kaml/YamlInput.kt

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions src/main/kotlin/com/charleskorn/kaml/YamlOutput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

package com.charleskorn.kaml

import kotlinx.serialization.CompositeEncoder
import kotlinx.serialization.KSerializer
import kotlinx.serialization.PolymorphicKind
import kotlinx.serialization.SerialDescriptor
import kotlinx.serialization.StructureKind
import kotlinx.serialization.builtins.AbstractEncoder
import kotlinx.serialization.modules.SerialModule
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.descriptors.PolymorphicKind
import kotlinx.serialization.descriptors.StructureKind
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.AbstractEncoder
import kotlinx.serialization.encoding.CompositeEncoder
import kotlinx.serialization.modules.SerializersModule
import org.snakeyaml.engine.v2.api.DumpSettings
import org.snakeyaml.engine.v2.api.StreamDataWriter
import org.snakeyaml.engine.v2.common.FlowStyle
Expand All @@ -40,9 +40,10 @@ import org.snakeyaml.engine.v2.events.SequenceStartEvent
import org.snakeyaml.engine.v2.events.StreamStartEvent
import java.util.Optional

@OptIn(ExperimentalSerializationApi::class)
internal class YamlOutput(
writer: StreamDataWriter,
override val context: SerialModule,
override val serializersModule: SerializersModule,
private val configuration: YamlConfiguration
) : AbstractEncoder() {
private val settings = DumpSettings.builder().build()
Expand Down Expand Up @@ -87,7 +88,7 @@ internal class YamlOutput(
return super.encodeElement(descriptor, index)
}

override fun beginStructure(descriptor: SerialDescriptor, vararg typeSerializers: KSerializer<*>): CompositeEncoder {
override fun beginStructure(descriptor: SerialDescriptor): CompositeEncoder {
when (descriptor.kind) {
is PolymorphicKind -> shouldReadTypeName = true
StructureKind.LIST -> emitter.emit(SequenceStartEvent(Optional.empty(), Optional.empty(), true, FlowStyle.BLOCK))
Expand All @@ -111,7 +112,7 @@ internal class YamlOutput(
}
}

return super.beginStructure(descriptor, *typeSerializers)
return super.beginStructure(descriptor)
}

override fun endStructure(descriptor: SerialDescriptor) {
Expand Down
Loading

0 comments on commit 938d250

Please sign in to comment.