com.fasterxml.jackson.databind.cfg.MapperConfig Java Examples

The following examples show how to use com.fasterxml.jackson.databind.cfg.MapperConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JacksonAnnotationIntrospector.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected BeanPropertyWriter _constructVirtualProperty(JsonAppend.Attr attr,
        MapperConfig<?> config, AnnotatedClass ac, JavaType type)
{
    PropertyMetadata metadata = attr.required() ?
                PropertyMetadata.STD_REQUIRED : PropertyMetadata.STD_OPTIONAL;
    // could add Index, Description in future, if those matter
    String attrName = attr.value();

    // allow explicit renaming; if none, default to attribute name
    PropertyName propName = _propertyName(attr.propName(), attr.propNamespace());
    if (!propName.hasSimpleName()) {
        propName = PropertyName.construct(attrName);
    }
    // now, then, we need a placeholder for member (no real Field/Method):
    AnnotatedMember member = new VirtualAnnotatedMember(ac, ac.getRawType(),
            attrName, type);
    // and with that and property definition
    SimpleBeanPropertyDefinition propDef = SimpleBeanPropertyDefinition.construct(config,
            member, propName, metadata, attr.include());
    // can construct the property writer
    return AttributePropertyWriter.construct(attrName, propDef,
            ac.getAnnotations(), type);
}
 
Example #2
Source File: JaxbAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
public PropertyName findNameForSerialization(MapperConfig<?> config, Annotated a)
{
    // 16-Sep-2016, tatu: Prior to 2.9 logic her more complicated, on assumption
    //    that visibility rules may require return of "" if method/fied visible;
    //    however, that is not required and causes issues so... now simpler:
    if (a instanceof AnnotatedMethod) {
        AnnotatedMethod am = (AnnotatedMethod) a;
        return isVisible(am)
            ? findJaxbPropertyName(am, am.getRawType(), BeanUtil.okNameForGetter(am))
            : null;
    }
    if (a instanceof AnnotatedField) {
        AnnotatedField af = (AnnotatedField) a;
        return isVisible(af)
            ? findJaxbPropertyName(af, af.getRawType(), null)
            : null;
    }
    return null;
}
 
Example #3
Source File: ConcreteBeanPropertyBase.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public JsonFormat.Value findPropertyFormat(MapperConfig<?> config, Class<?> baseType)
{
    // 15-Apr-2016, tatu: Let's calculate lazily, retain; assumption being however that
    //    baseType is always the same
    JsonFormat.Value v = _propertyFormat;
    if (v == null) {
        JsonFormat.Value v1 = config.getDefaultPropertyFormat(baseType);
        JsonFormat.Value v2 = null;
        AnnotationIntrospector intr = config.getAnnotationIntrospector();
        if (intr != null) {
            AnnotatedMember member = getMember();
            if (member != null) {
                v2 = intr.findFormat(member);
            }
        }
        if (v1 == null) {
            v = (v2 == null) ? EMPTY_FORMAT : v2;
        } else {
            v = (v2 == null) ? v1 : v1.withOverrides(v2);
        }
        _propertyFormat = v;
    }
    return v;
}
 
Example #4
Source File: ParanamerAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
public PropertyName findNameForDeserialization(MapperConfig<?> config, Annotated a)
{
    /* 14-Apr-2014, tatu: Important -- we should NOT introspect name here,
     *   since we are not using annotations; instead it needs to be done
     *   in {@link #findParameterSourceName(AnnotatedParameter)}.
     */
    /*
    PropertyName name = super.findNameForDeserialization(a);
    if (name == null) {
        if (a instanceof AnnotatedParameter) {
            String rawName _paranamer.findParameterName((AnnotatedParameter) a);
            if (rawName != null) {
                return new PropertyName(rawName);
            }
        }
    }
    */
    return null;
}
 
Example #5
Source File: JaxbAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
public Boolean hasRequiredMarker(MapperConfig<?> config, AnnotatedMember m) {
    // 17-Oct-2017, tatu: [modules-base#32]
    //   Before 2.9.3, was handling `true` correctly,
    //   but otherwise used confusing logic (probably in attempt to try to avoid
    //   reporting not-required for default value case
    XmlAttribute attr = m.getAnnotation(XmlAttribute.class);
    if (attr != null) {
        return attr.required();
    }
    XmlElement elem = m.getAnnotation(XmlElement.class);
    if (elem != null) {
        return elem.required();
    }
    return null;
}
 
Example #6
Source File: StdSubtypeResolver.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> config,
        AnnotatedClass baseType)
{
    final Class<?> rawBase = baseType.getRawType();
    Set<Class<?>> typesHandled = new HashSet<Class<?>>();
    Map<String,NamedType> byName = new LinkedHashMap<String,NamedType>();

    NamedType rootType = new NamedType(rawBase, null);
    _collectAndResolveByTypeId(baseType, rootType, config, typesHandled, byName);
    
    if (_registeredSubtypes != null) {
        for (NamedType subtype : _registeredSubtypes) {
            // is it a subtype of root type?
            if (rawBase.isAssignableFrom(subtype.getType())) { // yes
                AnnotatedClass curr = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
                        subtype.getType());
                _collectAndResolveByTypeId(curr, subtype, config, typesHandled, byName);
            }
        }
    }
    return _combineNamedAndUnnamed(rawBase, typesHandled, byName);
}
 
Example #7
Source File: ParanamerOnJacksonAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
public PropertyName findNameForDeserialization(MapperConfig<?> config, Annotated a)
{
    /* 14-Apr-2014, tatu: Important -- we should NOT introspect name here,
     *   since we are not using annotations; instead it needs to be done
     *   in {@link #findParameterSourceName(AnnotatedParameter)}.
     */
    /*
    PropertyName name = super.findNameForDeserialization(a);
    if (name == null) {
        if (a instanceof AnnotatedParameter) {
            String rawName _paranamer.findParameterName((AnnotatedParameter) a);
            if (rawName != null) {
                return new PropertyName(rawName);
            }
        }
    }
    */
    return null;
}
 
Example #8
Source File: JaxbAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 6 votes vote down vote up
@Override
public PropertyName findNameForDeserialization(MapperConfig<?> config, Annotated a)
{
    // 16-Sep-2016, tatu: Prior to 2.9 logic her more complicated, on assumption
    //    that visibility rules may require return of "" if method/fied visible;
    //    however, that is not required and causes issues so... now simpler:
    if (a instanceof AnnotatedMethod) {
        AnnotatedMethod am = (AnnotatedMethod) a;
        if (!isVisible(am)) {
            return null;
        }
        Class<?> rawType = am.getRawParameterType(0);
        return findJaxbPropertyName(am, rawType, BeanUtil.okNameForMutator(am, "set"));
    }
    if (a instanceof AnnotatedField) {
        AnnotatedField af = (AnnotatedField) a;
        return isVisible(af)
            ? findJaxbPropertyName(af, af.getRawType(), null)
            : null;
    }
    return null;
}
 
Example #9
Source File: AnnotatedClassResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static AnnotatedClass resolveWithoutSuperTypes(MapperConfig<?> config, JavaType forType,
        MixInResolver r)
{
    if (forType.isArrayType() && skippableArray(config, forType.getRawClass())) {
        return createArrayType(config, forType.getRawClass());
    }
    return new AnnotatedClassResolver(config, forType, r).resolveWithoutSuperTypes();
}
 
Example #10
Source File: SpringHandlerInstantiator.java    From java-technology-stack with MIT License 5 votes vote down vote up
/** @since 4.3 */
@Override
public ValueInstantiator valueInstantiatorInstance(MapperConfig<?> config,
		Annotated annotated, Class<?> implClass) {

	return (ValueInstantiator) this.beanFactory.createBean(implClass);
}
 
Example #11
Source File: SpringHandlerInstantiator.java    From java-technology-stack with MIT License 5 votes vote down vote up
/** @since 4.3 */
@Override
public Converter<?, ?> converterInstance(MapperConfig<?> config,
		Annotated annotated, Class<?> implClass) {

	return (Converter<?, ?>) this.beanFactory.createBean(implClass);
}
 
Example #12
Source File: JacksonAnnotationIntrospector.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypeResolverBuilder<?> findPropertyContentTypeResolver(MapperConfig<?> config,
        AnnotatedMember am, JavaType containerType)
{
    /* First: let's ensure property is a container type: caller should have
     * verified but just to be sure
     */
    if (containerType.getContentType() == null) {
        throw new IllegalArgumentException("Must call method with a container or reference type (got "+containerType+")");
    }
    return _findTypeResolver(config, am, containerType);
}
 
Example #13
Source File: BeanProperty.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public JsonFormat.Value findPropertyFormat(MapperConfig<?> config, Class<?> baseType) {
    JsonFormat.Value v0 = config.getDefaultPropertyFormat(baseType);
    AnnotationIntrospector intr = config.getAnnotationIntrospector();
    if ((intr == null) || (_member == null)) {
        return v0;
    }
    JsonFormat.Value v = intr.findFormat(_member);
    if (v == null) {
        return v0;
    }
    return v0.withOverrides(v);
}
 
Example #14
Source File: JaxbAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
@Override
    public JsonTypeInfo.Value findPolymorphicTypeInfo(MapperConfig<?> config, Annotated ann)
    {
        // If simple type, @XmlElements and @XmlElementRefs are applicable.
        // Note: @XmlElement and @XmlElementRef are NOT handled here, since they
        // are handled specifically as non-polymorphic indication
        // of the actual type
        XmlElements elems = findAnnotation(XmlElements.class, ann, false, false, false);

        if (elems == null) {
            XmlElementRefs elemRefs = findAnnotation(XmlElementRefs.class, ann, false, false, false);
            if (elemRefs == null) {
                return null;
            }
        }
        // JAXB always uses type name as id; let's consider WRAPPER_OBJECT to be canonical inclusion method
        // (TODO: should it be possible to merge such annotations in AnnotationIntrospector pair?)
        return JsonTypeInfo.Value.construct(JsonTypeInfo.Id.NAME, JsonTypeInfo.As.WRAPPER_OBJECT,
                "", null, false);
/*// in 2.0 we had:
        TypeResolverBuilder<?> b = new StdTypeResolverBuilder();
        // JAXB always uses type name as id
        b = b.init(JsonTypeInfo.Id.NAME, null);
        // and let's consider WRAPPER_OBJECT to be canonical inclusion method
        b = b.inclusion(JsonTypeInfo.As.WRAPPER_OBJECT);
        return b;
*/
    }
 
Example #15
Source File: POJOPropertyBuilder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected POJOPropertyBuilder(MapperConfig<?> config, AnnotationIntrospector ai,
        boolean forSerialization, PropertyName internalName, PropertyName name)
{
    _config = config;
    _annotationIntrospector = ai;
    _internalName = internalName;
    _name = name;
    _forSerialization = forSerialization;
}
 
Example #16
Source File: SpringHandlerInstantiator.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/** @since 4.3 */
@Override
public Converter<?, ?> converterInstance(MapperConfig<?> config,
		Annotated annotated, Class<?> implClass) {

	return (Converter<?, ?>) this.beanFactory.createBean(implClass);
}
 
Example #17
Source File: JaxbAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
private boolean adapterTypeMatches(MapperConfig<?> config, XmlAdapter<?,?> adapter,
        Class<?> targetType)
{
    JavaType adapterType = config.constructType(adapter.getClass());
    JavaType[] params = config.getTypeFactory().findTypeParameters(adapterType, XmlAdapter.class);
    // should not happen, except if our type resolution has a flaw, but:
    Class<?> boundType = (params == null || params.length < 2) ? Object.class
            : params[1].getRawClass();
    return boundType.isAssignableFrom(targetType);
}
 
Example #18
Source File: EsPropertyNamingStrategy.java    From soundwave with Apache License 2.0 5 votes vote down vote up
@Override
public String nameForSetterMethod(MapperConfig<?> config, AnnotatedMethod method,
                                  String defaultName) {
  if (method.getDeclaringClass() == this.effectiveType) {
    return fieldToJsonMapping
        .getOrDefault(defaultName, super.nameForSetterMethod(config, method, defaultName));
  } else {
    return super.nameForSetterMethod(config, method, defaultName);
  }
}
 
Example #19
Source File: AnnotationIntrospectorPair.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override // since 2.7
public JavaType refineSerializationType(MapperConfig<?> config,
        Annotated a, JavaType baseType) throws JsonMappingException
{
    JavaType t = _secondary.refineSerializationType(config, a, baseType);
    return _primary.refineSerializationType(config, a, t);
}
 
Example #20
Source File: JaxbAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
@Override
public Object findSerializationConverter(MapperConfig<?> config, Annotated a)
{
    Class<?> serType = _rawSerializationType(a);
    // Can apply to both container and regular type; no difference yet here
    XmlAdapter<?,?> adapter = findAdapter(config, a, true, serType);
    if (adapter != null) {
        return _converter(config, adapter, true);
    }
    return null;
}
 
Example #21
Source File: AnnotationIntrospectorPair.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override // since 2.7
public AnnotatedMethod resolveSetterConflict(MapperConfig<?> config,
        AnnotatedMethod setter1, AnnotatedMethod setter2)
{
    AnnotatedMethod res = _primary.resolveSetterConflict(config, setter1, setter2);
    if (res == null) {
        res = _secondary.resolveSetterConflict(config, setter1, setter2);
    }
    return res;
}
 
Example #22
Source File: GuiceAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
@Override // since 2.9
public JacksonInject.Value findInjectableValue(MapperConfig<?> config, AnnotatedMember m) {
    Object id = _findGuiceInjectId(m);
    if (id == null) {
        return null;
    }
    return JacksonInject.Value.forId(id);
}
 
Example #23
Source File: BasicClassIntrospector.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected BasicBeanDescription _findStdJdkCollectionDesc(MapperConfig<?> cfg, JavaType type)
{
    if (_isStdJDKCollection(type)) {
        return BasicBeanDescription.forOtherUse(cfg, type,
                _resolveAnnotatedClass(cfg, type, cfg));
    }
    return null;
}
 
Example #24
Source File: AnnotationIntrospectorPair.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypeResolverBuilder<?> findTypeResolver(MapperConfig<?> config,
        AnnotatedClass ac, JavaType baseType)
{
    TypeResolverBuilder<?> b = _primary.findTypeResolver(config, ac, baseType);
    if (b == null) {
        b = _secondary.findTypeResolver(config, ac, baseType);
    }
    return b;
}
 
Example #25
Source File: JaxbAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
protected Converter<Object,Object> _converter(MapperConfig<?> config,
        XmlAdapter<?,?> adapter, boolean forSerialization)
{
    TypeFactory tf = config.getTypeFactory();
    JavaType adapterType = tf.constructType(adapter.getClass());
    JavaType[] pt = tf.findTypeParameters(adapterType, XmlAdapter.class);
    // Order of type parameters for Converter is reverse between serializer, deserializer,
    // whereas JAXB just uses single ordering
    if (forSerialization) {
        return new AdapterConverter(adapter, pt[1], pt[0], forSerialization);
    }
    return new AdapterConverter(adapter, pt[0], pt[1], forSerialization);
}
 
Example #26
Source File: StdSubtypeResolver.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Collection<NamedType> collectAndResolveSubtypesByTypeId(MapperConfig<?> config, 
        AnnotatedMember property, JavaType baseType)
{
    final AnnotationIntrospector ai = config.getAnnotationIntrospector();
    Class<?> rawBase = 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 = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
            rawBase);
    _collectAndResolveByTypeId(ac, rootType, config, typesHandled, byName);
    
    // then with definitions from property
    if (property != null) {
        Collection<NamedType> st = ai.findSubtypes(property);
        if (st != null) {
            for (NamedType nt : st) {
                ac = AnnotatedClassResolver.resolveWithoutSuperTypes(config, nt.getType());
                _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 = AnnotatedClassResolver.resolveWithoutSuperTypes(config,
                        subtype.getType());
                _collectAndResolveByTypeId(curr, subtype, config, typesHandled, byName);
            }
        }
    }
    return _combineNamedAndUnnamed(rawBase, typesHandled, byName);
}
 
Example #27
Source File: JaxbAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
@Override
public String findImplicitPropertyName(MapperConfig<?> config, AnnotatedMember m) {
    XmlValue valueInfo = m.getAnnotation(XmlValue.class);
    if (valueInfo != null) {
        return _xmlValueName;
    }
    return null;
}
 
Example #28
Source File: JaxbAnnotationIntrospector.java    From jackson-modules-base with Apache License 2.0 5 votes vote down vote up
@Override
public Object findDeserializationConverter(MapperConfig<?> config, Annotated a)
{
    // One limitation: for structured types this is done later on
    Class<?> deserType = _rawDeserializationType(a);
    XmlAdapter<?,?> adapter = findAdapter(config, a, true, deserType);
    if (adapter != null) {
        return _converter(config, adapter, false);
    }
    return null;
}
 
Example #29
Source File: BasicBeanDescription.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Factory method to use for constructing an instance to use for purposes
 * other than building serializers or deserializers; will only have information
 * on class, not on properties.
 */
public static BasicBeanDescription forOtherUse(MapperConfig<?> config,
        JavaType type, AnnotatedClass ac)
{
    return new BasicBeanDescription(config, type,
            ac, Collections.<BeanPropertyDefinition>emptyList());
}
 
Example #30
Source File: SimpleBeanPropertyDefinition.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @since 2.7
 */
public static SimpleBeanPropertyDefinition construct(MapperConfig<?> config,
        AnnotatedMember member, PropertyName name, PropertyMetadata metadata,
        JsonInclude.Value inclusion) {
      return new SimpleBeanPropertyDefinition(config.getAnnotationIntrospector(),
              member, name, metadata, inclusion);
}