Java Jackson @JsonAlias Example

1. Introduction

The @JsonAlias annotation in Jackson provides a way to specify alternative names for properties during deserialization. This is useful when the incoming JSON might have variations in the naming of some properties, and we want our deserialization process to remain flexible to handle those variations.

2. Example Steps

1. Create a User class with a username field.

2. Use the @JsonAlias annotation on the username field to specify multiple potential JSON property names that can be mapped to this field.

3. Deserialize different JSON strings containing variations of the user's username property.

4. Print the deserialized User objects to verify that alternative property names were correctly mapped to the username field.

3. Java Jackson @JsonAlias Example

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonAlias;

public class JsonAliasExample {

    public static void main(String[] args) throws Exception {
        // Sample JSON inputs with different variations for the username property
        String json1 = "{\"username\":\"john_doe\"}";
        String json2 = "{\"user_name\":\"john_doe\"}";
        String json3 = "{\"userAlias\":\"john_doe\"}";

        ObjectMapper objectMapper = new ObjectMapper();

        // Deserialize each JSON string and print the result
        System.out.println(objectMapper.readValue(json1, User.class));
        System.out.println(objectMapper.readValue(json2, User.class));
        System.out.println(objectMapper.readValue(json3, User.class));
    }

    public static class User {
        // Use @JsonAlias to specify alternative names for the username property
        @JsonAlias({"user_name", "userAlias"})
        private String username;

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        @Override
        public String toString() {
            return "User{username='" + username + "'}";
        }
    }
}

Output:

User{username='john_doe'}
User{username='john_doe'}
User{username='john_doe'}

4. Step By Step Explanation

In the above code, the User class has a field named username. Since the incoming JSON can have variations in how the username property is named, we've annotated the username field with @JsonAlias and specified alternative names (user_name and userAlias) that it might appear as in the JSON.

During deserialization, Jackson considers both the field's actual name (username) and its aliases (user_name, userAlias) as valid property names for mapping the value to the username field.

We have three sample JSON strings with different names for the username property. When each JSON string is deserialized using Jackson's ObjectMapper, regardless of the variation in the username property's naming, the value is correctly mapped to the username field in the User object, as confirmed by the printed output.


Comments