What is YAML used for?

Another text data format is YAML (Yet Another Markup Language; later, YAML Ain't Markup Language). It is used to serialize objects for transmission over the network (just as XML and JSON are used this way). Because it is human readable, it is also used to write configuration files, for example, for Docker, Kubernetes, Ansible, etc. When YAML needs to be saved to a file (for example, a configuration file), we use one of two extensions: .yaml or .yml.

Language syntax

In XML, angle brackets (<>) are used to designate tags. In JSON, we use curly brackets ({}). YAML uses new lines and indentation.

The data is stored as key-value pairs, where the key is a string, and the value can be various data types (string, number, true/false, array, etc.). Keys are written without quotation marks.

Let's take a look at how information is stored in YAML:

Type Java YAML
Integer
int number = 5
number: 5
Fractional number
double number = 4.3
number: 4.3
Boolean variable
boolean valid = false
valid: false
valid: no
valid: off

* Valid boolean values: true/false, yes/no, on/off.

String
String city = "New York"
city: New York
city: 'New York'
city: "New York"

* All three options are equivalent.

String with special characters
String line = "aaa\nbbb"
line: "aaa\nbbb"
Comment in code
// comment
# comment
Object
public class Person {
  String name = "Dennis";
  int age = 32;
}

* The object's class is given so you can see the structure of the object.

person:
  name: "Dennis"
  age: 32

* Pay attention to the indentation before the attributes. It must be the same for all attributes.

List of simple values
var ages =
    List.of(1, 3, 5, 9, 78, -5);
ages: [1, 3,5,9,78, -5]
ages:
  - 1
  - 3
  - 5
  - 9
  - 78
  - -5

* Both options are equivalent.
** Each element of the list is marked with a hyphen.

List of objects
class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}List<Person> people = List.of(
        new Person("Ian", 21),
        new Person("Marina", 25),
        new Person("Owen", 73)      );
people:
  - name: "Ian"
    age: 21
  - name: "Marina"
    age: 25
  - name: "Owen"
    age: 73

As in Java, an element of a list can be a list, that is, objects can be nested inside one another. The hyphen that indicates the next element of the list can be horizontally offset relative to the parent key, or located directly below it. The main thing is that all elements have the same format. This will help avoid confusion and an ambiguous nesting hierarchy.

ages:
  - 1
  - 3
  - 5
  - 9
  - 78
  - -5
ages:
- 1
- 3
- 5
- 9
- 78
- -5

There are two more nuances when working with text values:

  1. Multiline text. We can save text like this:

    multilineText: "line 1\nline 2\n....line n"

    But it would be very unpleasant to try to read that. So there is the | (pipe) symbol, which you can use to write the text differently:

    multilineText: |
     line 1
     line 2
     ....
     line n

    You'll agree that the second option is more convenient, right?

  2. Long lines. If you want to keep the text on one line but also want it to fit in the IDE's visible workspace, you can use the > (greater than) symbol.

    singlelineText: >
     begin
     ...
     continue same line
     ...
     end

    All the text will be treated as one line.

If you need to write several YAML data structures into one file, then you need to separate them with --- (three hyphens). In practice, there is rarely a need for this, but it's best to be aware of this possibility.

Example of a YAML document

Let's create some Java data structure (a class) and a corresponding object, and try to represent the object as YAML.

class Family {
   private Date weddingDate;
   private Person wife;
   private Person husband;
   private List<Person> children;

   // Getters and setters are omitted
}

class Person {
   private final String name;
   private final boolean isWoman;
   private int age;

   public Person(String name, int age, boolean isWoman) {
       this.name = name;
       this.age = age;
       this.isWoman = isWoman;
   }

// Getters and setters are omitted

}

public static void main(String[] args) {
   Person wife = new Person("Ann", 37, true);
   Person husband = new Person("Alex", 40, false);
   var children = List.of(
           new Person("Iris", 12, true),
           new Person("Olivia", 5, true)
   );
   Date weddingDate = new Date(/* some long */);

   Family family = new Family();
   family.setWeddingDate(weddingDate);
   family.setWife(wife);
   family.setHusband(husband);
   family.setChildren(children);
}

Valid representation in YAML:

---
weddingDate: 2000-12-03
wife:
 name: Ann
 age: 37
 isWoman: yes
husband:
 name: Alex
 age: 40
 isWoman: no
children:
 - name: Iris
   age: 12
   isWoman: true
 - name: Olivia
   age: 5
   isWoman: true
---