Skip to content

yaml

YAML serialization and deserialization.

Types

TypeDescription
Data A value decoded from YAML. A mapping key may be any YAML value.
Encodable A value that encodes as YAML.

Data = (nil | Bool | Int | Float | Str | Array[Data] | Dict[Data, Data])

A value decoded from YAML. A mapping key may be any YAML value.

Encodable = (nil | Bool | Int | Float | Str | Sym | Array[Encodable] | Tuple[...Encodable] | Record[...{...Encodable, ...Sym: Encodable}] | Dict[Encodable, Encodable])

A value that encodes as YAML.

Functions

decode yaml -> Data

Parses a YAML string into a Do value.

YAML Value Do Type
null nil
boolean Bool
integer Int
float Float
string Str
sequence Array
mapping Dict

Parameters

NameTypeDescription
yaml Str YAML document to parse.

Errors

ValueError if the input is invalid, contains more than one document, or uses unsupported YAML features such as tags or aliases.

Example

let doc = decode |
  name: Alice
  enabled: true
  ports:
    - 80
    - 443
assert_eq $doc["name"] "Alice"
assert_eq $doc["enabled"] true
assert_eq $doc["ports"] [80, 443]
Open in playground

encode value -> Str

Serializes a Do value to a YAML string.

Do Type YAML Value
nil null
Bool boolean
Int integer
Float float
Str string
Sym string
Array sequence
Tuple sequence
Dict mapping
Record mapping

Parameters

NameTypeDescription
value Encodable The value to serialize.

Errors

TypeError if a value is not YAML-representable, such as binary data or a custom object.

Example

assert_eq (encode nil) "~"
assert_eq (encode [1, 2, 3]) "- 1\n- 2\n- 3"