Search code examples
javaserializationhashmapyamlsnakeyaml

missing output from serialize with hashMap, YAML


Hi I'm trying to learn how to use snakeYAML.

I want to save a Library object so I can load it again when I start my application. Simply I want to store my library with books in it. Internet told me yaml was a good way to do this.

I have to following class:

public class Library {
private HashMap<String, List<Book>> library; 

public Library() {
    library = new HashMap<String, List<Book>>();
}

//getter
public HashMap<String, List<Book>> getHashMap() {
    return library;
}

//setter
public void setHashMap(HashMap<String, List<Book>> library) {
    this.library = library;
}
}

and now I want to serialize it using a main method:

public static void main(String[] args) {

    Library library = new Library();
    LinkedList<Book> books = new LinkedList<Book>();

    books.add(new Book("Some title", false));
    books.add(new Book("Other Title", true));

    library.putMany("books", books);

    System.out.println(new Yaml().dump(books));

but I only get the output:

- !!model.Book {done: false, title: Some title}
- !!model.Book {done: true, title: Other Title}

and something tells me I'm missing out on something like the Library itself.


Solution

  • That must be because you are only dump()ing the list of two books, not the entire Library.