Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize yaml to list of objects

Tags:

java

snakeyaml

Say I have a Yaml file like this,

people:
- name         : Joe
  surname    : Barber
  age : 16
- name         : Andy
  surname    : Lots
  age : 17

And I have a class like this,

public class people {
    private String name;
    private String surname;
    private String age;

<!-- With getters and setters -->
}

How would i go about getting a list of people objects from the Yaml file? Just getting the value from a key in the file is fairly simple but mapping it to a collection of objects is not. I am using the snakeYaml lib.

like image 959
Ernie Avatar asked Mar 12 '14 12:03

Ernie


People also ask

How do you create an array of objects in YAML?

An array is a group of similar values with a single name. In YAML, Array represents a single key mapped to multiple values. Each value starts with a hyphen - symbol followed by space. In a single line, write the same thing above using 'square brackets syntax.

How do you declare an array in YAML?

The block sequence style of YAML uses hyphens or dashes to ( - ) to represent arrays. A hyphen ( - ) followed by white space ( ) represents an element of an array. When you enter the dashes, you need to ensure that all items are at the same indentation level.


1 Answers

i hope this can help you.

public class StackOverflow {

public static void main(String[] args) {
    final URL resource = StackOverflow.class.getResource("people.yaml");
    final Constructor peopleContructor = new Constructor(Group.class);
    final TypeDescription peopleDescription = new TypeDescription(People.class);
    peopleDescription.putMapPropertyType("people", People.class, Object.class);
    peopleContructor.addTypeDescription(peopleDescription);

    final Yaml yaml = new Yaml(peopleContructor);
    try {
        final Group group = (Group) yaml.load(resource.openStream());
        for (final People people : group.getPeople()) {
            System.out.println(people);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static class People {
    private String name;
    private String surname;
    private int age;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSurname() {
        return surname;
    }
    public void setSurname(String surname) {
        this.surname = surname;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "People: {name: " + this.name + ", surname: " + this.surname + ", age: " + this.age + "}";
    }
}
public static class Group {
    private List<People> people;

    public List<People> getPeople() {
        return people;
    }

    public void setPeople(List<People> people) {
        this.people = people;
    }
}}
like image 165
Jahroots Avatar answered Sep 22 '22 12:09

Jahroots