23

I would like to define my custom serialization strategy (which fields to include), while using Jackson. I know, that I can do it with views/filters, but it introduces very bad thing - using string-representation of field names, which automatically enables problems with auto-refactoring.

How do I force Jackson into serializing only annotated properties and nothing more?

Denis Kulagin
  • 8,472
  • 17
  • 60
  • 129
  • What you can easily do is **not** serialize annotated properties by using `@JsonIgnore`. – Mena Sep 17 '14 at 15:00
  • @Mena Not good at all - it forces me into annotating base classes as well. I don't want to do that. – Denis Kulagin Sep 17 '14 at 15:04
  • "It forces me to annotate base classes as well" --> I can't see how it would to that. You annotate your properties to ignore and you're done, your object serializes with all props except for those ignored. – Mena Sep 17 '14 at 15:05
  • Possible duplicate of [Jackson: Serialize only marked fields](http://stackoverflow.com/questions/22668374/jackson-serialize-only-marked-fields) – Chris Peacock Jan 09 '17 at 12:39

2 Answers2

30

If you disable all auto-detection it should only serialize the properties that you have annotated--whether it be the properties themselves or the getters. Here's a simple example:

private ObjectMapper om;

@Before
public void setUp() throws Exception {
    om = new ObjectMapper();
    // disable auto detection
    om.disable(MapperFeature.AUTO_DETECT_CREATORS,
            MapperFeature.AUTO_DETECT_FIELDS,
            MapperFeature.AUTO_DETECT_GETTERS,
            MapperFeature.AUTO_DETECT_IS_GETTERS);
    // if you want to prevent an exception when classes have no annotated properties
    om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
}

@Test
public void test() throws Exception {
    BlahClass blahClass = new BlahClass(5, "email", true);
    String s = om.writeValueAsString(blahClass);
    System.out.println(s);
}

public static class BlahClass {
    @JsonProperty("id")
    public Integer id;
    @JsonProperty("email")
    public String email;
    public boolean isThing;

    public BlahClass(Integer id, String email, boolean thing) {
        this.id = id;
        this.email = email;
        isThing = thing;
    }
}
Sam Berry
  • 7,394
  • 6
  • 40
  • 58
  • 1
    Works as a charm. You are thE bEst! – Denis Kulagin Sep 18 '14 at 06:52
  • Only to mention: it requires **com.fasterxml.jackson.core.jackson-databind** maven artifact-o. – Denis Kulagin Sep 18 '14 at 06:54
  • Thanks for providing this response. Jackson is serializing exactly how I want without having to go through a Mixin or CustomSerializer. – TemarV Sep 29 '15 at 20:14
  • 2
    As of 2.13 ObjectMapper::disable has been deprecated. New approach is to use JsonMapper.Builder::.featuresToDisable. I used the provided Jackson2ObjectMapperBuilder as a convenience. – MrAaronOlsen Dec 31 '21 at 13:16
21

In case you want to do this without configuring the mapper just for a specific type:

@JsonAutoDetect(
    fieldVisibility = Visibility.NONE,
    setterVisibility = Visibility.NONE,
    getterVisibility = Visibility.NONE,
    isGetterVisibility = Visibility.NONE,
    creatorVisibility = Visibility.NONE
)
public class BlahClass {
    @JsonProperty("id")
    private Integer id;
    @JsonProperty("email")
    private String email;
}
fischermatte
  • 3,327
  • 4
  • 42
  • 52