module

YAML

The stdlib's require "yaml" (also pulled in by big/yaml, uri/yaml and uuid/yaml) defines the same YAML module. Loaded after cryaml it would compile, because both load the same Any/Nodes/schema files, and its PullParser and Builder methods would silently replace cryaml's and link libyaml. A finished hook runs after every file is loaded, so this check catches either order.

Class methods

build(io : IO, &) : Nil

Writes YAML into the given IO. A YAML::Builder is yielded to the block.

Source
build

Returns the resulting String of writing YAML to the yielded YAML::Builder.

require "yaml"

string = YAML.build do |yaml|
  yaml.mapping do
    yaml.scalar "foo"
    yaml.sequence do
      yaml.scalar 1
      yaml.scalar 2
    end
  end
end
string # => "---\nfoo:\n- 1\n- 2\n"
Source
dump(object, io : IO) : Nil

Serializes an object to YAML, writing it to io.

Source
dump(object) : String

Serializes an object to YAML, returning it as a String.

Source
libyaml_version

Returns the version of libyaml whose behavior is reproduced.

No libyaml is linked: the scanner, parser and emitter are a pure Crystal port of libyaml 0.2.5. Code that branches on this version (for example to account for the document end marker fix in 0.2.1) keeps working.

Source
parse(data : String | IO) : Any

Deserializes a YAML document according to the core schema.

# ./foo.yml
data:
  string: "foobar"
  array:
    - John
    - Sarah
  hash: {key: value}
  paragraph: |
    foo
    bar
require "yaml"

YAML.parse(File.read("./foo.yml"))
# => {
# => "data" => {
# => "string" => "foobar",
# => "array" => ["John", "Sarah"],
# => "hash" => {"key" => "value"},
# => "paragraph" => "foo\nbar\n"
# => }
Source
parse_all(data : String) : Array(Any)

Deserializes multiple YAML documents according to the core schema.

# ./foo.yml
foo: bar
---
hello: world
require "yaml"

YAML.parse_all(File.read("./foo.yml"))
# => [{"foo" => "bar"}, {"hello" => "world"}]
Source

Nested types