(sigil yaml)
(sigil yaml) - YAML Parsing and Serialization
Streaming YAML encoding and decoding for Sigil. Provides bidirectional conversion between YAML and native Scheme data structures using ports for composability with files and other I/O.
Type Mapping
| YAML | Scheme |
|---|---|
| mapping | dict: #{ name: "Alice" } |
| sequence | list: (1 2 3) |
| string | string: "hello" |
| integer | integer: 42 |
| float | float: 3.14 |
| true | #t |
| false | #f |
| null | 'null symbol |
Basic Usage
(import (sigil yaml))
;; Decode YAML string
(yaml-decode "name: Alice\nage: 30")
; => #{ name: "Alice" age: 30 }
;; Encode to YAML
(yaml-encode #{ name: "Alice" age: 30 })
; => "name: Alice\nage: 30\n"
;; Decode a sequence
(yaml-decode "- apple\n- banana\n- cherry")
; => ("apple" "banana" "cherry")Port-Based I/O
;; Write YAML to a file
(call-with-output-file "config.yaml"
(lambda (port)
(yaml-write #{ name: "Alice" } port)))
;; Read YAML from a file
(call-with-input-file "config.yaml" yaml-read)Multi-Document
(yaml-decode-all "---\na: 1\n---\nb: 2")
; => (#{ a: 1 } #{ b: 2 })Exports
yaml-readprocedureRead a YAML document from a port.
Parses one complete YAML document from the port and returns the corresponding Scheme value. YAML mappings become dicts with keyword keys, sequences become lists.
;; Read from a string port
(call-with-input-string "name: Alice\nage: 30" yaml-read)
; => #{ name: "Alice" age: 30 }
;; Read from a file
(call-with-input-file "config.yaml" yaml-read)
;; Read sequences
(call-with-input-string "- apple\n- banana" yaml-read)
; => ("apple" "banana")yaml-read-allprocedureRead all YAML documents from a port.
Parses all documents separated by --- markers and returns them as a list.
(call-with-input-string "---\na: 1\n---\nb: 2" yaml-read-all)
; => (#{ a: 1 } #{ b: 2 })yaml-decodeprocedureDecode a YAML string into a Scheme value.
Parses a YAML string and returns the corresponding Scheme value. YAML mappings become dicts with keyword keys, sequences become lists, and scalars map to their Scheme equivalents.
;; Decode mappings (become dicts)
(yaml-decode "name: Alice\nage: 30")
; => #{ name: "Alice" age: 30 }
;; Decode sequences (become lists)
(yaml-decode "- 1\n- 2\n- 3")
; => (1 2 3)
;; Decode scalars
(yaml-decode "true") ; => #t
(yaml-decode "42") ; => 42
(yaml-decode "null") ; => nullyaml-decode-allprocedureDecode all YAML documents from a string.
(yaml-decode-all "---\na: 1\n---\nb: 2")
; => (#{ a: 1 } #{ b: 2 })yaml-writeprocedureWrite a Scheme value as YAML to a port.
Writes YAML directly to the port without creating intermediate strings. By default writes block style; use flow: #t for inline flow style.
;; Write to stdout
(yaml-write #{ name: "Alice" } (current-output-port))
; outputs: name: Alice\n
;; Write to a file
(call-with-output-file "config.yaml"
(lambda (port)
(yaml-write #{ name: "Alice" age: 30 } port)))
;; Flow style
(yaml-write #{ a: 1 } (current-output-port) flow: #t)
; outputs: {a: 1}yaml-encodevariableEncode a Scheme value as a YAML string.
Converts a Scheme value to its YAML string representation. Returns block-style YAML by default. Use flow: #t for compact inline style.
;; Encode dicts as YAML mappings
(yaml-encode #{ name: "Alice" age: 30 })
; => "name: Alice\nage: 30\n"
;; Encode lists as sequences
(yaml-encode '("apple" "banana"))
; => "- apple\n- banana\n"
;; Flow style
(yaml-encode #{ a: 1 b: 2 } flow: #t)
; => "{a: 1, b: 2}"yaml-null?procedureCheck if a value represents YAML null.
YAML null is represented as the symbol 'null in Scheme. Use this predicate to distinguish null from #f (YAML false).
(yaml-null? 'null) ; => #t
(yaml-null? #f) ; => #f
(yaml-null? '()) ; => #f
(yaml-null? (yaml-decode "null")) ; => #t
(yaml-null? (yaml-decode "~")) ; => #t
(yaml-null? (yaml-decode "false")) ; => #f