YamlWriter

object YamlWriter

Writes serialized configuration data to YAML format with comment support.

This writer produces clean, human-readable YAML output and supports two comment placement modes:

  • CommentPlacement.INLINE: Single-line comments are placed on the same line as the value (e.g., port: 8080 # The server port). Falls back to ABOVE placement for multi-line comments, lists, and map sections.

  • CommentPlacement.ABOVE: Comments are placed on separate lines above the property. An empty line is inserted before section-level comments for readability.

Scalar values are automatically quoted when they contain special YAML characters (:, #, {, }, etc.), look like booleans (true/false), nulls, or numbers.

Example:

val data = mapOf("server" to mapOf("port" to 8080, "host" to "localhost"))
val comments = mapOf("server.port" to CommentData(listOf("The server port"), CommentPlacement.INLINE))

YamlWriter.write(File("config.yml"), data, comments)
// Produces:
// server:
// port: 8080 # The server port
// host: localhost

Since

1.0

See also

Functions

Link copied to clipboard
fun write(file: File, data: Map<String, Any?>, comments: Map<String, CommentData> = emptyMap())

Writes serialized configuration data to a file in YAML format.

Link copied to clipboard
fun writeToString(data: Map<String, Any?>, comments: Map<String, CommentData> = emptyMap()): String

Renders serialized configuration data to a YAML-formatted string.