YAML
This module provides utilities for parsing and emitting data in the YAML data serialization format to and from Racket values. See the YAML web site for more information about YAML. The implementation is ported from PyYAML.
1 Examples
As a quick introduction, this section shows an example of using the module to go from Racket values to YAML and back again.
The write-yaml procedure turns a Racket value into YAML output. Here it displays a sequence of mappings in flow style (by default):
> (write-yaml '(#hash(("name" . "Mark McGwire") ("hr" . 65) ("avg" . 0.278)) #hash(("name" . "Sammy Sosa") ("hr" . 63) ("avg" . 0.288))))
- {hr: 65, avg: 0.278, name: Mark McGwire}
- {hr: 63, avg: 0.288, name: Sammy Sosa}
The string->yaml procedure turns YAML text into a Racket value. Here it constructs the same sequence of mappings from above, where this time the YAML is in block style:
> (string->yaml (string-append "- name: Mark McGwire\n" " hr: 65\n" " avg: 0.278\n" "- name: Sammy Sosa\n" " hr: 63\n" " avg: 0.288\n"))
'(#hash(("hr" . 65) ("avg" . 0.278) ("name" . "Mark McGwire"))
#hash(("hr" . 63) ("avg" . 0.288) ("name" . "Sammy Sosa")))
The yaml-struct macro defines a struct that can be written to and read from YAML:
> (yaml-struct player (name hr avg) #:transparent)
> (write-yaml (player "Mark McGwire" 65 0.278)) !!struct:player {name: Mark McGwire, avg: 0.278, hr: 65}
> (string->yaml "!!struct:player {hr: 65, avg: 0.278, name: Mark McGwire}") (player "Mark McGwire" 65 0.278)
2 YAML Expressions
procedure
(yaml-struct? v) → boolean?
v : any/c
syntax
(yaml-struct id maybe-super (field ...) struct-option ...)
3 Reading YAML
procedure
in : input-port? = (current-input-port)
procedure
(read-yaml* [in]) → (listof yaml?)
in : input-port? = (current-input-port)
procedure
(string->yaml str) → yaml?
str : string?
procedure
(string->yaml* str) → (listof yaml?)
str : string?
4 Writing YAML
procedure
(write-yaml document [out]) → void?
document : yaml? out : output-port? = (current-output-port)
(write-yaml* (list document) out ....)
procedure
(write-yaml* documents [ out #:canonical canonical #:indent indent #:width width #:explicit-start explicit-start #:explicit-end explicit-end #:scalar-style scalar-style #:style style]) → void? documents : (listof yaml?) out : output-port? = (current-output-port) canonical : boolean? = #f indent : exact-positive-integer? = 2 width : exact-positive-integer? = 80 explicit-start : boolean? = #f explicit-end : boolean? = #f scalar-style : (or/c #\" #\' #\| #\> 'plain) = 'plain style : (or/c 'block 'flow 'best) = 'best
procedure
(yaml->string document) → string?
document : yaml?
(with-output-to-string (λ () (write-yaml document ....)))
procedure
(yaml*->string documents) → string?
documents : (listof yaml?)
(with-output-to-string (λ () (write-yaml* documents ....)))