Search in sources :

Example 16 with AnnotatedMember

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

the class MapSerializer method createContextual.

/*
    /**********************************************************
    /* Post-processing (contextualization)
    /**********************************************************
     */
@Override
public JsonSerializer<?> createContextual(SerializerProvider provider, BeanProperty property) throws JsonMappingException {
    JsonSerializer<?> ser = null;
    JsonSerializer<?> keySer = null;
    final AnnotationIntrospector intr = provider.getAnnotationIntrospector();
    final AnnotatedMember propertyAcc = (property == null) ? null : property.getMember();
    // First: if we have a property, may have property-annotation overrides
    if (_neitherNull(propertyAcc, intr)) {
        Object serDef = intr.findKeySerializer(propertyAcc);
        if (serDef != null) {
            keySer = provider.serializerInstance(propertyAcc, serDef);
        }
        serDef = intr.findContentSerializer(propertyAcc);
        if (serDef != null) {
            ser = provider.serializerInstance(propertyAcc, serDef);
        }
    }
    if (ser == null) {
        ser = _valueSerializer;
    }
    // [databind#124]: May have a content converter
    ser = findContextualConvertingSerializer(provider, property, ser);
    if (ser == null) {
        // 20-Aug-2013, tatu: Need to avoid trying to access serializer for java.lang.Object tho
        if (_valueTypeIsStatic && !_valueType.isJavaLangObject()) {
            ser = provider.findValueSerializer(_valueType, property);
        }
    }
    if (keySer == null) {
        keySer = _keySerializer;
    }
    if (keySer == null) {
        keySer = provider.findKeySerializer(_keyType, property);
    } else {
        keySer = provider.handleSecondaryContextualization(keySer, property);
    }
    Set<String> ignored = _ignoredEntries;
    boolean sortKeys = false;
    if (_neitherNull(propertyAcc, intr)) {
        JsonIgnoreProperties.Value ignorals = intr.findPropertyIgnorals(propertyAcc);
        if (ignorals != null) {
            Set<String> newIgnored = ignorals.findIgnoredForSerialization();
            if (_nonEmpty(newIgnored)) {
                ignored = (ignored == null) ? new HashSet<String>() : new HashSet<String>(ignored);
                for (String str : newIgnored) {
                    ignored.add(str);
                }
            }
        }
        Boolean b = intr.findSerializationSortAlphabetically(propertyAcc);
        sortKeys = Boolean.TRUE.equals(b);
    }
    JsonFormat.Value format = findFormatOverrides(provider, property, Map.class);
    if (format != null) {
        Boolean B = format.getFeature(JsonFormat.Feature.WRITE_SORTED_MAP_ENTRIES);
        if (B != null) {
            sortKeys = B.booleanValue();
        }
    }
    MapSerializer mser = withResolved(property, keySer, ser, ignored, sortKeys);
    // [databind#307]: allow filtering
    if (property != null) {
        AnnotatedMember m = property.getMember();
        if (m != null) {
            Object filterId = intr.findFilterId(m);
            if (filterId != null) {
                mser = mser.withFilterId(filterId);
            }
        }
        JsonInclude.Value inclV = property.findPropertyInclusion(provider.getConfig(), null);
        if (inclV != null) {
            JsonInclude.Include incl = inclV.getContentInclusion();
            if (incl != JsonInclude.Include.USE_DEFAULTS) {
                Object valueToSuppress;
                boolean suppressNulls;
                switch(incl) {
                    case NON_DEFAULT:
                        valueToSuppress = BeanUtil.getDefaultValue(_valueType);
                        suppressNulls = true;
                        if (valueToSuppress != null) {
                            if (valueToSuppress.getClass().isArray()) {
                                valueToSuppress = ArrayBuilders.getArrayComparator(valueToSuppress);
                            }
                        }
                        break;
                    case NON_ABSENT:
                        suppressNulls = true;
                        valueToSuppress = _valueType.isReferenceType() ? MARKER_FOR_EMPTY : null;
                        break;
                    case NON_EMPTY:
                        suppressNulls = true;
                        valueToSuppress = MARKER_FOR_EMPTY;
                        break;
                    case CUSTOM:
                        valueToSuppress = provider.includeFilterInstance(null, inclV.getContentFilter());
                        if (valueToSuppress == null) {
                            // is this legal?
                            suppressNulls = true;
                        } else {
                            suppressNulls = provider.includeFilterSuppressNulls(valueToSuppress);
                        }
                        break;
                    case NON_NULL:
                        valueToSuppress = null;
                        suppressNulls = true;
                        break;
                    // default
                    case ALWAYS:
                    default:
                        valueToSuppress = null;
                        // 30-Sep-2016, tatu: Should not need to check global flags here,
                        //   if inclusion forced to be ALWAYS
                        suppressNulls = false;
                        break;
                }
                mser = mser.withContentInclusion(valueToSuppress, suppressNulls);
            }
        }
    }
    return mser;
}
Also used : AnnotatedMember(com.fasterxml.jackson.databind.introspect.AnnotatedMember) JsonInclude(com.fasterxml.jackson.annotation.JsonInclude) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) JsonFormat(com.fasterxml.jackson.annotation.JsonFormat)

Example 17 with AnnotatedMember

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

the class StdSerializer method findAnnotatedContentSerializer.

/**
     * Convenience method for finding out possibly configured content value serializer.
     *
     * @since 2.7.4
     */
protected JsonSerializer<?> findAnnotatedContentSerializer(SerializerProvider serializers, BeanProperty property) throws JsonMappingException {
    if (property != null) {
        // First: if we have a property, may have property-annotation overrides
        AnnotatedMember m = property.getMember();
        final AnnotationIntrospector intr = serializers.getAnnotationIntrospector();
        if (m != null) {
            Object serDef = intr.findContentSerializer(m);
            if (serDef != null) {
                return serializers.serializerInstance(m, serDef);
            }
        }
    }
    return null;
}
Also used : AnnotatedMember(com.fasterxml.jackson.databind.introspect.AnnotatedMember)

Example 18 with AnnotatedMember

use of com.fasterxml.jackson.databind.introspect.AnnotatedMember in project fabric8 by jboss-fuse.

the class BeanValidationAnnotationIntrospector method hasIgnoreMarker.

@Override
public boolean hasIgnoreMarker(AnnotatedMember m) {
    Member member = m.getMember();
    int modifiers = member.getModifiers();
    if (Modifier.isTransient(modifiers)) {
        if (LOG.isLoggable(Level.FINE)) {
            LOG.fine("Ignoring transient member " + m);
        }
        return true;
    } else if (m instanceof AnnotatedMethod) {
        AnnotatedMethod method = (AnnotatedMethod) m;
        String methodName = method.getName();
        // lets see if there is a transient field of the same name as the getter
        if (methodName.startsWith("get") && method.getParameterCount() == 0) {
            String fieldName = Introspector.decapitalize(methodName.substring(3));
            Class<?> declaringClass = method.getDeclaringClass();
            Field field = findField(fieldName, declaringClass);
            if (field != null) {
                int fieldModifiers = field.getModifiers();
                if (Modifier.isTransient(fieldModifiers)) {
                    LOG.fine("Ignoring member " + m + " due to transient field called " + fieldName);
                    return true;
                }
            }
        }
    }
    return super.hasIgnoreMarker(m);
}
Also used : Field(java.lang.reflect.Field) AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) Member(java.lang.reflect.Member) AnnotatedMember(com.fasterxml.jackson.databind.introspect.AnnotatedMember)

Example 19 with AnnotatedMember

use of com.fasterxml.jackson.databind.introspect.AnnotatedMember in project beam by apache.

the class PipelineOptionsFactory method computeDeserializerForMethod.

private static JsonDeserializer<Object> computeDeserializerForMethod(Method method) {
    try {
        BeanProperty prop = createBeanProperty(method);
        AnnotatedMember annotatedMethod = prop.getMember();
        DefaultDeserializationContext context = DESERIALIZATION_CONTEXT.get();
        Object maybeDeserializerClass = context.getAnnotationIntrospector().findDeserializer(annotatedMethod);
        JsonDeserializer<Object> jsonDeserializer = context.deserializerInstance(annotatedMethod, maybeDeserializerClass);
        if (jsonDeserializer == null) {
            jsonDeserializer = context.findContextualValueDeserializer(prop.getType(), prop);
        }
        TypeDeserializer typeDeserializer = context.getFactory().findTypeDeserializer(context.getConfig(), prop.getType());
        if (typeDeserializer != null) {
            jsonDeserializer = new TypeWrappedDeserializer(typeDeserializer, jsonDeserializer);
        }
        return jsonDeserializer;
    } catch (JsonMappingException e) {
        throw new RuntimeException(e);
    }
}
Also used : DefaultDeserializationContext(com.fasterxml.jackson.databind.deser.DefaultDeserializationContext) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) AnnotatedMember(com.fasterxml.jackson.databind.introspect.AnnotatedMember) TypeDeserializer(com.fasterxml.jackson.databind.jsontype.TypeDeserializer) TypeWrappedDeserializer(com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer) BeanProperty(com.fasterxml.jackson.databind.BeanProperty)

Example 20 with AnnotatedMember

use of com.fasterxml.jackson.databind.introspect.AnnotatedMember in project jackson-module-afterburner by FasterXML.

the class SerializerModifier method findProperties.

protected PropertyAccessorCollector findProperties(Class<?> beanClass, SerializationConfig config, List<BeanPropertyWriter> beanProperties) {
    PropertyAccessorCollector collector = new PropertyAccessorCollector(beanClass);
    ListIterator<BeanPropertyWriter> it = beanProperties.listIterator();
    while (it.hasNext()) {
        BeanPropertyWriter bpw = it.next();
        AnnotatedMember member = bpw.getMember();
        Member jdkMember = member.getMember();
        // 11-Sep-2015, tatu: Let's skip virtual members (related to #57)
        if (jdkMember == null) {
            continue;
        }
        // We can't access private fields or methods, skip:
        if (Modifier.isPrivate(jdkMember.getModifiers())) {
            continue;
        }
        // 30-Jul-2012, tatu: [#6]: Needs to skip custom serializers, if any.
        if (bpw.hasSerializer()) {
            if (!isDefaultSerializer(config, bpw.getSerializer())) {
                continue;
            }
        }
        // [#9]: also skip unwrapping stuff...
        if (bpw.isUnwrapping()) {
            continue;
        }
        /* 04-Mar-2015, tatu: This might be too restrictive, as core databind has some 
             *   other sub-classes; if this becomes problematic may start using annotation
             *   to indicate "standard" implementations. But for now this solves the issue.
             */
        if (bpw.getClass() != BeanPropertyWriter.class) {
            continue;
        }
        Class<?> type = bpw.getType().getRawClass();
        boolean isMethod = (member instanceof AnnotatedMethod);
        if (type.isPrimitive()) {
            if (type == Integer.TYPE) {
                if (isMethod) {
                    it.set(collector.addIntGetter(bpw));
                } else {
                    it.set(collector.addIntField(bpw));
                }
            } else if (type == Long.TYPE) {
                if (isMethod) {
                    it.set(collector.addLongGetter(bpw));
                } else {
                    it.set(collector.addLongField(bpw));
                }
            } else if (type == Boolean.TYPE) {
                if (isMethod) {
                    it.set(collector.addBooleanGetter(bpw));
                } else {
                    it.set(collector.addBooleanField(bpw));
                }
            }
        } else {
            if (type == String.class) {
                if (isMethod) {
                    it.set(collector.addStringGetter(bpw));
                } else {
                    it.set(collector.addStringField(bpw));
                }
            } else {
                // any other Object types; we can at least call accessor
                if (isMethod) {
                    it.set(collector.addObjectGetter(bpw));
                } else {
                    it.set(collector.addObjectField(bpw));
                }
            }
        }
    }
    return collector;
}
Also used : AnnotatedMethod(com.fasterxml.jackson.databind.introspect.AnnotatedMethod) AnnotatedMember(com.fasterxml.jackson.databind.introspect.AnnotatedMember) Member(java.lang.reflect.Member) AnnotatedMember(com.fasterxml.jackson.databind.introspect.AnnotatedMember)

Aggregations

AnnotatedMember (com.fasterxml.jackson.databind.introspect.AnnotatedMember)22 AnnotatedMethod (com.fasterxml.jackson.databind.introspect.AnnotatedMethod)5 JsonFormat (com.fasterxml.jackson.annotation.JsonFormat)4 JsonIgnoreProperties (com.fasterxml.jackson.annotation.JsonIgnoreProperties)4 JsonIdentityInfo (com.fasterxml.jackson.annotation.JsonIdentityInfo)3 JsonIdentityReference (com.fasterxml.jackson.annotation.JsonIdentityReference)3 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)3 JavaType (com.fasterxml.jackson.databind.JavaType)3 PropertyMetadata (com.fasterxml.jackson.databind.PropertyMetadata)3 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)3 Annotation (java.lang.annotation.Annotation)3 Member (java.lang.reflect.Member)3 ArrayList (java.util.ArrayList)3 JsonInclude (com.fasterxml.jackson.annotation.JsonInclude)2 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)2 JsonUnwrapped (com.fasterxml.jackson.annotation.JsonUnwrapped)2 AnnotationIntrospector (com.fasterxml.jackson.databind.AnnotationIntrospector)2 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)2 BeanProperty (com.fasterxml.jackson.databind.BeanProperty)2 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)2