6.3.90.900
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)))) |
|
- {avg: 0.278, name: Mark McGwire, hr: 65} | - {avg: 0.288, name: Sammy Sosa, hr: 63} |
|
|
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)) |
!!struct:player {name: Mark McGwire, hr: 65, avg: 0.278} |
|
> (string->yaml | "!!struct:player {hr: 65, avg: 0.278, name: Mark McGwire}") |
|
(player "Mark McGwire" 65 0.278) |
2 YAML Expressions
Returns #t if v is a YAML expression, #f otherwise.
This library defines a subset of Racket values that can be represented as
YAML strings, and this predicate checks for such values.
A
YAML expression is one of:
A parameter that determines the Racket value that corresponds to a YAML
“null” (empty scalar) value. It is 'null by default.
Returns #t if v is an instance of a YAML structure,
#f otherwise.
(yaml-struct id maybe-super (field ...) | struct-option ...) |
|
Creates a new structure that can be used as a YAML expression. This
macro expands to a call to
struct that uses
#:methods
to register itself with the YAML implementation.
3 Reading YAML
Parses the first
YAML document
from
in and returns the corresponding YAML expression in Racket.
Like
read-yaml, but parses
all
YAML documents
from
in and returns a list of the corresponding YAML expressions
in Racket.
(string->yaml str) → yaml?
|
str : string? |
Equivalent to
Equivalent to
(file->yaml path [#:mode mode-flag]) → yaml?
|
path : path-string? |
mode-flag : (or/c 'binary 'text) = 'binary |
Equivalent to
(file->yaml* path [#:mode mode-flag]) → (listof yaml?)
|
path : path-string? |
mode-flag : (or/c 'binary 'text) = 'binary |
Equivalent to
4 Writing YAML
Equivalent to
(write-yaml* (list document) out ....)
Accepts the same keyword arguments as write-yaml*
(represented above by ....).
Writes a sequence of Racket YAML expressions to
out as YAML
text formatted with the keyword arguments. See the
YAML specification
for more information on style.
(yaml->string document) → string?
|
document : yaml? |
Equivalent to
Accepts the same keyword arguments as write-yaml
(represented above by ....).
Equivalent to
Accepts the same keyword arguments as write-yaml*
(represented above by ....).
(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 |
|
Equivalent to
Accepts the same keyword arguments as write-yaml
(represented above by ....).
(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 |
|
Equivalent to
(with-output-to-file |
path |
(λ () (write-yaml* documents ....)) |
#:mode mode-flag |
#:exists exists-flag) |
Accepts the same keyword arguments as write-yaml*
(represented above by ....).