Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Jackson mapper for a particular annotation

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.

like image 583
yves amsellem Avatar asked Dec 27 '12 15:12

yves amsellem


People also ask

How do you ignore certain fields based on a serializing object to JSON?

If there are fields in Java objects that do not wish to be serialized, we can use the @JsonIgnore annotation in the Jackson library. The @JsonIgnore can be used at the field level, for ignoring fields during the serialization and deserialization.

How do I ignore JsonProperty?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.

How do I ignore properties in ObjectMapper?

ObjectMapper; ObjectMapper objectMapper = new ObjectMapper(); objectMapper. configure(DeserializationFeature. FAIL_ON_UNKNOWN_PROPERTIES, false); This will now ignore unknown properties for any JSON it's going to parse, You should only use this option if you can't annotate a class with @JsonIgnoreProperties annotation.

How do I turn off annotations in spring boot?

If you find that specific auto-configure classes are being applied that you don't want, you can use the exclude attribute of @EnableAutoConfiguration to disable them. If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead.

How to disable the Jackson annotation After completing its work?

Usually we need to disable the Jackson annotation after completing its work. So, we use the disable () method of ObjectMapper class for disabling the annotation. We pass the USE_ANNOTATIONS mapper feature to the disable () method of the ObjectMapper class.

How to ignore annotations like @jsonproperty in objectmapper?

You can configure the ObjectMapper to ignore annotations like @JsonProperty by doing: ObjectMapper objectMapper = new ObjectMapper ().configure (org.codehaus.jackson.map.DeserializationConfig.Feature.USE_ANNOTATIONS, false).configure (org.codehaus.jackson.map.SerializationConfig.Feature.USE_ANNOTATIONS, false)

What is objectmapper in Jackson?

We've covered the ObjectMapper class - the central API of Jackson for serialization and deserialization of Java Objects and JSON data. We've first taken a look at how to install Jackson, and then dived into converting JSON to Java Objects - from strings, files, HTTP Responses, InputStreams and byte arrays.

What is custom jackson2objectmapperbuildercustomizer?

Jackson2ObjectMapperBuilderCustomizer The purpose of this functional interface is to allow us to create configuration beans. They will be applied to the default ObjectMapper created via Jackson2ObjectMapperBuilder: The configuration beans are applied in a specific order, which we can control using the @Order annotation.


1 Answers

This the best I've come across. I think I saw it on the Jackson user group forums somewhere.

Essentially it makes a custom annotation introspector, which returns null if it sees that it has a specific annotation (in this case JsonTypeInfo)

JacksonAnnotationIntrospector ignoreJsonTypeInfoIntrospector = new JacksonAnnotationIntrospector() {
            @Override
            protected TypeResolverBuilder<?> _findTypeResolver(
                    MapperConfig<?> config, Annotated ann, JavaType baseType) {
                if (!ann.hasAnnotation(JsonTypeInfo.class)) {
                    return super._findTypeResolver(config, ann, baseType);
                }
                return null;
            }
        };

        mapper.setAnnotationIntrospector(ignoreJsonTypeInfoIntrospector);
like image 84
755 Avatar answered Sep 30 '22 18:09

755