Handling nulls
Null values can be handled in one of two ways with JSON-B:
- Properties with null values are not shown in the resulting JSON string
- Properties with null values are shown with the null value. The choice between one of the techniques is completely up to your implementation considerations.
By default, JSON-B does not show properties with null values when serializing Java objects. In order to display the attributes with null values in the resulting JSON, the @JsonNillable annotation is used on Java classes, to tell the jsonb processor to use the mentioned technique, as shown in the following example:
@JsonbNillable
public class Movie {
private long id;
private String title;
private int productionYear;
// getters and setters
}
JsonbConfig config = new JsonbConfig().withFormatting(true);
Jsonb jsonb = JsonbBuilder.create(config);
Movie movie = new Movie();
movie.setId(15);
movie.setProductionYear(2017);
String json = jsonb.toJson(movie);
System...