Search in sources :

Example 1 with AnnotationIntrospector

use of com.fasterxml.jackson.databind.AnnotationIntrospector in project hadoop by apache.

the class YarnJacksonJaxbJsonProvider method configObjectMapper.

public static void configObjectMapper(ObjectMapper mapper) {
    AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
    mapper.setAnnotationIntrospector(introspector);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}
Also used : AnnotationIntrospector(com.fasterxml.jackson.databind.AnnotationIntrospector) JaxbAnnotationIntrospector(com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector) JaxbAnnotationIntrospector(com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector)

Example 2 with AnnotationIntrospector

use of com.fasterxml.jackson.databind.AnnotationIntrospector in project jersey by jersey.

the class FilteringJacksonJaxbJsonProvider method _configForWriting.

@Override
protected JsonEndpointConfig _configForWriting(final ObjectMapper mapper, final Annotation[] annotations, final Class<?> defaultView) {
    final AnnotationIntrospector customIntrospector = mapper.getSerializationConfig().getAnnotationIntrospector();
    // Set the custom (user) introspector to be the primary one.
    final ObjectMapper filteringMapper = mapper.setAnnotationIntrospector(AnnotationIntrospector.pair(customIntrospector, new JacksonAnnotationIntrospector() {

        @Override
        public Object findFilterId(final Annotated a) {
            final Object filterId = super.findFilterId(a);
            if (filterId != null) {
                return filterId;
            }
            if (a instanceof AnnotatedMethod) {
                final Method method = ((AnnotatedMethod) a).getAnnotated();
                // Interested only in getters - trying to obtain "field" name from them.
                if (ReflectionHelper.isGetter(method)) {
                    return ReflectionHelper.getPropertyName(method);
                }
            }
            if (a instanceof AnnotatedField || a instanceof AnnotatedClass) {
                return a.getName();
            }
            return null;
        }
    }));
    return super._configForWriting(filteringMapper, annotations, defaultView);
}
Also used : JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) Annotated(com.fasterxml.jackson.databind.introspect.Annotated) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) AnnotationIntrospector(com.fasterxml.jackson.databind.AnnotationIntrospector) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) Method(java.lang.reflect.Method) AnnotatedField(com.fasterxml.jackson.databind.introspect.AnnotatedField) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with AnnotationIntrospector

use of com.fasterxml.jackson.databind.AnnotationIntrospector in project jersey by jersey.

the class MyObjectMapperProvider method createJaxbJacksonAnnotationIntrospector.

private static AnnotationIntrospector createJaxbJacksonAnnotationIntrospector() {
    final AnnotationIntrospector jaxbIntrospector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance());
    final AnnotationIntrospector jacksonIntrospector = new JacksonAnnotationIntrospector();
    return AnnotationIntrospector.pair(jacksonIntrospector, jaxbIntrospector);
}
Also used : JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) AnnotationIntrospector(com.fasterxml.jackson.databind.AnnotationIntrospector) JaxbAnnotationIntrospector(com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector) JaxbAnnotationIntrospector(com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector)

Example 4 with AnnotationIntrospector

use of com.fasterxml.jackson.databind.AnnotationIntrospector in project jackson-databind by FasterXML.

the class StdSubtypeResolver method collectAndResolveSubtypesByTypeId.

/*
    /**********************************************************
    /* Resolution by class (deserialization)
    /**********************************************************
     */
@Override
public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> config, AnnotatedMember property, JavaType baseType) {
    final AnnotationIntrospector ai = config.getAnnotationIntrospector();
    Class<?> rawBase = (baseType == null) ? property.getRawType() : baseType.getRawClass();
    // Need to keep track of classes that have been handled already 
    Set<Class<?>> typesHandled = new HashSet<Class<?>>();
    Map<String, NamedType> byName = new LinkedHashMap<String, NamedType>();
    // start with lowest-precedence, which is from type hierarchy
    NamedType rootType = new NamedType(rawBase, null);
    AnnotatedClass ac = AnnotatedClass.constructWithoutSuperTypes(rawBase, config);
    _collectAndResolveByTypeId(ac, rootType, config, typesHandled, byName);
    // then with definitions from property
    Collection<NamedType> st = ai.findSubtypes(property);
    if (st != null) {
        for (NamedType nt : st) {
            ac = AnnotatedClass.constructWithoutSuperTypes(nt.getType(), config);
            _collectAndResolveByTypeId(ac, nt, config, typesHandled, byName);
        }
    }
    // and finally explicit type registrations (highest precedence)
    if (_registeredSubtypes != null) {
        for (NamedType subtype : _registeredSubtypes) {
            // is it a subtype of root type?
            if (rawBase.isAssignableFrom(subtype.getType())) {
                // yes
                AnnotatedClass curr = AnnotatedClass.constructWithoutSuperTypes(subtype.getType(), config);
                _collectAndResolveByTypeId(curr, subtype, config, typesHandled, byName);
            }
        }
    }
    return _combineNamedAndUnnamed(typesHandled, byName);
}
Also used : NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) AnnotationIntrospector(com.fasterxml.jackson.databind.AnnotationIntrospector)

Example 5 with AnnotationIntrospector

use of com.fasterxml.jackson.databind.AnnotationIntrospector in project jackson-databind by FasterXML.

the class StdSubtypeResolver method _collectAndResolveByTypeId.

/**
     * Method called to find subtypes for a specific type (class), using
     * type id as the unique key (in case of conflicts).
     */
protected void _collectAndResolveByTypeId(AnnotatedClass annotatedType, NamedType namedType, MapperConfig<?> config, Set<Class<?>> typesHandled, Map<String, NamedType> byName) {
    final AnnotationIntrospector ai = config.getAnnotationIntrospector();
    if (!namedType.hasName()) {
        String name = ai.findTypeName(annotatedType);
        if (name != null) {
            namedType = new NamedType(namedType.getType(), name);
        }
    }
    if (namedType.hasName()) {
        byName.put(namedType.getName(), namedType);
    }
    // only check subtypes if this type hadn't yet been handled
    if (typesHandled.add(namedType.getType())) {
        Collection<NamedType> st = ai.findSubtypes(annotatedType);
        if (st != null && !st.isEmpty()) {
            for (NamedType subtype : st) {
                AnnotatedClass subtypeClass = AnnotatedClass.constructWithoutSuperTypes(subtype.getType(), config);
                _collectAndResolveByTypeId(subtypeClass, subtype, config, typesHandled, byName);
            }
        }
    }
}
Also used : NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) AnnotationIntrospector(com.fasterxml.jackson.databind.AnnotationIntrospector)

Aggregations

AnnotationIntrospector (com.fasterxml.jackson.databind.AnnotationIntrospector)14 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)4 JacksonAnnotationIntrospector (com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector)3 JaxbAnnotationIntrospector (com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 JsonFormat (com.fasterxml.jackson.annotation.JsonFormat)1 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)1 PropertyName (com.fasterxml.jackson.databind.PropertyName)1 JsonPOJOBuilder (com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder)1 Annotated (com.fasterxml.jackson.databind.introspect.Annotated)1 AnnotatedClass (com.fasterxml.jackson.databind.introspect.AnnotatedClass)1 AnnotatedField (com.fasterxml.jackson.databind.introspect.AnnotatedField)1 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)1 AnnotationIntrospectorPair (com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair)1 Method (java.lang.reflect.Method)1