Skip to content

Commit

Permalink
Update to kotlinx.serialization 0.11.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskorn committed Apr 15, 2019
1 parent 1d7645b commit a556d4b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 47 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ repositories {
dependencies {
compile(kotlin("stdlib-jdk8", "1.3.30"))
compile(group = "org.snakeyaml", name = "snakeyaml-engine", version = "1.0")
compile(group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-runtime", version = "0.10.0")
compile(group = "org.jetbrains.kotlinx", name = "kotlinx-serialization-runtime", version = "0.11.0")

val spekVersion = "2.0.0"

Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/com/charleskorn/kaml/Yaml.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ 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 org.snakeyaml.engine.v1.api.StreamDataWriter
import java.io.StringWriter

class Yaml(val extensionDefinitionPrefix: String? = null) : AbstractSerialFormat(), StringFormat {
class Yaml(val extensionDefinitionPrefix: String? = null, override val context: SerialModule = EmptyModule) : AbstractSerialFormat(context), StringFormat {
override fun <T> parse(deserializer: DeserializationStrategy<T>, string: String): T {
val parser = YamlParser(string)
val reader = YamlNodeReader(parser, extensionDefinitionPrefix)
Expand Down
14 changes: 7 additions & 7 deletions src/main/kotlin/com/charleskorn/kaml/YamlInput.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialDescriptor
import kotlinx.serialization.StructureKind
import kotlinx.serialization.UpdateMode
import kotlinx.serialization.context.SerialContext
import kotlinx.serialization.modules.SerialModule
import kotlinx.serialization.internal.EnumDescriptor

sealed class YamlInput(val node: YamlNode, override var context: SerialContext) : ElementValueDecoder() {
sealed class YamlInput(val node: YamlNode, override var context: SerialModule) : ElementValueDecoder() {
companion object {
fun createFor(node: YamlNode, context: SerialContext): YamlInput = when (node) {
fun createFor(node: YamlNode, context: SerialModule): YamlInput = when (node) {
is YamlScalar -> YamlScalarInput(node, context)
is YamlNull -> YamlNullInput(node, context)
is YamlList -> YamlListInput(node, context)
Expand All @@ -44,7 +44,7 @@ sealed class YamlInput(val node: YamlNode, override var context: SerialContext)
abstract fun getCurrentLocation(): Location
}

private class YamlScalarInput(val scalar: YamlScalar, context: SerialContext) : YamlInput(scalar, context) {
private class YamlScalarInput(val scalar: YamlScalar, context: SerialModule) : YamlInput(scalar, context) {
override fun decodeString(): String = scalar.content
override fun decodeInt(): Int = scalar.toInt()
override fun decodeLong(): Long = scalar.toLong()
Expand Down Expand Up @@ -73,7 +73,7 @@ private class YamlScalarInput(val scalar: YamlScalar, context: SerialContext) :
override fun getCurrentLocation(): Location = scalar.location
}

private class YamlNullInput(val nullValue: YamlNode, context: SerialContext) : YamlInput(nullValue, context) {
private class YamlNullInput(val nullValue: YamlNode, context: SerialModule) : YamlInput(nullValue, context) {
override fun decodeNotNullMark(): Boolean = false

override fun decodeValue(): Any = throw UnexpectedNullValueException(nullValue.location)
Expand All @@ -83,7 +83,7 @@ private class YamlNullInput(val nullValue: YamlNode, context: SerialContext) : Y
override fun getCurrentLocation(): Location = nullValue.location
}

private class YamlListInput(val list: YamlList, context: SerialContext) : YamlInput(list, context) {
private class YamlListInput(val list: YamlList, context: SerialModule) : YamlInput(list, context) {
private var nextElementIndex = 0
private lateinit var currentElementDecoder: YamlInput

Expand Down Expand Up @@ -125,7 +125,7 @@ private class YamlListInput(val list: YamlList, context: SerialContext) : YamlIn
override fun getCurrentLocation(): Location = currentElementDecoder.node.location
}

private class YamlMapInput(val map: YamlMap, context: SerialContext) : YamlInput(map, context) {
private class YamlMapInput(val map: YamlMap, context: SerialModule) : YamlInput(map, context) {
private val entriesList = map.entries.entries.toList()
private var nextIndex = 0
private lateinit var currentEntry: Map.Entry<YamlNode, YamlNode>
Expand Down
104 changes: 66 additions & 38 deletions src/test/kotlin/com/charleskorn/kaml/YamlTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@ import kotlinx.serialization.ContextualSerialization
import kotlinx.serialization.Decoder
import kotlinx.serialization.Encoder
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Optional
import kotlinx.serialization.SerialDescriptor
import kotlinx.serialization.Serializable
import kotlinx.serialization.Serializer
import kotlinx.serialization.context.SimpleModule
import kotlinx.serialization.internal.BooleanSerializer
import kotlinx.serialization.internal.ByteSerializer
import kotlinx.serialization.internal.CharSerializer
Expand All @@ -45,6 +43,7 @@ import kotlinx.serialization.internal.StringSerializer
import kotlinx.serialization.internal.makeNullable
import kotlinx.serialization.list
import kotlinx.serialization.map
import kotlinx.serialization.modules.serializersModuleOf
import kotlinx.serialization.serializer
import org.spekframework.spek2.Spek
import org.spekframework.spek2.style.specification.describe
Expand Down Expand Up @@ -1042,9 +1041,8 @@ object YamlTest : Spek({
override fun serialize(encoder: Encoder, obj: Inner) = throw UnsupportedOperationException()
}

val module = SimpleModule(Inner::class, contextSerializer)
val parser = Yaml()
parser.install(module)
val module = serializersModuleOf(Inner::class, contextSerializer)
val parser = Yaml(context = module)

context("given some input that should be parsed with a dynamically installed serializer") {
val input = """
Expand Down Expand Up @@ -1220,34 +1218,40 @@ object YamlTest : Spek({
val output = Yaml.default.stringify(IntSerializer.list, listOf(1, 2, 3))

it("returns the value serialized in the expected YAML form") {
assert(output).toBe("""
assert(output).toBe(
"""
- 1
- 2
- 3
""".trimIndent())
""".trimIndent()
)
}
}

context("serializing a list of nullable integers") {
val output = Yaml.default.stringify(makeNullable(IntSerializer).list, listOf(1, null, 3))

it("returns the value serialized in the expected YAML form") {
assert(output).toBe("""
assert(output).toBe(
"""
- 1
- null
- 3
""".trimIndent())
""".trimIndent()
)
}
}

context("serializing a list of strings") {
val output = Yaml.default.stringify(StringSerializer.list, listOf("item1", "item2"))

it("returns the value serialized in the expected YAML form") {
assert(output).toBe("""
assert(output).toBe(
"""
- "item1"
- "item2"
""".trimIndent())
""".trimIndent()
)
}
}

Expand All @@ -1260,13 +1264,15 @@ object YamlTest : Spek({
val output = Yaml.default.stringify(IntSerializer.list.list, input)

it("returns the value serialized in the expected YAML form") {
assert(output).toBe("""
assert(output).toBe(
"""
- - 1
- 2
- 3
- - 4
- 5
""".trimIndent())
""".trimIndent()
)
}
}

Expand All @@ -1285,11 +1291,13 @@ object YamlTest : Spek({
val output = Yaml.default.stringify(serializer, input)

it("returns the value serialized in the expected YAML form") {
assert(output).toBe("""
assert(output).toBe(
"""
- "key1": "value1"
"key2": "value2"
- "key3": "value3"
""".trimIndent())
""".trimIndent()
)
}
}

Expand All @@ -1302,10 +1310,12 @@ object YamlTest : Spek({
val output = Yaml.default.stringify(SimpleStructure.serializer().list, input)

it("returns the value serialized in the expected YAML form") {
assert(output).toBe("""
assert(output).toBe(
"""
- name: "name1"
- name: "name2"
""".trimIndent())
""".trimIndent()
)
}
}
}
Expand All @@ -1320,10 +1330,12 @@ object YamlTest : Spek({
val output = Yaml.default.stringify((StringSerializer to StringSerializer).map, input)

it("returns the value serialized in the expected YAML form") {
assert(output).toBe("""
assert(output).toBe(
"""
"key1": "value1"
"key2": "value2"
""".trimIndent())
""".trimIndent()
)
}
}

Expand All @@ -1342,13 +1354,15 @@ object YamlTest : Spek({
val output = Yaml.default.stringify(serializer, input)

it("returns the value serialized in the expected YAML form") {
assert(output).toBe("""
assert(output).toBe(
"""
"map1":
"key1": "value1"
"key2": "value2"
"map2":
"key3": "value3"
""".trimIndent())
""".trimIndent()
)
}
}

Expand All @@ -1362,7 +1376,8 @@ object YamlTest : Spek({
val output = Yaml.default.stringify(serializer, input)

it("returns the value serialized in the expected YAML form") {
assert(output).toBe("""
assert(output).toBe(
"""
"list1":
- 1
- 2
Expand All @@ -1371,7 +1386,8 @@ object YamlTest : Spek({
- 4
- 5
- 6
""".trimIndent())
""".trimIndent()
)
}
}

Expand All @@ -1385,12 +1401,14 @@ object YamlTest : Spek({
val output = Yaml.default.stringify(serializer, input)

it("returns the value serialized in the expected YAML form") {
assert(output).toBe("""
assert(output).toBe(
"""
"item1":
name: "name1"
"item2":
name: "name2"
""".trimIndent())
""".trimIndent()
)
}
}
}
Expand All @@ -1401,9 +1419,11 @@ object YamlTest : Spek({
val output = Yaml.default.stringify(SimpleStructure.serializer(), input)

it("returns the value serialized in the expected YAML form") {
assert(output).toBe("""
assert(output).toBe(
"""
name: "The name"
""".trimIndent())
""".trimIndent()
)
}
}

Expand All @@ -1416,12 +1436,14 @@ object YamlTest : Spek({
val output = Yaml.default.stringify(NestedObjects.serializer(), input)

it("returns the value serialized in the expected YAML form") {
assert(output).toBe("""
assert(output).toBe(
"""
firstPerson:
name: "name1"
secondPerson:
name: "name2"
""".trimIndent())
""".trimIndent()
)
}
}

Expand All @@ -1430,31 +1452,37 @@ object YamlTest : Spek({
val output = Yaml.default.stringify(Team.serializer(), input)

it("returns the value serialized in the expected YAML form") {
assert(output).toBe("""
assert(output).toBe(
"""
members:
- "name1"
- "name2"
""".trimIndent())
""".trimIndent()
)
}
}

context("serializing an object with a nested map") {
@Serializable
data class ThingWithMap(val variables: Map<String, String>)

val input = ThingWithMap(mapOf(
"var1" to "value1",
"var2" to "value2"
))
val input = ThingWithMap(
mapOf(
"var1" to "value1",
"var2" to "value2"
)
)

val output = Yaml.default.stringify(ThingWithMap.serializer(), input)

it("returns the value serialized in the expected YAML form") {
assert(output).toBe("""
assert(output).toBe(
"""
variables:
"var1": "value1"
"var2": "value2"
""".trimIndent())
""".trimIndent()
)
}
}
}
Expand All @@ -1479,7 +1507,7 @@ data class ComplexStructure(
val enum: TestEnum,
val boolean: Boolean,
val char: Char,
@Optional val nullable: String? = null
val nullable: String? = null
)

@Serializable
Expand Down

0 comments on commit a556d4b

Please sign in to comment.