Working with YAML in Kotlin: Libraries, Parsing, and Serialization
YAML (YAML Ain't Markup Language) is a popular data serialization format widely used for configuration files, dependency descriptions, and data exchange. Due to its readability and conciseness, YAML is often preferred over JSON or XML. In the Kotlin ecosystem, there are several mature libraries for working with YAML. In this article, we will look at three main approaches: SnakeYAML, Kaml, and Jackson YAML.
1. SnakeYAML — The Classic Choice
SnakeYAML is the most popular Java library for parsing YAML, which works perfectly in Kotlin as well. It provides a low-level API that allows reading YAML as Map, List, or arbitrary objects.
Adding Dependency (Gradle)
dependencies { implementation("org.yaml:snakeyaml:2.2")}Example: Reading Simple YAML
import org.yaml.snakeyaml.Yaml
fun main() { val yamlString = """ server: host: "localhost" port: 8080 ssl: false database: url: "jdbc:postgresql://localhost/mydb" user: "admin" password: "secret" """.trimIndent()
val yaml = Yaml() val data: Map<String, Any> = yaml.load(yamlString) println(data) // Output: {server={host=localhost, port=8080, ssl=false}, database={url=jdbc:postgresql://localhost/mydb, user=admin, password=secret}}}Example: Mapping to a Data Class
import org.yaml.snakeyaml.Yamlimport org.yaml.snakeyaml.constructor.Constructor
data class ServerConfig( val host: String, val port: Int, val ssl: Boolean)
data class DatabaseConfig( val url: String, val user: String, val password: String)
data class AppConfig( val server: ServerConfig, val database: DatabaseConfig)
fun main() { val yamlString = """ server: host: "localhost" port: 8080 ssl: false database: url: "jdbc:postgresql://localhost/mydb" user: "admin" password: "secret" """.trimIndent()
val yaml = Yaml(Constructor(AppConfig::class.java)) val config: AppConfig = yaml.load(yamlString) println(config.server.host) // localhost println(config.database.port) // Error! port is not in DatabaseConfig}Important: SnakeYAML uses Java reflection, so data classes must have a no-argument constructor or use Constructor with the class specified. For nested structures, manual configuration may be required.
2. Kaml — Native Kotlin Library
Kaml (Kotlin YAML library) is a modern library written in Kotlin using kotlinx.serialization. It provides type safety, support for sealed classes, nullable fields, and custom serializers.
Adding Dependency (Gradle)
dependencies { implementation("net.mamoe.yamlkt:yamlkt:0.13.0") // or implementation("com.charleskorn.kaml:kaml:0.56.0")}We will look at Kaml by charleskorn, as it is more popular and actively developed.
Example: Serialization and Deserialization
import kotlinx.serialization.Serializableimport kotlinx.serialization.encodeToStringimport kotlinx.serialization.decodeFromStringimport com.charleskorn.kaml.Yaml
@Serializabledata class ServerConfig( val host: String = "localhost", val port: Int = 8080, val ssl: Boolean = false)
@Serializabledata class AppConfig( val server: ServerConfig, val name: String)
fun main() { val config = AppConfig( server = ServerConfig(host = "example.com", port = 443, ssl = true), name = "MyApp" )
// Serialization to YAML val yamlO