Home » Java » Enterprise Java » Get to Know Adapters: JSON Binding Overview Series

About Alex Theedom

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.

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.

(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments