YAML in depth

safe_load

YAML is JSON with comments, anchors and a friendlier syntax, and Ruby has it in the standard library through Psych. YAML.safe_load turns a document into Ruby values without building arbitrary objects:

ruby -e 'require "yaml"; p YAML.safe_load("a: 1")'

{"a" => 1}

Keys become strings, indentation is the structure, and a scalar that looks like a number becomes one. YAML.load is the old name: since Psych 4 it is safe by default as well, but it permits symbols, so new code still calls safe_load.

dump

YAML.dump writes the document, and the result starts with --- and lists the sequence items with a dash:

ruby -e 'require "yaml"; puts YAML.dump({"a" => 1, "list" => [1, 2]})'

--- a: 1 list: - 1 - 2

A round trip through dump and safe_load gives the same values, but not the same text: comments and the original formatting are lost, while the order of the keys is kept. For a config a person edits, that is the reason a program writes it only when asked.

Anchors and aliases

An anchor names a value and an alias reuses it. The feature is what makes a YAML config shorter, and it is off by default in safe_load because an alias can expand exponentially:

ruby -e 'require "yaml"; y = "base: &b\n x: 1\ncopy: *b\n"; p YAML.safe_load(y, aliases: true)'

{"base" => {"x" => 1}, "copy" => {"x" => 1}}

Without the option the load raises, and the message names the switch:

ruby -e 'require "yaml"; y = "base: &b\n x: 1\ncopy: *b\n"; begin; YAML.safe_load(y); rescue => e; puts e.class; puts e.message; end'

Psych::AliasesNotEnabled Alias parsing was not enabled. To enable it, pass `aliases: true` to `Psych::load` or `Psych::safe_load`.

Enable aliases only for a document you control. A config that comes from a user is a place where a small file can expand into a large structure.

Permitted classes

YAML can name a Ruby class, and safe_load refuses to build one unless it is on the list. That is the difference between a data format and a deserialisation gadget:

ruby -e 'require "yaml"; begin; YAML.safe_load("--- !ruby/object:OpenStruct\nname: Ada\n"); rescue => e; puts e.class; puts e.message; end'

Psych::DisallowedClass Tried to load unspecified class: OpenStruct

Add the class when the config really uses it:

ruby -e 'require "yaml"; require "ostruct"; p YAML.safe_load("--- !ruby/object:OpenStruct\nname: Ada\n", permitted_classes: [OpenStruct])'

#<OpenStruct name="Ada">

A project config rarely needs a class at all: strings, numbers, booleans, arrays and hashes are enough. When a config does need a Date or a Symbol, list exactly that class rather than reaching for unsafe_load.

What to watch for

YAML is indentation-sensitive, and a tab where a space belongs is a parse error with a line number. The error is usually accurate; the surprise is that an editor inserted the tab.

JSON is a subset of YAML, so a JSON document loads with safe_load, but a YAML document is not JSON. For data that travels between programs, JSON is the smaller, stricter choice; YAML earns its place in a file a person edits.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? require "yaml" p YAML.safe_load("n: 36\ns: \"36\"\nb: true")

    Answer

    {"n" => 36, "s" => "36", "b" => true}

    An unquoted 36 is a number and an unquoted true is a boolean, while the quotes keep the second value a string.

  2. What does this program print? require "yaml" puts YAML.dump({"a" => [1, 2]})

    Answer

    --- / a: / - 1 / - 2

    dump starts the document with --- and writes each item of the array on its own line with a dash.

  3. What does YAML.safe_load build from a YAML mapping?

    Answer

    A Ruby hash whose keys are strings

    The document becomes ordinary Ruby values, and the keys arrive as strings.

  4. What does this program print? require "yaml" y = "base: &b\n x: 1\ncopy: *b\n" p YAML.safe_load(y, aliases: true)

    Answer

    {"base" => {"x" => 1}, "copy" => {"x" => 1}}

    With aliases enabled the alias expands to the anchored value, so both keys hold their own hash with the same contents.

  5. Which sentence about JSON and YAML is true?

    Answer

    JSON is a subset of YAML, so safe_load reads a JSON document

    A JSON document is valid YAML, but a YAML document may use features JSON does not have.

  6. What does this program print? require "yaml" begin YAML.safe_load("base: &b\n x: 1\ncopy: *b\n") rescue => e puts e.class end

    Answer

    Psych::AliasesNotEnabled

    Aliases are off by default, and the error names the switch that turns them on.

  7. What does this program print? require "yaml" require "ostruct" p YAML.safe_load("--- !ruby/object:OpenStruct\nname: Ada\n", permitted_classes: [OpenStruct])

    Answer

    #<OpenStruct name="Ada">

    Once the class is permitted, the tagged mapping becomes an OpenStruct with the value from the document.

  8. What does this program print? require "yaml" puts YAML.dump({"b" => 1, "a" => 2})

    Answer

    --- / b: 1 / a: 2

    A Ruby hash keeps insertion order and dump writes the keys in that order.

  9. Which document does YAML.safe_load refuse?

    Answer

    One that names a Ruby class that is not on the permitted list

    safe_load builds only the basic types unless a class is named in permitted_classes.

  10. What does this program print? require "yaml" begin YAML.safe_load("a:\n\tb: 1\n") rescue => e puts e.class end

    Answer

    Psych::SyntaxError

    A tab cannot indent a YAML line, so the parser reports a syntax error with its line and column.

Article author: Arthur Isaev