YAML
This module provides utilities for parsing and emitting data in the YAML data serialization format. The implementation is hosted at GitHub and is based on PyYAML. See the YAML web site for more information about YAML.
1 YAML Expressions
This module defines a subset of Racket values that can be represented as or constructed from YAML, and this predicate checks for such values.
The following table shows how the standard YAML tags, in addition to a number of Racket-specific tags, correspond to values in Racket.
YAML tag |
| Racket type |
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
| ||
!!racket/pair |
| |
!!racket/vector |
| |
!!racket/symbol |
|
This module can be extended with support for application-specific tags using custom representers and constructors, and this predicate checks for those values as well.
procedure
(yaml-null? v) → boolean?
v : any/c
> (yaml-null? 'null) #t
> (yaml-null? '()) #f
> (parameterize ([yaml-null '()]) (yaml-null? '())) #t
2 Reading YAML
procedure
(read-yaml [ in #:allow-undefined? allow-undefined?]) → yaml? in : input-port? = (current-input-port) allow-undefined? : boolean? = #f
The #:allow-undefined? keyword argument controls whether an undefined tag should raise an exception (the default behavior), or be interpreted as either a scalar, sequence, or mapping value.
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?
procedure
(file->yaml path [#:mode mode-flag]) → yaml?
path : path-string? mode-flag : (or/c 'binary 'text) = 'binary
procedure
(file->yaml* path [#:mode mode-flag]) → (listof yaml?)
path : path-string? mode-flag : (or/c 'binary 'text) = 'binary
> (string->yaml "!invoice {id: 1, total: 251.42}") string:0:0: could not determine a constructor for the tag
!invoice
> (string->yaml "!invoice {id: 1, total: 251.42}" #:allow-undefined? #t) '#hash(("id" . 1) ("total" . 251.42))
> (string->yaml* (string-append "# Ranking of 1998 home runs\n" "---\n" "- Mark McGwire\n" "- Sammy Sosa\n" "- Ken Griffey\n" "\n" "# Team ranking\n" "---\n" "- Chicago Cubs\n" "- St. Louis Cardinals\n"))
'(("Mark McGwire" "Sammy Sosa" "Ken Griffey")
("Chicago Cubs" "St. Louis Cardinals"))
3 Writing YAML
procedure
(write-yaml document [ out #:canonical? canonical? #:indent indent #:width width #:explicit-start? explicit-start? #:explicit-end? explicit-end? #:scalar-style scalar-style #:style style #:sort-mapping mapping-less-than? #:sort-mapping-key mapping-extract-key]) → void? document : 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 mapping-less-than? : (or/c (any/c any/c . -> . any/c) #f) = #f mapping-extract-key : (any/c . -> . any/c) = identity
(sort (hash->list mapping) mapping-less-than? #:key mapping-extract-key)
> (write-yaml 42 #:canonical? #t)
---
!!int "42"
> (write-yaml '#hash(("Apple" . 3.99) ("Orange" . 2.15) ("Cherry" . 4.12)) #:style 'block #:sort-mapping string<? #:sort-mapping-key car)
Apple: 3.99
Cherry: 4.12
Orange: 2.15
procedure
(write-yaml* documents [out]) → void?
documents : (listof yaml?) out : output-port? = (current-output-port)
procedure
(yaml->string document) → string?
document : yaml?
procedure
(yaml*->string documents) → string?
documents : (listof yaml?)
procedure
(yaml->file document path) → void?
document : yaml? path : path-string?
procedure
(yaml*->file documents path) → void?
documents : (listof yaml?) path : path-string?
4 Extending YAML
This module provides a simple API for extending the set of Racket values that can be written to or read from YAML. The idea is to write functions that represent Racket values as and construct them from YAML nodes.
> (struct player (name hr avg) #:transparent)
procedure
v : any/c
procedure
(scalar-node? v) → boolean?
v : any/c
procedure
(sequence-node? v) → boolean?
v : any/c
procedure
(mapping-node? v) → boolean?
v : any/c
4.1 Representers
parameter
(yaml-representers representers) → void? representers : (listof yaml-representer?)
procedure
(yaml-representer? v) → boolean?
v : any/c
procedure
(yaml-representer type? represent) → yaml-representer?
type? : (any/c . -> . boolean?) represent : (any/c . -> . node?)
> (define (represent-player p) (define mapping (make-hash)) (hash-set! mapping "name" (player-name p)) (hash-set! mapping "hr" (player-hr p)) (hash-set! mapping "avg" (player-avg p)) (represent-mapping "!player" mapping))
> (define player-representer (yaml-representer player? represent-player))
> (parameterize ([yaml-representers (list player-representer)]) (write-yaml (player "Mark McGwire" 65 0.278))) !player {avg: 0.278, name: Mark McGwire, hr: 65}
procedure
(represent-scalar tag str) → scalar-node?
tag : string? str : string?
procedure
(represent-sequence tag lst) → sequence-node?
tag : string? lst : (listof yaml?)
procedure
(represent-mapping tag hash) → mapping-node?
tag : string? hash : (hash/c yaml? yaml?)
4.2 Constructors
parameter
(yaml-constructors constructors) → void? constructors : (listof yaml-constructor?)
procedure
(yaml-constructor? v) → boolean?
v : any/c
procedure
(yaml-constructor type? tag construct) → yaml-constructor?
type? : (any/c . -> . boolean?) tag : string? construct : (node? . -> . yaml?)
> (define (construct-player node) (define mapping (construct-mapping node)) (player (hash-ref mapping "name") (hash-ref mapping "hr") (hash-ref mapping "avg")))
> (define player-constructor (yaml-constructor player? "!player" construct-player))
> (parameterize ([yaml-constructors (list player-constructor)]) (string->yaml "!player {name: Sammy Sosa, hr: 63, avg: 0.288}")) (player "Sammy Sosa" 63 0.288)
procedure
v : any/c
Note that yaml-multi-constructor? is a subtype of yaml-constructor?.
procedure
(yaml-multi-constructor type? tag-prefix construct) → yaml-multi-constructor? type? : (any/c . -> . boolean?) tag-prefix : string? construct : (string? node? . -> . yaml?)
procedure
(construct-scalar node) → string?
node : scalar-node?
procedure
(construct-sequence node) → (listof yaml?)
node : sequence-node?
procedure
(construct-mapping node) → (hash/c yaml? yaml?)
node : mapping-node?