yaml
The yaml module provides YAML parsing and stringification. It implements a line-based parser in pure Wren that supports common YAML features including nested structures, lists, and scalar types.
import "yaml" for Yaml
Yaml Class
Yaml
YAML parsing and stringification
Static Methods
Yaml.parse(text) → Map|List|String|Num|Bool|null
Parses a YAML string and returns the corresponding Wren value.
- text (String) - YAML string to parse
- Returns: Parsed value (Map, List, String, Num, Bool, or null)
var data = Yaml.parse("name: Alice\nage: 30")
System.print(data["name"]) // Alice
System.print(data["age"]) // 30
var list = Yaml.parse("- one\n- two\n- three")
System.print(list[0]) // one
Yaml.stringify(value) → String
Converts a Wren value to a YAML string with default indentation (2 spaces).
- value (any) - Value to stringify
- Returns: YAML string
var yaml = Yaml.stringify({"name": "Alice", "age": 30})
System.print(yaml)
// name: Alice
// age: 30
Yaml.stringify(value, indent) → String
Converts a Wren value to a YAML string with custom indentation.
- value (any) - Value to stringify
- indent (Num) - Number of spaces for indentation
- Returns: YAML string
var yaml = Yaml.stringify({"server": {"host": "localhost"}}, 4)
System.print(yaml)
// server:
// host: localhost
Supported Features
Key-Value Pairs
var data = Yaml.parse("
name: Alice
age: 30
email: alice@example.com
")
System.print(data["name"]) // Alice
Nested Maps
var config = Yaml.parse("
database:
host: localhost
port: 5432
credentials:
user: admin
password: secret
")
System.print(config["database"]["host"]) // localhost
System.print(config["database"]["credentials"]["user"]) // admin
Lists
var data = Yaml.parse("
languages:
- Wren
- C
- Python
")
for (lang in data["languages"]) {
System.print(lang)
}
Lists of Maps
var nav = Yaml.parse("
pages:
- file: index
title: Home
- file: about
title: About Us
")
for (page in nav["pages"]) {
System.print(page["title"])
}
Comments
var data = Yaml.parse("
# This is a comment
name: test
# Another comment
value: 123
")
System.print(data["name"]) // test
Data Types
var types = Yaml.parse("
string: hello world
number: 42
float: 3.14
bool_true: true
bool_false: false
null_value: null
tilde_null: ~
quoted: \"with:special chars\"
")
System.print(types["string"]) // hello world (String)
System.print(types["number"]) // 42 (Num)
System.print(types["bool_true"]) // true (Bool)
System.print(types["null_value"]) // null
Type Mapping
| YAML Type | Wren Type |
|---|---|
| mapping | Map |
| sequence | List |
| string | String |
| integer/float | Num |
| true/false | Bool |
| null/~ | null |
Examples
Configuration File
import "yaml" for Yaml
import "io" for File
var config = Yaml.parse(File.read("config.yaml"))
System.print("Server: %(config["server"]["host"]):%(config["server"]["port"])")
System.print("Database: %(config["database"]["url"])")
Building and Stringifying
import "yaml" for Yaml
var data = {
"name": "Project",
"version": "1.0.0",
"dependencies": ["wren", "libuv"],
"settings": {
"debug": true,
"timeout": 30
}
}
System.print(Yaml.stringify(data))
Round-Trip
import "yaml" for Yaml
var original = "
title: Test
items:
- first
- second
"
var parsed = Yaml.parse(original)
var reserialized = Yaml.stringify(parsed)
var reparsed = Yaml.parse(reserialized)
System.print(parsed["title"] == reparsed["title"]) // true
Note
This module implements a subset of YAML 1.2 suitable for configuration files. Advanced features like anchors, aliases, multi-line strings with | or >, and tags are not supported.
Warning
Strings containing special characters (:, #, quotes) should be quoted in YAML input. The stringify method handles this automatically.