How to load YAML data and map to Java Objects in SnakeYAML?

[Updated: Jul 27, 2017, Created: Jul 27, 2017]

In this tutorial, we will learn how to map YAML data to Java Objects in SnakeYAML.

Mapping to Java Object

src/main/resources/person.yml

name: Joe
phone: 111-111-11111
address: Park Dr, Charlie Hill
public class Person {
  private String name;
  private String phone;
  private String address;
    .............
}
public class LoadAsJavaObject {
  public static void main(String[] args) throws IOException {
      Yaml yaml = new Yaml();
      try (InputStream in = LoadAsJavaObject.class
              .getResourceAsStream("/person.yml")) {
          Person person = yaml.loadAs(in, Person.class);
          System.out.println(person);
      }
  }
}

Output

Person{name='Joe', phone='111-111-11111', address='Park Dr, Charlie Hill'}

Mapping to list of Java objects

src/main/resources/persons.yml

persons:
 - name: Joe
   phone: 111-111-1111
   address: Park Dr, Charlie Hill

 - name: Trish
   phone: 111-111-11111
   address: Small Vine, River Town
public class Persons {
  private List<Person> persons;
    .............
}
public class LoadAsJavaObjectList {

  public static void main(String[] args) throws IOException {
      Yaml yaml = new Yaml();
      try (InputStream in = LoadAsJavaObject.class.getResourceAsStream("/persons.yml")) {
          Persons persons = yaml.loadAs(in, Persons.class);
          for (Person person : persons.getPersons()) {
              System.out.println(person);
          }
      }
  }
}

Output

Person{name='Joe', phone='111-111-1111', address='Park Dr, Charlie Hill'}
Person{name='Trish', phone='111-111-11111', address='Small Vine, River Town'}

Load As Map

public class LoadAsMap {

  public static void main(String[] args) throws IOException {
      loadFromFile("/person.yml");
      loadFromFile("/persons.yml");
  }

  private static void loadFromFile(String path) throws IOException {
      System.out.printf("-- loading from %s --%n", path);
      Yaml yaml = new Yaml();
      try (InputStream in = LoadAsMap.class.getResourceAsStream(path)) {
          Iterable<Object> itr = yaml.loadAll(in);
          for (Object o : itr) {
              System.out.println("Loaded object type:" + o.getClass());
              System.out.println(o);
          }
      }
  }
}

Output

-- loading from /person.yml --
Loaded object type:class java.util.LinkedHashMap
{name=Joe, phone=111-111-11111, address=Park Dr, Charlie Hill}
-- loading from /persons.yml --
Loaded object type:class java.util.LinkedHashMap
{persons=[{name=Joe, phone=111-111-1111, address=Park Dr, Charlie Hill}, {name=Trish, phone=111-111-11111, address=Small Vine, River Town}]}

Example Project

Dependencies and Technologies Used :

  • snakeyaml 1.18: YAML 1.1 parser and emitter for Java.
  • JDK 1.8
  • Maven 3.3.9

Yaml Java Object Mapping Examples Select All Download
  • yml-java-object-mapping
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources

See Also