Mapping objects
The first thing to learn in JSON-B API, is basically mapping Java objects from/to JSON strings. The entry point to use mapping and other JSON-B features are the Jsonb
object. The Jsonb
object is instantiated through a JsonbBuilder
class, as will be shown in the following example.
After creating the jsonb
object, the methods toJson()
and fromJson()
serialize an object to a JSON string and deserialize an object from a JSON string, respectively. In the following example, we will show how to serialize a movie object into a JSON string:
public class Movie { private long id; private String title; // setters and getters here } // instantiating a jsonb object Jsonb jsonb = JsonbBuilder.create(); jsonb.toJson(jsonb); //creating a movie object Movie movie = new Movie(); movie.setId(15); movie.setTitle("Beauty and The Beast"); // mapping the movie object into a json string String json = jsonb.toJson(movie); System.out.println(json);
By running the...