Example usage for com.fasterxml.jackson.databind BeanDescription getClassAnnotations

List of usage examples for com.fasterxml.jackson.databind BeanDescription getClassAnnotations

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind BeanDescription getClassAnnotations.

Prototype

public abstract Annotations getClassAnnotations();

Source Link

Usage

From source file:dk.nykredit.jackson.dataformat.hal.deser.HALBeanDeserializerModifier.java

@Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc,
        JsonDeserializer<?> deserializer) {
    Resource ann = beanDesc.getClassAnnotations().get(Resource.class);
    if (ann != null) {
        return new HALBeanDeserializer((BeanDeserializer) deserializer);
    }// w  w w  .  j  a  v  a2s  . c  o m
    return deserializer;
}

From source file:dk.nykredit.jackson.dataformat.hal.deser.HALBeanDeserializerModifier.java

@Override
public List<BeanPropertyDefinition> updateProperties(DeserializationConfig config, BeanDescription beanDesc,
        List<BeanPropertyDefinition> propDefs) {
    Resource ann = beanDesc.getClassAnnotations().get(Resource.class);
    if (ann != null) {
        List<BeanPropertyDefinition> modified = new ArrayList<>();
        Iterator<BeanPropertyDefinition> defIt = propDefs.iterator();
        while (defIt.hasNext()) {
            BeanPropertyDefinition pbd = defIt.next();
            for (ReservedProperty rp : ReservedProperty.values()) {
                String alternateName = rp.alternateName(pbd, pbd.getName());
                if (!pbd.getName().equals(alternateName)) {
                    modified.add(pbd.withName(new PropertyName(alternateName)));
                    defIt.remove();//from   w ww  . j  a va2 s  . c o  m
                }
            }
        }
        propDefs.addAll(modified);
    }
    return propDefs;
}

From source file:com.github.jonpeterson.jackson.module.versioning.VersioningBeanSerializationModifier.java

@Override
public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDescription,
        JsonSerializer<?> serializer) {
    if (serializer instanceof StdSerializer) {
        JsonVersionedModel jsonVersionedModel = beanDescription.getClassAnnotations()
                .get(JsonVersionedModel.class);
        if (jsonVersionedModel != null)
            return createVersioningSerializer((StdSerializer) serializer, jsonVersionedModel,
                    VersionedModelUtils.getSerializeToVersionProperty(beanDescription));
    }/*  w  ww .j av a  2s .c o m*/

    return serializer;
}

From source file:com.github.jonpeterson.jackson.module.versioning.VersioningBeanDeserializationModifier.java

@Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDescription,
        JsonDeserializer<?> deserializer) {
    if (deserializer instanceof StdDeserializer) {
        JsonVersionedModel jsonVersionedModel = beanDescription.getClassAnnotations()
                .get(JsonVersionedModel.class);
        if (jsonVersionedModel != null)
            return createVersioningDeserializer((StdDeserializer) deserializer, jsonVersionedModel,
                    VersionedModelUtils.getSerializeToVersionProperty(beanDescription));
    }/*from  w  w w.j a  v a 2 s  .  co m*/

    return deserializer;
}

From source file:com.github.shyiko.jackson.module.advice.JsonAdviceModule.java

@Override
public void setupModule(SetupContext context) {
    context.addBeanSerializerModifier(new BeanSerializerModifier() {

        @Override//  w  w  w.  ja v  a  2s. c  o m
        public com.fasterxml.jackson.databind.ser.BeanSerializerBuilder updateBuilder(
                SerializationConfig config, BeanDescription beanDesc,
                com.fasterxml.jackson.databind.ser.BeanSerializerBuilder builder) {
            JsonSerializerAdvice advice = beanDesc.getClassAnnotations().get(JsonSerializerAdvice.class);
            return advice != null ? new AdvisedBeanSerializerBuilder(builder, advice.value()) : builder;
        }
    });
    context.addBeanDeserializerModifier(new BeanDeserializerModifier() {

        @Override
        public BeanDeserializerBuilder updateBuilder(DeserializationConfig config, BeanDescription beanDesc,
                BeanDeserializerBuilder builder) {
            JsonDeserializerAdvice advice = beanDesc.getClassAnnotations().get(JsonDeserializerAdvice.class);
            return advice != null ? new AdvisedBeanDeserializerBuilder(builder, advice.value()) : builder;
        }
    });
}

From source file:com.github.jonpeterson.jackson.module.interceptor.JsonInterceptorModule.java

public JsonInterceptorModule() {
    super("JsonInterceptor");

    setDeserializerModifier(new BeanDeserializerModifier() {
        @Override// w  w  w  .  java2s  .  com
        public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config,
                BeanDescription beanDescription, JsonDeserializer<?> deserializer) {
            if (deserializer instanceof StdDeserializer) {
                JsonInterceptors jsonInterceptors = beanDescription.getClassAnnotations()
                        .get(JsonInterceptors.class);
                if (jsonInterceptors != null) {
                    Class<? extends JsonInterceptor>[] interceptors = jsonInterceptors.beforeDeserialization();
                    if (interceptors.length > 0)
                        return createInterceptingDeserializer((StdDeserializer) deserializer, interceptors);
                }
            }

            return deserializer;
        }

        // here just to make generics work without warnings
        private <T> JsonInterceptingDeserializer<T> createInterceptingDeserializer(
                StdDeserializer<T> deserializer, Class<? extends JsonInterceptor>[] interceptors) {
            return new JsonInterceptingDeserializer<T>(deserializer, interceptors);
        }
    });

    setSerializerModifier(new BeanSerializerModifier() {

        @Override
        public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDescription,
                JsonSerializer<?> serializer) {
            if (serializer instanceof StdSerializer) {
                JsonInterceptors jsonInterceptors = beanDescription.getClassAnnotations()
                        .get(JsonInterceptors.class);
                if (jsonInterceptors != null) {
                    Class<? extends JsonInterceptor>[] interceptors = jsonInterceptors.afterSerialization();
                    if (interceptors.length > 0)
                        return createInterceptingSerializer((StdSerializer) serializer, interceptors);
                }
            }

            return serializer;
        }

        // here just to make generics work without warnings
        private <T> JsonInterceptingSerializer<T> createInterceptingSerializer(StdSerializer<T> serializer,
                Class<? extends JsonInterceptor>[] interceptors) {
            return new JsonInterceptingSerializer<T>(serializer, interceptors);
        }
    });
}

From source file:de.fraunhofer.iosb.ilt.sta.serialize.EntitySerializer.java

protected void serializeFieldTyped(Entity entity, JsonGenerator gen, SerializerProvider serializers,
        BeanDescription beanDescription, BeanPropertyDefinition beanPropertyDefinition,
        TypeSerializer typeSerializer) throws Exception {
    try {//from w  w w.j a va2  s  . c  o  m
        if (typeSerializer == null) {
            typeSerializer = serializers.findTypeSerializer(
                    serializers.constructType(beanPropertyDefinition.getAccessor().getRawType()));
        }
        if (typeSerializer == null) {
            // if not static type if available use dynamic type if available
            Object propertyValue = beanPropertyDefinition.getAccessor().getValue(entity);
            if (propertyValue != null) {
                typeSerializer = serializers
                        .findTypeSerializer(serializers.constructType(propertyValue.getClass()));
            }
        }

        BeanPropertyWriter bpw = new BeanPropertyWriter(beanPropertyDefinition,
                beanPropertyDefinition.getAccessor(), beanDescription.getClassAnnotations(),
                beanPropertyDefinition.getAccessor().getType(), null, // will be searched automatically
                typeSerializer, // will not be searched automatically
                beanPropertyDefinition.getAccessor().getType(),
                suppressNulls(serializers.getConfig().getDefaultPropertyInclusion()),
                suppressableValue(serializers.getConfig().getDefaultPropertyInclusion()));
        bpw.serializeAsField(entity, gen, serializers);
    } catch (JsonMappingException ex) {
        Logger.getLogger(EntitySerializer.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.candlepin.swagger.CandlepinSwaggerModelConverter.java

public Model resolve(Type rawType, ModelConverterContext context, Iterator<ModelConverter> next) {
    if (this.shouldIgnoreClass(rawType)) {
        return null;
    }/*from www  . ja  v  a 2 s .c om*/

    /**
     * See java doc of NestedComplexType. This unwrapping makes sure that a
     * real type of field used throughout the method. At the same time flag
     * 'isNested' helps to indicate later in the method that this type may
     * be introspected as Hateoas enabled field
     */
    boolean isNested = false;
    if (rawType instanceof NestedComplexType) {
        isNested = true;
        NestedComplexType nested = (NestedComplexType) rawType;
        rawType = nested.getOriginalType();
    }
    JavaType type = pMapper.constructType(rawType);

    if (type.isEnumType() || PrimitiveType.fromType(type) != null) {
        // We don't build models for primitive types
        return null;
    }

    final BeanDescription beanDesc = pMapper.getSerializationConfig().introspect(type);
    // Couple of possibilities for defining
    String name = isNested ? "Nested" : "";
    name += pTypeName(type, beanDesc);

    if ("Object".equals(name)) {
        return new ModelImpl();
    }

    final ModelImpl model = new ModelImpl().type(ModelImpl.OBJECT).name(name)
            .description(pDescription(beanDesc.getClassInfo()));

    if (!type.isContainerType()) {
        // define the model here to support self/cyclic referencing of
        // models
        context.defineModel(name, model, type, null);
    }

    if (type.isContainerType()) {
        // We treat collections as primitive types, just need to add models
        // for values (if any)
        context.resolve(type.getContentType());
        return null;
    }
    // if XmlRootElement annotation, construct an Xml object and attach it
    // to the model
    XmlRootElement rootAnnotation = beanDesc.getClassAnnotations().get(XmlRootElement.class);
    if (rootAnnotation != null && !"".equals(rootAnnotation.name())
            && !"##default".equals(rootAnnotation.name())) {
        log.debug(rootAnnotation.toString());
        Xml xml = new Xml().name(rootAnnotation.name());
        if (rootAnnotation.namespace() != null && !"".equals(rootAnnotation.namespace())
                && !"##default".equals(rootAnnotation.namespace())) {
            xml.namespace(rootAnnotation.namespace());
        }
        model.xml(xml);
    }

    // see if @JsonIgnoreProperties exist
    Set<String> propertiesToIgnore = new HashSet<String>();
    JsonIgnoreProperties ignoreProperties = beanDesc.getClassAnnotations().get(JsonIgnoreProperties.class);
    if (ignoreProperties != null) {
        propertiesToIgnore.addAll(Arrays.asList(ignoreProperties.value()));
    }

    final ApiModel apiModel = beanDesc.getClassAnnotations().get(ApiModel.class);
    String disc = (apiModel == null) ? "" : apiModel.discriminator();

    if (apiModel != null && StringUtils.isNotEmpty(apiModel.reference())) {
        model.setReference(apiModel.reference());
    }

    if (disc.isEmpty()) {
        // longer method would involve
        // AnnotationIntrospector.findTypeResolver(...) but:
        JsonTypeInfo typeInfo = beanDesc.getClassAnnotations().get(JsonTypeInfo.class);
        if (typeInfo != null) {
            disc = typeInfo.property();
        }
    }
    if (!disc.isEmpty()) {
        model.setDiscriminator(disc);
    }

    List<Property> props = new ArrayList<Property>();
    for (BeanPropertyDefinition propDef : beanDesc.findProperties()) {
        parseProperty(context, isNested, beanDesc, propertiesToIgnore, props, propDef);
    }

    Collections.sort(props, getPropertyComparator());

    Map<String, Property> modelProps = new LinkedHashMap<String, Property>();
    for (Property prop : props) {
        modelProps.put(prop.getName(), prop);
    }
    model.setProperties(modelProps);

    /**
     * This must be done after model.setProperties so that the model's set
     * of properties is available to filter from any subtypes
     **/
    if (!resolveSubtypes(model, beanDesc, context)) {
        model.setDiscriminator(null);
    }

    return model;
}