I'm having trouble deserializing JSON arrays to optional lists and adding them to MongoDB using morphia. Instead of adding the actual elements of the array to Mongo, the size of the array is added. See code below...
BookWrapper Class
public class BookWrapper {
private List<Book> books;
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
}
Book Class
@Entity("Book")
public class Book {
private String name;
private Optional<List<String>> authors = Optional.empty();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Optional<List<String>> getAuthors() {
return authors;
}
public void setAuthors(Optional<List<String>> authors) {
this.authors = authors;
}
}
Upload Books Method
public void uploadBook() throws IOException {
File file = new File("data/book.json");
mapper.registerModule(new Jdk8Module());
BookWrapper wrapper = mapper.readValue(file, BookWrapper.class);
List<Book> books = wrapper.getBooks();
for(Book book : books) {
db.save(book);
}
}
book.json File
{
"books" : [
{
"name": "Best Book Ever",
"authors": ["John Smith", "Mike Apple"]
},
{
"name": "Tale of Legends",
"authors": ["Anne Lumo", "Andre John"]
}
]
}
Mongo Inserted Document no elements, only the size of the array!