chickadee » yaml

YAML

Author

Aaron Patterson

Requirements

libyaml

Object mapping

Parsing YAML to Scheme:

Serializing Scheme to YAML:

API

yaml-load inputprocedure

Parse a YAML representation into an object. input may be a string or input port.

Example:

#;1> (import yaml)
#;2> (yaml-load "--- foo")
"foo"
#;3> (yaml-load "--- :bar")
bar
#;4> (yaml-load "--- ")
#<sql-null#sql-null-type>
#;5> (yaml-load "--- ['foo', ['bar']]")
("foo" ("bar"))
#;6> (yaml-load "--- {foo: bar}")
(("foo" . "bar"))
#;7> (yaml-load "--- {foo: bar, bar: baz}")
(("foo" . "bar") ("bar" . "baz"))
#;8>
yaml-parse input stream-start stream-end document-start document-end alias scalar sequence-start sequence-end mapping-start mapping-end seedprocedure

Parse a YAML representation into an object. input may be a string or input port. The remaining arguments are callback procedures along with the initial seed value seed. Each callback procedure accepts zero or more arguments representing the callback event, followed by the seed value. The return value of each callback procedure becomes the new seed value. Finally, the seed value is returned.

The following example prints out a list of all events:

(import scheme)
(import (chicken base))
(import (chicken pretty-print))
(import yaml)

(define (yaml-events yaml)
  (reverse
   (yaml-parse yaml
               (lambda (enc seed)
                 (cons (list 'stream-start enc) seed))
               (lambda (seed)
                 (cons (list 'stream-end) seed))
               (lambda (version tags implicit seed)
                 (cons (list 'document-start version tags implicit) seed))
               (lambda (implicit? seed)
                 (cons (list 'document-end implicit?) seed))
               (lambda (alias seed)
                 (cons (list 'alias alias) seed))
               (lambda (value anchor tag plain quoted style seed)
                 (cons (list 'scalar value anchor tag plain quoted style) seed))
               (lambda (anchor tag implicit style seed)
                 (cons (list 'sequence-start anchor tag implicit style) seed))
               (lambda (seed)
                 (cons '(sequence-end) seed))
               (lambda (anchor tag implicit style seed)
                 (cons (list 'mapping-start anchor tag implicit style) seed))
               (lambda (seed)
                 (cons '(mapping-end) seed))
               '())))

(pp (yaml-events "--- { }"))
yaml-dump object #!optional portprocedure

Serialize object to its YAML representation. If the output port port is given, write it to that port, otherwise return a string.

Example:

#;1> (import yaml sql-null)
#;2> (yaml-dump "foo")
"--- foo\n"
#;3> (yaml-dump 'bar)
"--- :bar\n"
#;4> (yaml-dump (sql-null))
"---\n"
#;5> (yaml-dump '("foo" ("bar")))
"---\n- foo\n- - bar\n"
#;6> (yaml-dump '(("foo" . "bar")))
"---\nfoo: bar\n"
#;7> (yaml-dump '(("foo" . "bar") ("bar" .  "baz")))
"---\nfoo: bar\nbar: baz\n"
#;8>

License

MIT

Source

The source can be found at https://github.com/tenderlove/chicken-yaml .

Contents »