YAML
1 Examples
2 YAML Expressions
yaml?
yaml-null
yaml-struct?
yaml-struct
3 Reading YAML
read-yaml
read-yaml*
string->yaml
string->yaml*
file->yaml
file->yaml*
4 Writing YAML
write-yaml
write-yaml*
yaml->string
yaml*->string
yaml->file
yaml*->file
6.3.90.900

YAML

Erik Silkensen <eriksilkensen@gmail.com>

 (require yaml) package: 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

procedure

(yaml? v)  boolean?

  v : any/c
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:

parameter

(yaml-null)  any/c

(yaml-null null)  void?
  null : any/c
A parameter that determines the Racket value that corresponds to a YAML “null” (empty scalar) value. It is 'null by default.

procedure

(yaml-struct? v)  boolean?

  v : any/c
Returns #t if v is an instance of a YAML structure, #f otherwise.

syntax

(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

procedure

(read-yaml [in])  yaml?

  in : input-port? = (current-input-port)
Parses the first YAML document from in and returns the corresponding YAML expression in Racket.

procedure

(read-yaml* [in])  (listof yaml?)

  in : input-port? = (current-input-port)
Like read-yaml, but parses all YAML documents from in and returns a list of the corresponding YAML expressions in Racket.

procedure

(string->yaml str)  yaml?

  str : string?
Equivalent to

(with-input-from-string str read-yaml)

procedure

(string->yaml* str)  (listof yaml?)

  str : string?
Equivalent to

(with-input-from-string str read-yaml*)

procedure

(file->yaml path [#:mode mode-flag])  yaml?

  path : path-string?
  mode-flag : (or/c 'binary 'text) = 'binary
Equivalent to

(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
Equivalent to

(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)
Equivalent to

(write-yaml* (list document) out ....)

Accepts the same keyword arguments as write-yaml* (represented above by ....).

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
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.

procedure

(yaml->string document)  string?

  document : yaml?
Equivalent to
(with-output-to-string
  (λ () (write-yaml document ....)))
Accepts the same keyword arguments as write-yaml (represented above by ....).

procedure

(yaml*->string documents)  string?

  documents : (listof yaml?)
Equivalent to
(with-output-to-string
  (λ () (write-yaml* documents ....)))
Accepts the same keyword arguments as write-yaml* (represented above by ....).

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
Equivalent to
(with-output-to-file
  path
  (λ () (write-yaml document ....))
  #:mode mode-flag
  #:exists exists-flag)
Accepts the same keyword arguments as write-yaml (represented above by ....).

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
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 ....).