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.
See the GitHub repository for more information.
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)))) make-serializer: broke its own contract;
expected 1 value, returned 3 values
in: the range of
(->*
()
(output-port?
#:allow-unicode
boolean?
#:canonical
boolean?
#:explicit-end
boolean?
#:explicit-start
boolean?
#:indent
(or/c exact-positive-integer? #f)
#:line-break
(or/c "\n" "\r" "\r\n" #f)
#:tags
(or/c (hash/c string? string?) #f)
#:version
(or/c
(cons/c exact-integer? exact-integer?)
#f)
#:width
(or/c exact-positive-integer? #f))
(-> node? void?))
contract from:
<pkgs>/yaml/yaml/serializer.rkt
blaming: <pkgs>/yaml/yaml/serializer.rkt
(assuming the contract is correct)
at: <pkgs>/yaml/yaml/serializer.rkt:15.3
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(("avg" . 0.278) ("hr" . 65) ("name" . "Mark McGwire"))
#hash(("avg" . 0.288) ("hr" . 63) ("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)) make-serializer: broke its own contract;
expected 1 value, returned 3 values
in: the range of
(->*
()
(output-port?
#:allow-unicode
boolean?
#:canonical
boolean?
#:explicit-end
boolean?
#:explicit-start
boolean?
#:indent
(or/c exact-positive-integer? #f)
#:line-break
(or/c "\n" "\r" "\r\n" #f)
#:tags
(or/c (hash/c string? string?) #f)
#:version
(or/c
(cons/c exact-integer? exact-integer?)
#f)
#:width
(or/c exact-positive-integer? #f))
(-> node? void?))
contract from:
<pkgs>/yaml/yaml/serializer.rkt
blaming: <pkgs>/yaml/yaml/serializer.rkt
(assuming the contract is correct)
at: <pkgs>/yaml/yaml/serializer.rkt:15.3
> (string->yaml "!!struct:player {hr: 65, avg: 0.278, name: Mark McGwire}") (player "Mark McGwire" 65 0.278)
2 YAML Expressions
3 Reading YAML
procedure
in : input-port? = (current-input-port)
procedure
(read-yaml* [in]) → (listof yaml?)
in : input-port? = (current-input-port)
procedure
str : string?
(with-input-from-string str read-yaml)
(with-input-from-string str read-yaml*)
procedure
path : path-string? mode-flag : (or/c 'binary 'text) = 'binary
(with-input-from-file path read-yaml #:mode mode-flag)
procedure
(file->yaml* path [#:mode mode-flag]) → (listof yaml?)
path : path-string? mode-flag : (or/c 'binary 'text) = 'binary
(with-input-from-file path read-yaml* #:mode mode-flag)
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 ....)))
(with-output-to-string (λ () (write-yaml* documents ....)))
procedure
(yaml->file document path [ #:mode mode-flag #:exists exists-flag]) → string? document : yaml? path : path-string? mode-flag : (or/c 'binary 'text) = 'binary
exists-flag :
(or/c 'error 'append 'update 'replace 'truncate 'truncate/replace) = 'error
(with-output-to-file path (λ () (write-yaml document ....)) #:mode mode-flag #:exists exists-flag)
procedure
(yaml*->file documents path [ #:mode mode-flag #:exists exists-flag]) → string? documents : (listof yaml?) path : path-string? mode-flag : (or/c 'binary 'text) = 'binary
exists-flag :
(or/c 'error 'append 'update 'replace 'truncate 'truncate/replace) = 'error
(with-output-to-file path (λ () (write-yaml* documents ....)) #:mode mode-flag #:exists exists-flag)