@JsonAnySetter Jackson Annotation

@JsonAnySetter annotation allows the non-static setter method to deserialize unmapped/unrecognized JSON properties. It’s not mandatory to populate unrecognized properties in a map using the setter method, but for simplicity, we are populating them in a map. We can also use @JsonAnyGetter annotation at the getter method of the same map to serialize these unrecognized properties as standard properties.

There are a few points to remember about the @JsonAnySetter annotation –

  • @JsonAnySetter only works with the deserialization of JSON. If you want a similar functionality for serialization as well, then @JsonAnyGetter will do the trick for you
  • @JsonAnySetter can be used on field level if you are using the latest version of Jackson annotations. It can also be put on a non-static setter method
  • The setter method must accept two arguments –
    • One is the JSON property name, which will become the key to the map
    • And second would be the value of that JSON key
    @JsonAnySetter 
    private Map<String, Object> values = new HashMap<>();

	public Map<String, Object> getValues() {
		return values;
	}

	public void setValues(String key, Object value) {
		this.values.put(key, value);
	}

This would work in the latest versions of Jackson’s annotations

    private Map<String, Object> values = new HashMap<>();
    
    @JsonAnySetter 
	public Map<String, Object> getValues() {
		return values;
	}

	public void setValues(String key, Object value) {
		this.values.put(key, value);
	}

This is not right, and things won’t work as intended

    private Map<String, Object> values = new HashMap<>();

	public Map<String, Object> getValues() {
		return values;
	}
   
    @JsonAnySetter 
	public void setValues(String key, Object value) {
		this.values.put(key, value);
	}

This is correct

    private Map<String, Object> values = new HashMap<>();

	public Map<String, Object> getValues() {
		return values;
	}
   
    @JsonAnySetter 
	public void setValues(Map<String,Object> values) {
		this.values = values
	}

This is wrong, and you will get errors.

In the example below, @JsonAnySetter annotation is applied at the setValues() method that accepts key and value parameters. In that method, we have used this key-value pair to populate it into the map.

package com.codekru.model;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Person {

    private String firstName;
    private String lastName;
    private Map<String, Object> values = new HashMap<>();

    public Person() {
        super();
    }

    public Person(String firstName, String lastName, Map<String, Object> values) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.values = values;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Map<String, Object> getValues() {
        return values;
    }

    @JsonAnySetter
    public void setValues(String key, Object value) {
        this.values.put(key, value);
    }

    public static void main(String[] args) throws JsonProcessingException {
        String personJson = "{\"firstName\" : \"Codekru\", \"lastName\" : \"Programming\", \"profession\" : \"blogging\", \"age\" : 20}";
        ObjectMapper mapper = new ObjectMapper();
        Person person = mapper.readValue(personJson, Person.class);
        System.out.println(person.getValues().get("age")); // 20
        System.out.println(person.getValues().get("profession")); // blogging
    }
}

Output –

20
blogging

References: https://fasterxml.github.io/jackson-annotations/javadoc/2.6/com/fasterxml/jackson/annotation/JsonAnySetter.html

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.

Other Jackson Annotations –

Liked the article? Share this on

Leave a Comment

Your email address will not be published. Required fields are marked *