yaml

Is it possible to have a list in the root section?


Is it possible to have a list in the root section of a YAML file? So far I've seen no files that follow such a structure and I've been asking myself whether it violates the syntax.

Here is an example:

- 'entry A'
- 'entry B'
- 'entry C'

What I've seen so far:

list:
  - 'entry A'
  - 'entry B'
  - 'entry C'

In other words, is the list: section obsolete?


Solution

  • It's ok to do that.
    Here is a Java sample which use snakeYaml:

    Yaml yaml = new Yaml();
    Object o = yaml.load("- 'entry A'\n- 'entry B'\n- 'entry C'");
    System.out.println(o.getClass().getName());
    

    The output of the code is java.util.ArrayList.

    But in a real scenario, we store an object's content into a YAML file. When we do that, if the type of one field is a list, we actually store it in the way as you always see:

    field-name:
    - item1
    - item2