4

A simple class has "is" methods and "get" methods. I would like Jackson to ignore calling all "is" methods.

I tried by setting ObjectMapper's visibility by setting as

mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.NONE);

But it is still considering the is-getter methods, why?

Jackson serializes getter methods and public variables. Is it possible to indicate Jackson to call only public getter methods but not serialize variables?

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
user3435827
  • 111
  • 1
  • 1
  • 3
  • As to your second question: yes, it is possible to prevent use of `public` variables; using `@JsonAutoDetect` or `mapper.setVisibility()`, similar to how getters/is-getters discovery may be prevented. – StaxMan Mar 21 '14 at 05:14

1 Answers1

2

You should consider @JsonAutoDetect annotation. For example, (using POJO class from this question: Conflicting getter definitions for property in Jackson 2.2.3 solution could look like this:

@JsonAutoDetect(isGetterVisibility = Visibility.NONE)
class GetterMethodsObject {

    private int id = 10;

    public int getId() {
        return id;
    }

    public boolean isId() {
        return true;
    }
}

Example usage:

ObjectMapper mapper = new ObjectMapper();
ObjectWriter objectWriter = mapper.writerWithDefaultPrettyPrinter();
System.out.println(objectWriter.writeValueAsString(new GetterMethodsObject()));

Above program prints:

{
  "id" : 10
}
Community
  • 1
  • 1
Michał Ziober
  • 37,175
  • 18
  • 99
  • 146