Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allowed characters in map key identifier in YAML?

Which characters are and are not allowed in a key (i.e. example in example: "Value") in YAML?

like image 751
Mats Stijlaart Avatar asked Jan 30 '12 14:01

Mats Stijlaart


People also ask

Does YAML accept special characters?

YAML doesn't require quoting most strings but you'll want to quote special characters if they fall within a certain list of characters. Use quotes if your value includes any of the following special characters: { , } , [ , ] , & , * , # , ? , | , - , < , > , = , ! , % , @ , : also ` and , YAML Spec.

Is underscore allowed in YAML?

If a definition only includes numbers and underscores, it is parsed by YAML as an integer and all underscores are stripped. To ensure the object becomes a string, it should be surrounded by quotes.


2 Answers

According to the YAML 1.2 specification simply advises using printable characters with explicit control characters being excluded (see here):

In constructing key names, characters the YAML spec. uses to denote syntax or special meaning need to be avoided (e.g. '#' denotes comment, '>' denotes folding, '-' denotes list, etc.).

Essentially, you are left to the relative coding conventions (restrictions) by whatever code (parser/tool implementation) that needs to consume your YAML document. The more you stick with alphanumerics the better; it has simply been our experience that the underscore has worked with most tooling we have encountered.

It has been a shared practice with others we work with to convert the period character '.' to an underscore character '_' when mapping namespace syntax that uses periods to YAML. Some people have similarly used hyphens successfully, but we have seen it misconstrued in some implementations.

like image 75
Matt Rutkowski Avatar answered Oct 23 '22 13:10

Matt Rutkowski


Any character (if properly quoted by either single quotes 'example' or double quotes "example"). Please be aware that the key does not have to be a scalar ('example'). It can be a list or a map.

like image 23
Andrey Avatar answered Oct 23 '22 13:10

Andrey