Jackson Ignore Properties on Marshalling

1. Overview

This tutorial will show how to ignore certain fields when serializing an object to json using Jackson 2.x.

This is very useful when the Jackson defaults aren’t enough and we need to control exactly what gets serialized to json – and there are several ways to ignore properties.

If you want to dig deeper and learn other cool things you can do with the Jackson 2 – head on over to the main Jackson tutorial.

2. Ignore fields at the class level

We can ignore specific fields at the class level, using the @JsonIgnoreProperties annotation and specifying the fields by name:

@JsonIgnoreProperties(value = { "intValue" })
public class MyDto {

    private String stringValue;
    private int intValue;
    private boolean booleanValue;

    public MyDto() {
        super();
    }

    // standard setters and getters are not shown
}

We can now test that, after the object is written to json, the field is indeed not part of the output:

@Test
public void givenFieldIsIgnoredByName_whenDtoIsSerialized_thenCorrect()
  throws JsonParseException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    MyDto dtoObject = new MyDto();

    String dtoAsString = mapper.writeValueAsString(dtoObject);

    assertThat(dtoAsString, not(containsString("intValue")));
}

3. Ignore field at the field level

We can also ignore a field directly via the @JsonIgnore annotation directly on the field:

public class MyDto {

    private String stringValue;
    @JsonIgnore
    private int intValue;
    private boolean booleanValue;

    public MyDto() {
        super();
    }

    // standard setters and getters are not shown
}

We can now test that the intValue field is indeed not part of the serialized json output:

@Test
public void givenFieldIsIgnoredDirectly_whenDtoIsSerialized_thenCorrect() 
  throws JsonParseException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    MyDto dtoObject = new MyDto();

    String dtoAsString = mapper.writeValueAsString(dtoObject);

    assertThat(dtoAsString, not(containsString("intValue")));
}

4. Ignore all fields by type

Finally, we can ignore all fields of a specified type, using the @JsonIgnoreType anotation. If we control the type, then we can annotate the class directly:

@JsonIgnoreType
public class SomeType { ... }

More often than not however, we don’t have control of the class itself; in this case, we can make good use of Jackson mixins.

First, we define a MixIn for the type we’d like to ignore, and annotate that with @JsonIgnoreType instead:

@JsonIgnoreType
public class MyMixInForString {
    //
}

Then we register that mixin to replace (and ignore) all String types during marshalling:

mapper.addMixInAnnotations(String.class, MyMixInForString.class);

At this point, all Strings will be ignored instead of marshalled to json:

@Test
public final void givenFieldTypeIsIgnored_whenDtoIsSerialized_thenCorrect() 
  throws JsonParseException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.addMixInAnnotations(String.class, MyMixInForString.class);
    MyDto dtoObject = new MyDto();
    dtoObject.setBooleanValue(true);

    String dtoAsString = mapper.writeValueAsString(dtoObject);

    assertThat(dtoAsString, containsString("intValue"));
    assertThat(dtoAsString, containsString("booleanValue"));
    assertThat(dtoAsString, not(containsString("stringValue")));
}

5. Ignore fields using Filters

Finally, we can also use Filters to ignore specific fields in Jackson. First, we need to define the filter on the java object:

@JsonFilter("myFilter")
public class MyDtoWithFilter { ... }

Then, we define a simple filter that will ignore the intValue field:

SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("intValue");
FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);

Now we can serialize the object and make sure that the intValue field is not present in the json output:

@Test
public final void givenTypeHasFilterThatIgnoresFieldByName_whenDtoIsSerialized_thenCorrect() 
  throws JsonParseException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    SimpleBeanPropertyFilter theFilter = SimpleBeanPropertyFilter.serializeAllExcept("intValue");
    FilterProvider filters = new SimpleFilterProvider().addFilter("myFilter", theFilter);

    MyDtoWithFilter dtoObject = new MyDtoWithFilter();
    String dtoAsString = mapper.writer(filters).writeValueAsString(dtoObject);

    assertThat(dtoAsString, not(containsString("intValue")));
    assertThat(dtoAsString, containsString("booleanValue"));
    assertThat(dtoAsString, containsString("stringValue"));
    System.out.println(dtoAsString);
}

6. Conclusion

The article illustrated how to ignore fields on serialization – first by name, then directly, and finally – we ignored the entire java type with MixIns and we use filters for more control of the output.

The implementation of all these examples and code snippets can be found in my github project – this is an Eclipse based project, so it should be easy to import and run as it is.

I usually post about Dev stuff on Google+ - you can follow me there:

Get My 3 Spring eBooks
Download the 3 eBooks - Build Your App "The Right Way"
×
Build Web App with Spring (and prototype it to 90% very quickly)