Exclude properties from nested instances using Jackson

Jackson provides an annotation @JsonIgnorePropertiesthat can be used to exclude properties from serialization in a class or in a field that belongs to that class. Unfortunately, it does not allow to exclude nested properties (i.e. properties of descendants).

Give the following hierarchy of objects:

static class Innermost {
  public String innerField0 = "c";
  public String innerField1 = "d";
}

static class Nested {
  public Innermost next = new Innermost();
}

static class Top {
  @JsonIgnoreProperties({"next.innerField1"}) // no effect
  public Nested next = new Nested();
}

Is there a mechanism to exclude innerField1types from serialization Topwithout excluding it from type serialization Nestedor Innermost?

Ideally, serialization Innermost, Nestedand Topwill accordingly look like this:

{"innerField0":"c","innerField1":"d"}
{"next":{"innerField0":"c","innerField1":"d"}}
{"next":{"next":{"innerField0":"c"}}}

I am using Jackson version 2.8.1

+4
source share

Source: https://habr.com/ru/post/1669994/


All Articles