Importance of @JsonIdentityInfo annotation using Jackson in Java?

JavaJSONObject Oriented ProgrammingProgramming

The @JsonIdentityInfo annotation is used when an object has a parent-child relationship in the Jackson library. The @JsonIdentityInfo annotation is used to indicate the object identity during the serialization and deserialization process. The ObjectIdGenerators.PropertyGenerator is an abstract place-holder class to denote a case where Object Identifier to use comes from a POJO property.


public @interface JsonIdentityInfo


import java.util.*;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonIdentityInfoTest {
   public static void main(String[] args) throws IOException {
      ObjectMapper mapper = new ObjectMapper();
      User user = new User(115, "Raja", "Ramesh");
      Address address = new Address(125, "Madhapur", "Hyderabad", user);
      String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(address);
// User class
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "userId")
class User {
   private int userId;
   private String firstName;
   private String lastName;
   private List<Address> addresses;
   public User(int userId, String firstName, String lastName) {
      this.userId = userId;
      this.firstName = firstName;
      this.lastName = lastName;
      this.addresses = new ArrayList<Address>();
   public int getUserId() {
      return userId;
   public String getFirstName() {
      return firstName;
   public String getLastName() {
      return lastName;
   public void addAddress(Address address) {
// Address class
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "userId")
class Address {
   private int userId;
   private String city;
   private String street;
   private User user;
   public Address(int userId, String street, String city, User user) {
      this.userId = userId;
      this.street = street; = city;
      this.user = user;
   public int getUserId() {
      return userId;
   public String getStreet() {
      return street;
   public String getCity() {
      return city;
   public User getUser() {
      return user;


 "userId" : 125,
 "city" : "Hyderabad",
 "street" : "Madhapur",
 "user" : {
    "userId" : 115,
    "firstName" : "Raja",
    "lastName" : "Ramesh"
Updated on 09-Jul-2020 05:13:16