Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable Jackson mapper for a particular annotation #133

Closed
yamsellem opened this issue Dec 28, 2012 · 6 comments
Closed

Disable Jackson mapper for a particular annotation #133

yamsellem opened this issue Dec 28, 2012 · 6 comments
Milestone

Comments

@yamsellem
Copy link

@yamsellem yamsellem commented Dec 28, 2012

With Jackson, it's easy to disable all annotations for a given ObjectMapper. Is there a way to only disable one given annotation?

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

// disable one?
ObjectMapper mapper = new ObjectMapper()
mapper.disable(@JsonIgnore);

Using @JacksonAnnotationsInside, I've defined a custom Jackson annotation and I only want it to be used in certain circumstances.

@cowtowncoder

This comment has been minimized.

Copy link
Member

@cowtowncoder cowtowncoder commented Dec 29, 2012

Mapper does not have any functionality for disabling processing of specific annotations. The best place to do this would be in JacksonAnnotationIntrospector, so this could be considered a feature request.
In the meantime, you can sub-class it and override methods that handle specific annotation.

@yamsellem

This comment has been minimized.

Copy link
Author

@yamsellem yamsellem commented Dec 29, 2012

I was looking for a way to disable a particular annotation, if it works with JacksonAnnotationIntrospector, so be it. Can you provide a sample code? Like:

ObjectMapper mapper = new ObjectMapper();
JacksonAnnotationIntrospector introspector = new JacksonAnnotationIntrospector();
introspector.disable(@JsonIgnore);
mapper.setAnnotationIntrospector(introspector);

The isHandled method seemed to be what I expected, but it is deprecated and do not say how to replace it.
Thanks a lot.

@yamsellem

This comment has been minimized.

Copy link
Author

@yamsellem yamsellem commented Jan 5, 2013

Any idea?

@cowtowncoder

This comment has been minimized.

Copy link
Member

@cowtowncoder cowtowncoder commented Jan 5, 2013

The purpose of isHandled was more to optimize handling (to avoid including annotations Jackson doesn't care about); and since this caused issues with custom annotations, it was dropped from 2.0. But it would indeed have worked for this use case if it was still supported.

In your case, you will need to figure out which methods of JacksonAnnotationintrospector handle annotation(s) you want to mask. There is no in-built functionality to disable handling of particular annotation.

But for Jackson 2.2, perhaps it would make sense to refactor JacksonAnnotationIntrospector to more easily allow disabling of handling; for example using internal method like 'findAnnotation()', default implementation of which simple does 'class.getAnnotation()', but overridden method could add a check to return null for particular annotation types.

@cowtowncoder cowtowncoder added this to the 2.5.0 milestone Sep 26, 2014
cowtowncoder added a commit that referenced this issue Sep 26, 2014
@cowtowncoder

This comment has been minimized.

Copy link
Member

@cowtowncoder cowtowncoder commented Sep 26, 2014

AnnotationIntrospector now has methods:

A _findAnnotation(Annotated annotated,  Class<A> annoClass);
boolean _hasAnnotation(Annotated annotated, Class<? extends Annotation> annoClass)

which may be overridden by custom implementations (subtypes of JacksonAnnotationIntrospector.
This may or may not be useful in changing the way things work. It should definitely allow blocking of specific annotations, regardless of what they are used for.

christophercurrie pushed a commit to christophercurrie/jackson-databind that referenced this issue Jul 19, 2015
@mjball

This comment has been minimized.

Copy link

@mjball mjball commented Jul 27, 2015

If you're not using Jackson 2.6 yet, this annotation introspector seems to work well for disabling a specific annotation (@JsonUnwrapped in this case; other annotations not tested):

public class IgnoreJsonUnwrappedAnnotationInspector extends JacksonAnnotationIntrospector {
  @Override
  public NameTransformer findUnwrappingNameTransformer(AnnotatedMember member) {
    return null;
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.