How to Serialize and Deserialize Dynamic Properties with Jackson’s @JsonAnyGetter and @JsonAnySetter
The article explains the requirements and usage of Jackson’s @JsonAnyGetter and @JsonAnySetter annotations, showing how to serialize dynamic properties into JSON and deserialize them back into a Java object, with complete code examples and step‑by‑step demonstrations.
1. @JsonAnyGetter
Jackson’s @JsonAnyGetter annotation marks a method that returns a Map of dynamic properties to be included in the JSON output during serialization.
1.1 Requirements
Method must be public.
Method must have no parameters.
Return type must be Map<String, Object> or a subclass.
1.2 Usage
Example:
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
class User {
private String name;
private int age;
private Map<String, Object> dynamicProps;
public User(String name, int age) {
this.name = name;
this.age = age;
this.dynamicProps = new HashMap<>();
}
public String getName() { return name; }
public int getAge() { return age; }
@JsonAnyGetter
public Map<String, Object> getDynamicProps() {
return dynamicProps;
}
}
public class Example {
public static void main(String[] args) throws Exception {
User user = new User("John", 30);
user.getDynamicProps().put("email", "[email protected]");
user.getDynamicProps().put("phone", "+1 123-456-7890");
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(user);
System.out.println(json);
}
}The User class contains two regular fields and a Map called dynamicProps. Adding entries for “email” and “phone” to the map and annotating the getter with @JsonAnyGetter causes Jackson to output those entries as top‑level JSON fields.
During serialization Jackson invokes the annotated method and merges the returned key‑value pairs into the generated JSON.
2. @JsonAnySetter
@JsonAnySetter tells Jackson to call the annotated method for each JSON property that does not match a regular field, allowing dynamic properties to be stored in the object.
2.1 Requirements
Method must be public.
Method must accept a String key and an Object value as parameters.
Method must have a void return type.
2.2 Usage
Example:
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
import java.util.Map;
class User {
private String name;
private int age;
private Map<String, Object> dynamicProps;
public User() {
this.dynamicProps = new HashMap<>();
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
@JsonAnySetter
public void setDynamicProp(String key, Object value) {
dynamicProps.put(key, value);
}
public Map<String, Object> getDynamicProps() {
return dynamicProps;
}
}
public class Example {
public static void main(String[] args) throws Exception {
String json = "{\"name\":\"John\",\"age\":30,\"email\":\"[email protected]\",\"phone\":\"+1 123-456-7890\"}";
ObjectMapper objectMapper = new ObjectMapper();
User user = objectMapper.readValue(json, User.class);
System.out.println("Name: " + user.getName());
System.out.println("Age: " + user.getAge());
System.out.println("Dynamic Properties: " + user.getDynamicProps());
}
}The User class again has a Map for dynamicProps. Annotating setDynamicProp with @JsonAnySetter makes Jackson call this method for each unknown JSON field, storing them in the map. This enables the object to receive and handle dynamic properties without predefined fields.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer1970
Formerly called 'Code to 35'. Add our main WeChat ID to access a wealth of shared resources (algorithms, interview prep, tech stacks: Java, Python, Go, big data). We mainly share serious development techniques, focusing on output-driven input. Occasionally we post life snippets and gossip. Our aim is to attract precise traffic and test advertising opportunities.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
