Search in sources :

Example 6 with NamedType

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

the class StdSubtypeResolver method _collectAndResolve.

/*
    /**********************************************************
    /* Internal methods
    /**********************************************************
     */
/**
     * Method called to find subtypes for a specific type (class), using
     * type (class) as the unique key (in case of conflicts).
     */
protected void _collectAndResolve(AnnotatedClass annotatedType, NamedType namedType, MapperConfig<?> config, AnnotationIntrospector ai, HashMap<NamedType, NamedType> collectedSubtypes) {
    if (!namedType.hasName()) {
        String name = ai.findTypeName(annotatedType);
        if (name != null) {
            namedType = new NamedType(namedType.getType(), name);
        }
    }
    // First things first: is base type itself included?
    if (collectedSubtypes.containsKey(namedType)) {
        // if so, no recursion; however, may need to update name?
        if (namedType.hasName()) {
            NamedType prev = collectedSubtypes.get(namedType);
            if (!prev.hasName()) {
                collectedSubtypes.put(namedType, namedType);
            }
        }
        return;
    }
    // if it wasn't, add and check subtypes recursively
    collectedSubtypes.put(namedType, namedType);
    Collection<NamedType> st = ai.findSubtypes(annotatedType);
    if (st != null && !st.isEmpty()) {
        for (NamedType subtype : st) {
            AnnotatedClass subtypeClass = AnnotatedClass.constructWithoutSuperTypes(subtype.getType(), config);
            _collectAndResolve(subtypeClass, subtype, config, ai, collectedSubtypes);
        }
    }
}
Also used : NamedType(com.fasterxml.jackson.databind.jsontype.NamedType)

Example 7 with NamedType

use of com.fasterxml.jackson.databind.jsontype.NamedType 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)

Example 8 with NamedType

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

the class TypeNameIdResolver method construct.

public static TypeNameIdResolver construct(MapperConfig<?> config, JavaType baseType, Collection<NamedType> subtypes, boolean forSer, boolean forDeser) {
    // sanity check
    if (forSer == forDeser)
        throw new IllegalArgumentException();
    Map<String, String> typeToId = null;
    Map<String, JavaType> idToType = null;
    if (forSer) {
        typeToId = new HashMap<String, String>();
    }
    if (forDeser) {
        idToType = new HashMap<String, JavaType>();
        // 14-Apr-2016, tatu: Apparently needed for special case of `defaultImpl`;
        //    see [databind#1198] for details.
        typeToId = new TreeMap<String, String>();
    }
    if (subtypes != null) {
        for (NamedType t : subtypes) {
            /* no name? Need to figure out default; for now, let's just
                 * use non-qualified class name
                 */
            Class<?> cls = t.getType();
            String id = t.hasName() ? t.getName() : _defaultTypeId(cls);
            if (forSer) {
                typeToId.put(cls.getName(), id);
            }
            if (forDeser) {
                // One more problem; sometimes we have same name for multiple types;
                // if so, use most specific
                JavaType prev = idToType.get(id);
                if (prev != null) {
                    // Can only override if more specific
                    if (cls.isAssignableFrom(prev.getRawClass())) {
                        // nope, more generic (or same)
                        continue;
                    }
                }
                idToType.put(id, config.constructType(cls));
            }
        }
    }
    return new TypeNameIdResolver(config, baseType, typeToId, idToType);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType)

Example 9 with NamedType

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

the class SimpleModule method registerSubtypes.

/**
     * Method for adding set of subtypes to be registered with
     * {@link ObjectMapper}
     * this is an alternative to using annotations in super type to indicate subtypes.
     */
public SimpleModule registerSubtypes(Class<?>... subtypes) {
    if (_subtypes == null) {
        _subtypes = new LinkedHashSet<NamedType>(Math.max(16, subtypes.length));
    }
    for (Class<?> subtype : subtypes) {
        _checkNotNull(subtype, "subtype to register");
        _subtypes.add(new NamedType(subtype));
    }
    return this;
}
Also used : NamedType(com.fasterxml.jackson.databind.jsontype.NamedType)

Example 10 with NamedType

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

the class BeanSerializerFactory method findPropertyTypeSerializer.

/**
     * Method called to create a type information serializer for values of given
     * non-container property
     * if one is needed. If not needed (no polymorphic handling configured), should
     * return null.
     *
     * @param baseType Declared type to use as the base type for type information serializer
     * 
     * @return Type serializer to use for property values, if one is needed; null if not.
     */
public TypeSerializer findPropertyTypeSerializer(JavaType baseType, SerializationConfig config, AnnotatedMember accessor) throws JsonMappingException {
    AnnotationIntrospector ai = config.getAnnotationIntrospector();
    TypeResolverBuilder<?> b = ai.findPropertyTypeResolver(config, accessor, baseType);
    TypeSerializer typeSer;
    // Defaulting: if no annotations on member, check value class
    if (b == null) {
        typeSer = createTypeSerializer(config, baseType);
    } else {
        Collection<NamedType> subtypes = config.getSubtypeResolver().collectAndResolveSubtypesByClass(config, accessor, baseType);
        typeSer = b.buildTypeSerializer(config, baseType, subtypes);
    }
    return typeSer;
}
Also used : NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) TypeSerializer(com.fasterxml.jackson.databind.jsontype.TypeSerializer)

Aggregations

NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)27 AnnotationIntrospector (com.fasterxml.jackson.databind.AnnotationIntrospector)4 JavaType (com.fasterxml.jackson.databind.JavaType)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ApiModel (io.swagger.annotations.ApiModel)3 HashMap (java.util.HashMap)3 Before (org.junit.Before)3 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)2 TypeSerializer (com.fasterxml.jackson.databind.jsontype.TypeSerializer)2 DataSchema (io.druid.segment.indexing.DataSchema)2 UniformGranularitySpec (io.druid.segment.indexing.granularity.UniformGranularitySpec)2 HashBasedNumberedShardSpec (io.druid.timeline.partition.HashBasedNumberedShardSpec)2 ApiModelProperty (io.swagger.annotations.ApiModelProperty)2 ComposedModel (io.swagger.models.ComposedModel)2 Model (io.swagger.models.Model)2 ModelImpl (io.swagger.models.ModelImpl)2 RefModel (io.swagger.models.RefModel)2 AbstractNumericProperty (io.swagger.models.properties.AbstractNumericProperty)2 ArrayProperty (io.swagger.models.properties.ArrayProperty)2