Mapping collections
Collections can also be mapped from/to JSON arrays. In the following example, we will create a collection of multiple JSON objects, and then serialize it into a JSON array:
Movie movie1 = new Movie(); movie1.setId(15); movie1.setTitle("Beauty and The Beast"); Movie movie2 = new Movie(); movie2.setId(16); movie2.setTitle("The Boss Baby"); Movie movie3 = new Movie(); movie3.setId(17); movie3.setTitle("Suicide Squad"); // creating a list of the movie objects List<Movie> movies = new ArrayList<>(); movies.add(movie1); movies.add(movie2); movies.add(movie3); Jsonb jsonb = JsonbBuilder.create(); jsonb.toJson(jsonb); // convert to json String json = jsonb.toJson(movies); System.out.println(json);
By running the previous example, we will have the following output:
[{"id":15,"title":"Beauty and The Beast"},{"id":16,"title":"The Boss Baby"},{"id":17,"title":"Suicide Squad"}]
In order to deserialize a JSON array back into a collection, we will...