Search in sources :

Example 1 with AnnotatedClass

use of com.fasterxml.jackson.databind.introspect.AnnotatedClass 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 2 with AnnotatedClass

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

the class TestJacksonAnnotationIntrospector method testJsonTypeResolver.

public void testJsonTypeResolver() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    JacksonAnnotationIntrospector ai = new JacksonAnnotationIntrospector();
    AnnotatedClass ac = AnnotatedClass.constructWithoutSuperTypes(TypeResolverBean.class, mapper.getSerializationConfig());
    JavaType baseType = TypeFactory.defaultInstance().constructType(TypeResolverBean.class);
    TypeResolverBuilder<?> rb = ai.findTypeResolver(mapper.getDeserializationConfig(), ac, baseType);
    assertNotNull(rb);
    assertSame(DummyBuilder.class, rb.getClass());
}
Also used : JacksonAnnotationIntrospector(com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass)

Example 3 with AnnotatedClass

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

the class DeprecatedConstructType1456Test method testGenericParameterViaClass.

// and this is how new code should resolve types if at all possible
public void testGenericParameterViaClass() throws Exception {
    BeanDescription desc = MAPPER.getDeserializationConfig().introspect(MAPPER.constructType(ImplController.class));
    AnnotatedClass ac = desc.getClassInfo();
    AnnotatedMethod m = ac.findMethod("process", new Class<?>[] { BaseEntity.class });
    assertNotNull(m);
    assertEquals(1, m.getParameterCount());
    AnnotatedParameter param = m.getParameter(0);
    assertEquals(ImplEntity.class, param.getType().getRawClass());
}
Also used : AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) AnnotatedParameter(com.fasterxml.jackson.databind.introspect.AnnotatedParameter)

Example 4 with AnnotatedClass

use of com.fasterxml.jackson.databind.introspect.AnnotatedClass in project crnk-framework by crnk-project.

the class JacksonResourceFieldInformationProvider method getName.

protected Optional<String> getName(Field field) {
    ObjectMapper objectMapper = context.getObjectMapper();
    SerializationConfig serializationConfig = objectMapper.getSerializationConfig();
    if (serializationConfig != null && serializationConfig.getPropertyNamingStrategy() != null) {
        AnnotationMap annotationMap = buildAnnotationMap(field.getDeclaredAnnotations());
        AnnotatedClass annotatedClass = AnnotatedClassBuilder.build(field.getDeclaringClass(), serializationConfig);
        AnnotatedField annotatedField = AnnotatedFieldBuilder.build(annotatedClass, field, annotationMap);
        return Optional.of(serializationConfig.getPropertyNamingStrategy().nameForField(serializationConfig, annotatedField, field.getName()));
    }
    return Optional.empty();
}
Also used : AnnotationMap(com.fasterxml.jackson.databind.introspect.AnnotationMap) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) AnnotatedField(com.fasterxml.jackson.databind.introspect.AnnotatedField) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 5 with AnnotatedClass

use of com.fasterxml.jackson.databind.introspect.AnnotatedClass in project endpoints-java by cloudendpoints.

the class ApiAnnotationIntrospector method findSerializerInstance.

@Nullable
private Transformer<?, ?> findSerializerInstance(Annotated a) {
    if (a instanceof AnnotatedClass) {
        AnnotatedClass clazz = (AnnotatedClass) a;
        List<Class<? extends Transformer<?, ?>>> serializerClasses = Serializers.getSerializerClasses(clazz.getRawType(), config);
        if (!serializerClasses.isEmpty()) {
            return Serializers.instantiate(serializerClasses.get(0), TypeToken.of(a.getRawType()));
        }
    }
    return null;
}
Also used : ResourceTransformer(com.google.api.server.spi.config.ResourceTransformer) Transformer(com.google.api.server.spi.config.Transformer) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) AnnotatedClass(com.fasterxml.jackson.databind.introspect.AnnotatedClass) Nullable(javax.annotation.Nullable)

Aggregations

AnnotatedClass (com.fasterxml.jackson.databind.introspect.AnnotatedClass)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 AnnotatedField (com.fasterxml.jackson.databind.introspect.AnnotatedField)3 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)3 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)2 MapperConfig (com.fasterxml.jackson.databind.cfg.MapperConfig)2 AnnotationMap (com.fasterxml.jackson.databind.introspect.AnnotationMap)2 JacksonAnnotationIntrospector (com.fasterxml.jackson.databind.introspect.JacksonAnnotationIntrospector)2 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)2 Test (org.junit.Test)2 AnnotationIntrospector (com.fasterxml.jackson.databind.AnnotationIntrospector)1 Module (com.fasterxml.jackson.databind.Module)1 Annotated (com.fasterxml.jackson.databind.introspect.Annotated)1 AnnotatedParameter (com.fasterxml.jackson.databind.introspect.AnnotatedParameter)1 SubtypeResolver (com.fasterxml.jackson.databind.jsontype.SubtypeResolver)1 ClassKey (com.fasterxml.jackson.databind.type.ClassKey)1 ResourceTransformer (com.google.api.server.spi.config.ResourceTransformer)1 Transformer (com.google.api.server.spi.config.Transformer)1 ClassPath (com.google.common.reflect.ClassPath)1 IOException (java.io.IOException)1