Consider the following document:
foo: bar: Yes According to the spec, this should be interpreted as a Boolean, not as a String.
However, it seems that this document yields the same interpretation:
foo: bar: 'Yes' If I misunderstand, and 'Yes' (in quotes) should in fact be understood as a String-typed value, where in the specs can I find justification for this?
However, if I do interpret correctly, and these documents are equivalent according to spec, how can I specify a string with the value "Yes" as the value of a YAML property?
2 Answers
It depends ;-)
In YAML 1.1 the bool type is defined as following:
A Boolean represents a true/false value. Booleans are formatted as English words (“true”/“false”, “yes”/“no” or “on”/“off”) for readability and may be abbreviated as a single character “y”/“n” or “Y”/“N”.
In YAML 1.2 the bool type is defined as following:
Booleans: [ true, True, false, FALSE ]
Assigning the value Yes to a key is done via quotes:
foo: 'Yes' bar: "Yes" Assigning a boolean and to be compatible with future versions of YAML parsers should be done with
foo: false bar: True You can play around yourself with YAML syntax at the
3The most recent YAML specification (you link to something replaced 9 years ago) states:
Application specific tag resolution rules should be restricted to resolving the “?” non-specific tag, most commonly to resolving plain scalars. These may be matched against a set of regular expressions to provide automatic resolution of integers, floats, timestamps, and similar types.
A boolean is one of these "similar types". So True would be interpreted as a boolean and "True" or 'True' (because these are not plain scalars) as strings.
In the outdated YAML 1.1 specification Yes and On (and their opposites, and all of these in all-caps, all-lowercase) were also interpreted as booleans, but this notion was dropped from the 1.2 specification.
So you want to represent the string "Yes" (without the quotes) as a value, and if you are sure your data will be read by a parser updated after 2009, then you can use a plain scalar, single quotes, double quotes or, e.g. a block style literal scalar (assuming a sequence with single key/value mappings, with key [1, 2]):
- [1, 2]: Yes - [1, 2]: 'Yes' - [1, 2]: "Yes" - [1, 2]: | Yes All but the first will also work with a parser that only supports the YAML 1.1 standard. You can of course make sure that later processing "knows" about what you expect by explicitly starting your document with the YAML directive and the end-of-directive indicator
%YAML 1.2 --- 2