Note
Buildable fork of yamlkt 0.8.0 See latest run at https://github.com/Kylmakalle/yamlkt/actions/workflows/Build-Artifact.yml
Warning
MacOS builds require build and dependencies installation (Java, Gradle, etc.) via Apple Rosetta 2, which is kinda tricky. As well as altering targets to support new architectures like macosArm64
Fast multi-platform YAML with comments support for kotlinx.serialization
This project is in alpha state.
Dependency requirements:
| yamlkt | Kotlin | kotlinx.serialization |
|---|---|---|
| 0.3.3 | 1.3.70+ | 0.20.0 |
| 0.5.3 | 1.4.0 | 1.0.0-RC |
| 0.6.0 | 1.4.10 | 1.0.0-RC2 |
| 0.8.0 | 1.4.10 | 1.0.1 |
repositories {
jcenter()
}implementation("net.mamoe.yamlkt:yamlkt:0.8.0")If your project is multiplatform, you need only to add this dependency for commonMain.
<repository>
<name>jcenter</name>
<url>https://jcenter.bintray.com/</url>
</repository>Only JVM is available for Maven.
<dependency>
<groupId>net.mamoe.yamlkt</groupId>
<artifactId>yamlkt</artifactId>
<version>0.8.0</version>
</dependency>This library supports:
- fast deserializing YAML text to a structured object
- contextual and polymorphic serialization:
@Contextual,@Polymorphic - dynamic types:
YamlDynamicSerializerwhich works onAny YamlElementwrapper classes, allowingYamlMap.getInt,YamlMap.getLong- comments encoding (Using annotation
Comment)
The features that aren't yet supported:
- Anchors (
*,&) - Explicit types (e.g.
!!map) - Multiline string (
|,>,\)
This approach is fastest and recommended way as the type is already provided.
@Serializable
data class Test(
val test: String,
val optional: String = "optional", // Having default value means optional
val nest: Nested,
val list: List<String>
) {
@Serializable
data class Nested(
val numberCast: Int
)
}
println(Yaml.default.parse(Test.serializer(), """
test: testString
nest:
numberCast: 0xFE
list: [str, "str2"]
"""))YamlKt provides a contextual serializer YamlDynamicSerializer for Any
and YamlNullDynamicSerializer for Any?
By default, YamlDynamicSerializer is installed to Any.
You can start by using @Contextual:
@Serializable
data class Test(
val any: @Contextual Any
)
Yaml.default.parse(Test.serializer(), yamlText)For input YAML text:
test: { key1: v1, key2: [v2, v3, v4] }Alternatively, you can deserialize without any class:
val map: Map<String?, Any?> = Yaml.default.parseMap("""test: { key1: v1, key2: [v2, v3, v4] }""")YamlElement is a type-safe way to deserialize without descriptors.
val map: YamlMap = Yaml.default.decodeYamlMapFromString("""test: { key1: v1, key2: [v2, v3, v4] }""")Annotate your comments to a field(property) using @Comment:
Example:
@Serializable
data class User(
@Comment("The name of the user")
val name: String = "value"
)gives yaml text:
# The name of the user
name: ""