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

YAML

Erik Silkensen <ejs@ccs.neu.edu>

 (require (planet esilkensen/yaml:3:=1)) package: base

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

- {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? 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

procedure

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

  str : string?
Equivalent to

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