Close

Jackson JSON - Using @JsonIdentityReference to always serialize a POJO by id

[Last Updated: Aug 11, 2020]

@JsonIdentityReference annotation can be used along with @JsonIdentityInfo to serialize Object by its id instead of as full POJO.

@JsonIdentityInfo allows to serialize a POJO by id but only when it is encountered second time during serialization. @JsonIdentityReference serialize the POJO by id the first time it is encountered.

@JsonIdentityReference annotation:

package com.fasterxml.jackson.annotation;
 ....

public @interface JsonIdentityReference{
    //if true then POJO is always serialized by ids 
    public boolean alwaysAsId() default false;
}

Example

With circular reference

Following example shows the use of @JsonIdentityReference and @JsonIdentityInfo with circular reference:

package com.logicbig.example;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,
      property = "id")
@JsonIdentityReference(alwaysAsId = true)
public class Customer {
  private int id;
  private String name;
  private Order order;
    .............
}
package com.logicbig.example;

import java.util.List;

public class Order {
  private int orderId;
  private List<Integer> itemIds;
  private Customer customer;
    .............
}

The main class:

public class ExampleMain {
  public static void main(String[] args) throws IOException {
      Order order = new Order();
      order.setOrderId(1);
      order.setItemIds(List.of(10, 30));

      Customer customer = new Customer();
      customer.setId(2);
      customer.setName("Peter");
      customer.setOrder(order);
      order.setCustomer(customer);
      System.out.println(customer);

      System.out.println("-- serializing Customer --");
      ObjectMapper om = new ObjectMapper();
      String s = om.writeValueAsString(customer);
      System.out.println(s);
  }
}
Customer{id=2, name='Peter', order=Order{id=1, itemIds=[10, 30]}}
-- serializing Customer --
2

Also check out @JsonIdentityInfo example without @JsonIdentityReference.

Without circular reference

package com.logicbig.example;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,
      property = "id")
@JsonIdentityReference(alwaysAsId = true)
public class Person {
  private int id;
  private String name;
    .............
}

The main class:

public class ExampleMain2 {
  public static void main(String[] args) throws IOException {
     Person person = new Person();
     person.setName("Joe");
     person.setId(1);
      System.out.println(person);

      System.out.println("-- serializing Person --");
      ObjectMapper om = new ObjectMapper();
      String s = om.writeValueAsString(person);
      System.out.println(s);
  }
}
Person{id=1, name='Joe'}
-- serializing Person --
1

Using @JsonIdentityReference on fields

@JsonIdentityReference can be used on type, annotation type, field, method and parameter. Following example shows how to use it on a field:

public class Employee {
  private String name;
  private String dept;
  @JsonIdentityReference(alwaysAsId = true)
  private Address address;
    .............
}
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,
      property = "id")
public class Address {
  private int id;
  private String street;
  private String city;
    .............
}

The main class:

public class ExampleMain3 {
  public static void main(String[] args) throws IOException {
      Employee  employee = Employee.of("Sara", "IT",
              Address.of(1,"111 Heaven Drive", "Sun Valley"));
      System.out.println(employee);

      System.out.println("-- serializing Employee --");
      ObjectMapper om = new ObjectMapper();
      String s = om.writeValueAsString(employee);
      System.out.println(s);
  }
}
Employee{name='Sara', dept='IT', address=Address{street='111 Heaven Drive', city='Sun Valley'}}
-- serializing Employee --
{"name":"Sara","dept":"IT","address":1}

Example Project

Dependencies and Technologies Used:

  • jackson-databind 2.9.6: General data-binding functionality for Jackson: works on core streaming API.
  • JDK 10
  • Maven 3.5.4

@JsonIdentityReference Example Select All Download
  • jackson-json-identity-ref-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • Customer.java

    See Also