Search code examples
javajacksonmixinsobjectmapper

Jackson mixin: How to Ignore or Rename fields from class as tree


I'm trying to do deep filtering (rename / ignore fields) from class that is represented as a tree.

With Jackson Mixin I can rename or ignore fields from root level. What I want to achieve, is how to do filtering (rename / ignore) on multy levels?

For instance, I have a tree that has two classes. Class A is the root, B is second level as depth in the tree. By applying Jackson Mxiin, I want to filter property a2 from the root A, and property b1 from B.

Class A that represents root class

public class A {
  private String a1;
  private String a2;
  private B b;

  public A(String a1, String a2, B b) {
      this.a1 = a1;
      this.a2 = a2;
      this.b = b;
  }

  public String getA1() {
      return a1;
  }

  public void setA1(String a1) {
      this.a1 = a1;
  }

  public String getA2() {
      return a2;
  }

  public void setA2(String a2) {
      this.a2 = a2;
  }

  public B getB() {
      return b;
  }

  public void setB(B b) {
      this.b = b;
  }

}

Class B - Second level depth

public class B {

      private String b1;
      private String b2;

      public B(String b2, String b1) {
          this.b1 = b1;
          this.b2 = b2;
      }

      public String getB1() {
          return b1;
      }

      public void setB1(String b1) {
          this.b1 = b1;
      }

      public String getB2() {
          return b2;
      }

      public void setB2(String b2) {
          this.b2 = b2;
      }
    }

Filters

public interface AMixIn {
  // Filter for A (implemented to filter second depth as well)
  @JsonIgnore
  String getA2();

  @JsonIgnore
  public BMixIn getB();
}

public interface BMixIn {
  // Filter for B
  @JsonIgnore
  public String getB1();

}

Test

public class SecondLevelTest {
  // Test
  private ObjectMapper mapper = null;
  private ObjectWriter writer = null;

  @Before
  public void setUp() {
      // init
      mapper = new ObjectMapper();
      mapper.setMixIns(ImmutableMap.<Class<?>, Class<?>>of(A.class, AMixIn.class));
      writer = mapper.writer().with(SerializationFeature.INDENT_OUTPUT);
  }

  @Test
  public void rename_field_jackson() throws JsonProcessingException {

      B b = new B("vb1", "vb2");
      A a = new A("va1", "va2", b);

      // I want to get this result
      // {
      // "a1" : "va1",
      // "b2" : "vb2"
      // }
      String json = writer.writeValueAsString(a);
      System.out.println(json);
  }
}

Solution

  • This should give you the output you're looking for. Change your setUp() to also use BMixIn:

    @Before
    public void setUp() {
        // init
        mapper = new ObjectMapper();
        mapper.setMixIns(ImmutableMap.of(A.class, AMixIn.class, B.class, BMixIn.class));
        writer = mapper.writer().with(SerializationFeature.INDENT_OUTPUT);
    }
    

    And change AMixIn to unwrap B

    public interface AMixIn {
        // Filter for A (implemented to filter second depth as well)
        @JsonIgnore
        String getA2();
    
        @JsonUnwrapped
        public BMixIn getB();
    }
    

    Forgetting to register BMixIn caused its @JsonIgnore to never be used. @JsonUnwrapped un-nests b so you get a flat structure.