Bartosz Bilicki
Bartosz Bilicki

Reputation: 13275

Is it possible to ignore @JsonSerialize annotation?

Is it possible to serialize object using Jackson,but ignoring custom serializers registered with annotation @JsonSerialize(using = MyCustomSerializer.class) ?

Rationale: I want to use Jackson to convert my object to Map, using com.fasterxml.jackson.databind.ObjectMapper.convertValue(object,Map.class). Currently it does not work, because my class has custom serializer (@JsonSerialize) but misses deserializer. I require custom serializer, and I really do not need and do not want to write deserializer.

ObjectMapper.convertValue uses my serialization then fails at deserialization.

I would like to have ObjectMapper that will just ignore @JsonSerialize and use default serialization logic. Is that possible with Jackson?

Upvotes: 5

Views: 4432

Answers (2)

Elad Hirsch
Elad Hirsch

Reputation: 294

you could also disable a spesific annoation by using ignoreJsonTypeInfoIntrospector and return null for the spsific annoation class;

take a look at the last github issue

public class IgnoreJsonUnwrappedAnnotationInspector extends JacksonAnnotationIntrospector {
  @Override
  public NameTransformer findUnwrappingNameTransformer(AnnotatedMember member) {
    return null;
  }
}

also take a look here for more detailed example

another solution is to create more ObjectMapper instances for the different scenarios.

Upvotes: 2

ingenious
ingenious

Reputation: 966

That is entirely possible. You could disable annotations on a per ObjectMapper basis, like this:

ObjectMapper mapper = new ObjectMapper();
mapper.disable(MapperFeature.USE_ANNOTATIONS);

For more details check the documentation over at github or check the examples at baeldung.

Upvotes: 4

Related Questions