Enterprise Java

Get to Know Adapters: JSON Binding Overview Series

An adapter configures custom object creation and serialization by implementing the JsonbAdapter interface. The methods adaptToJson() and adaptFromJson() are overwritten with logic that performs the serialization and deserialization operation.

The next article is about the most advanced way to customize JSON-B with custom serializers and deserializers.

In the example implementation here, the adaptToJson() method has been implemented with code that transforms the Booklet object into a JsonObject using the JSON object builder from the JSON Processing API. The adaptFromJson() method constructs a Booklet object from a JsonObject instance.

public class BookletAdapter implements JsonbAdapter<Booklet, JsonObject> {

    @Override
    public JsonObject adaptToJson(Booklet booklet) {
        return Json.createObjectBuilder()
           .add("title", booklet.getTitle())
           .add("firstName", booklet.getAuthor().getFirstName())
           .add("lastName", booklet.getAuthor().getLastName())
           .build();
    }

    @Override
    public Booklet adaptFromJson(JsonObject json) {
        Booklet booklet = new Booklet(json.getString("title"),
        new Author(json.getString("firstName"),
        json.getString("lastName")));
        return booklet;
    }
}

As you can see, the adaptToJson() method flattens the Author object to two properties: firstName and lastName. The adaptFromJson() method reconstructs the Author object and outputs a Booklet instance.

The JsonbAdapter is very flexible and can be used to customize the serialization and deserialization of individual fields, as well as entire objects.

This is achieved by marking the field, method or class that should be customized with the JsonbTypeAdapter annotation and passing it the class name of the JsonbAdapter to use.

An example implementation is shown here. The firstName field is marked with the JsonbTypeAdapter annotation and the FirstNameAdapter class specified as the adapter.

public class Author {
    @JsonbTypeAdapter(FirstNameAdapter.class)
    private String firstName;
}

public class FirstNameAdapter implements JsonbAdapter<String, JsonValue> {

    @Override
    public JsonValue adaptToJson(String fullName) {
        return Json.createValue(fullName.subSequence(0, 1).toString());
    }

    @Override
    public String adaptFromJson(JsonValue json) {
        return json.toString();
    }

}

And finally, the most advanced way to customize JSON-B with custom serializers and deserializers.

There is plenty more to know about the JSON Binding API than what I talk about in these blog posts.

Published on Java Code Geeks with permission by Alex Theedom, partner at our JCG program. See the original article here: Get to Know Adapters: JSON Binding Overview Series

Opinions expressed by Java Code Geeks contributors are their own.

Alex Theedom

Alex Theedom is a Senior Java Developer and has recently played a pivotal role in the architectural design and development of a microservice based, custom built lottery and instant win game platform. Alex has experience of Java web application development in a diverse range of fields including finance, e-learning, lottery and software development. He is the co-author of Professional Java EE Design Patterns and many articles.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button