DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Quality Crisis in Robotics and How Video Annotation Fixes It
  • Active Learning and Human-in-the-Loop for NLP Annotation and Model Improvement
  • How Does Video Annotation Augment Computer Vision?

Trending

  • A Practical Blueprint for Deploying Agentic Solutions
  • Every Cache Miss Is a Tiny Tax on Your Performance
  • How to Interpret the Number of Spring ApplicationContexts in Integration Tests
  • The Middleware Gap in AI Agent Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Jackson Mix-In Annotations

Jackson Mix-In Annotations

Take a look at how a Jackson mix-in class or interface, as well as some annotations, can help you with (de)serialization when you don't have source code access to POJOs.

By 
John Thompson user avatar
John Thompson
·
Jan. 12, 18 · Tutorial
Likes (10)
Comment
Save
Tweet
Share
23.8K Views

Join the DZone community and get the full member experience.

Join For Free

Prior to Jackson 1.2, the only way to serialize or deserialize JSON using Jackson was by using one of the following two methods:

  • Adding annotations to modify the POJO classes
  • Writing custom serializers and deserializers

Now imagine you want to serialize or deserialize a third-party POJO which you don’t have access to its source code. What would you do?

Also, you might want your code clean and open to another JSON library, such as GSON.

What would you do to decouple your code from Jackson annotations?

Jackson mix-in annotations help you resolve these kinds of problems. These annotations are used in a mix-in class or interface but function as if they were directly included in the target class.

In this post, we will look at how to use Jackson mix-in annotations.

Sample Application

Let's create a simple Spring Boot application to understand how Jackson mix-in annotation works.

Say you want to serialize or deserialize a User POJO in a Spring Boot application.

Here is the code of the User POJO.

User.java:

package guru.springframework.blog.domain;

public class User {
    private long id;
    private String name;
    private int age;
    private String gender;
    private String email;
    private String phoneNo;

    public User(long id, String name, int age, String gender, String email, String phoneNo) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.email = email;
        this.phoneNo = phoneNo;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                ", email='" + email + '\'' +
                ", phoneNo=" + phoneNo +
                '}';
    }
}


In the preceding code, User is a typical POJO, but it is not designed to be used with data binding. The User class doesn’t have the default constructor, nor any getter/setter methods.

Let’s assume that you don’t have access to the source code of the User POJO or there is some constraint disallowing you from modifying the existing POJO. In this scenario, you can’t serialize or deserialize a User object through annotations or by defining your own custom serializer and deserializer.

Let us see how mix-in annotations can solve this problem.

The Jackson Mix-in Class

For mix-in annotations, you first need to define a mix-in class or interface.

Let’s define an abstract mix-in class for User. Ensure that the mix-in class has a constructor matching the source POJO.

Use the @JsonCreator annotation on the constructor and the @JsonProperty property to specify all the properties of the POJO.

Here is the code for the UserMixin Jackson mix-in class.

UserMixin.java:

package guru.springframework.blog.mixin;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public abstract class UserMixin {
    @JsonCreator
    public UserMixin(@JsonProperty Long id, @JsonProperty String name,
                     @JsonProperty int age, @JsonProperty String gender,
                     @JsonProperty String email, @JsonProperty String phoneNo) {

    }
}


UserMixin is an abstract class where the constructor of the class is annotated with @JsonCreator to tell Jackson in what order to pass fields from a JSON object to the constructor.

Each argument of the constructor is annotated with @JsonProperty to indicate the name of the property to bind to.

After creating the UserMixin class, you must configure the ObjectMapper to use the mix-in for the User POJO, like this:

package guru.springframework.blog.mixin;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public abstract class UserMixin {
    @JsonCreator
    public UserMixin(@JsonProperty Long id, @JsonProperty String name,
                     @JsonProperty int age, @JsonProperty String gender,
                     @JsonProperty String email, @JsonProperty String phoneNo) {

    }
}


Here is the complete test code to test the Jackson mix-in.

UserTest.java:

package guru.springframework.blog.domain;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import guru.springframework.blog.mixin.UserMixin;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class UserTest {
    private User user;

    @Before
    public void setUp(){
        user = new User(123,"James",23,"Male",
                "[email protected]", "1234567890");
    }
    @After
    public void tearDown(){
        user = null;
    }

    @Test
    public void JacksonMixinAnnotationTest() throws JsonProcessingException{
        ObjectMapper objectMapper = buildMapper();
        objectMapper.addMixIn(User.class, UserMixin.class);
        String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(user);
        System.out.println(json);

    }

    private static ObjectMapper buildMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setVisibilityChecker(objectMapper.getSerializationConfig()
                .getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withCreatorVisibility(JsonAutoDetect.Visibility.NONE));
        return objectMapper;
    }

}


As you can see in the code, ObjectMapper is configured in the buildMapper() method.

In the test method, an ObjectMapper is created and the addMixIn() method is called on it. The addMixIn() method configures the association between the mix-in and target classes to be used during serialization.

Here is the output of the Jackson Mix-in test from IntelliJ:

Jackson Mixin Test Results

Jackson (API) Annotation

Published at DZone with permission of John Thompson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Stop Poisoning Your Models: How I Built a CV Dataset Quality Toolkit I Can Reuse Forever
  • Quality Crisis in Robotics and How Video Annotation Fixes It
  • Active Learning and Human-in-the-Loop for NLP Annotation and Model Improvement
  • How Does Video Annotation Augment Computer Vision?

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook