59

I am serializing a POJO into JSON using Jackson 2.1.4 but I want to ignore a particular field from getting serialized. I used transient but still it is serializing that element.

public class TestElement {

    int x;

    private transient String y;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public String getY() {
        return y;
    }

    public void setY(String y) {
        this.y = y;
    }
}

I am serializing as following:

public static void main(String[] args) throws JsonProcessingException {
    TestElement testElement = new TestElement();
    testElement.setX(10);
    testElement.setY("adasd");
    ObjectMapper om = new ObjectMapper();
    String serialized = om.writeValueAsString(testElement);
    System.err.println(serialized);
}

Please don't suggest @JsonIgnore as I don't want to tie my model to jackson specific annotations. Can it be done using transient only? Is there any API on objectmapper for visibility settings?

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
Naman
  • 2,569
  • 4
  • 27
  • 44

4 Answers4

77

The reason Jackson serializes the transient member is because the getters are used to determine what to serialize, not the member itself - and since y has a public getter, that gets serialized. If you want to change that default and have Jackson use fields - simply do:

om.setVisibilityChecker(
  om.getSerializationConfig()
    .getDefaultVisibilityChecker()
    .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
    .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
);

Another way to ignore a property on serialization is to do it directly on the class:

@JsonIgnoreProperties(value = { "y" })
public class TestElement {
...

And another way is directly on the field:

public class TestElement {

    @JsonIgnore
    private String y;
...

Hope this helps.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Eugen
  • 8,523
  • 8
  • 52
  • 74
  • 5
    As I mentioned in the question, I'd prefer a way in which I don't have to tie my model to jackson specific annotations. One thing i can't understand is why jackson is serializing even transient member? Other API's like Smart JSON or even java serializer don't do that. Any idea about this? – Naman Feb 13 '14 at 17:03
  • 3
    I updated my original answer to explain the problem further and provide you with a solution - hope it helps. – Eugen Feb 13 '14 at 21:02
  • Thanks, i was looking for first solution. Now I can do this without having to use all the jackson specific annotations. – Naman Feb 14 '14 at 09:41
  • No worries - there are actually a few additional ways of doing this - you can check out my post on ignoring certain fields here: http://www.baeldung.com/jackson-ignore-properties-on-serialization - glad I could help. Cheers. – Eugen Feb 14 '14 at 11:21
  • 3
    Here's an alternative way (suggested as an edit) to do it: ```om.disable(MapperFeature.AUTO_DETECT_GETTERS) om.disable(MapperFeature.AUTO_DETECT_IS_GETTERS) om.enable(MapperFeature.AUTO_DETECT_FIELDS)``` – Eugen May 13 '15 at 10:08
  • 1
    So if I didn't miss something here, there's no way to get it to simply respect the transient keyword? (Which you'd imagine it would do automatically.) Yeah that sounds incredibly dumb. – Manius Jan 29 '20 at 19:30
  • Wouldn't this break compatibility with existing code using the getters and setters? – Eaton Emmerich Aug 22 '22 at 09:00
47

A new way to stop Jackson from serializing and deserializing is to call mapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true).

manuel
  • 627
  • 7
  • 7
  • This is confirmed to work with Kotlin's @Transient annotation on Kotlin version 1.2.71. – bartonstanley Oct 24 '18 at 19:56
  • This method is deprecated now - use: `JsonMapper.builder().configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true)` – bbnt May 13 '23 at 15:19
9

You can configure it with springs properties

spring.jackson.mapper.propagate-transient-marker=true
Carlos Cuesta
  • 1,262
  • 1
  • 17
  • 20
4

I can't make comments so complete the previous response here, changing the (now) deprecated method setVisibilityChecker and adding a missing clause for booleans:

mapper.setVisibility(
    mapper.getSerializationConfig().
    getDefaultVisibilityChecker().
    withFieldVisibility(JsonAutoDetect.Visibility.ANY).
    withGetterVisibility(JsonAutoDetect.Visibility.NONE).
    withIsGetterVisibility(JsonAutoDetect.Visibility.NONE)
);
António Ribeiro
  • 4,129
  • 5
  • 32
  • 49
idelvall
  • 1,536
  • 15
  • 25