Example usage for com.fasterxml.jackson.databind JavaType isContainerType

List of usage examples for com.fasterxml.jackson.databind JavaType isContainerType

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind JavaType isContainerType.

Prototype

public abstract boolean isContainerType();

Source Link

Usage

From source file:org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper.java

public JavaType toJavaType(MessageProperties properties) {
    JavaType classType = getClassIdType(retrieveHeader(properties, getClassIdFieldName()));
    if (!classType.isContainerType() || classType.isArrayType()) {
        return classType;
    }/* w  w  w .j  a va  2 s .c  o  m*/

    JavaType contentClassType = getClassIdType(retrieveHeader(properties, getContentClassIdFieldName()));
    if (classType.getKeyType() == null) {
        return CollectionType.construct(classType.getRawClass(), contentClassType);
    }

    JavaType keyClassType = getClassIdType(retrieveHeader(properties, getKeyClassIdFieldName()));
    return MapType.construct(classType.getRawClass(), keyClassType, contentClassType);

}

From source file:org.springframework.amqp.support.converter.DefaultJackson2JavaTypeMapper.java

public void fromJavaType(JavaType javaType, MessageProperties properties) {
    addHeader(properties, getClassIdFieldName(), javaType.getRawClass());

    if (javaType.isContainerType() && !javaType.isArrayType()) {
        addHeader(properties, getContentClassIdFieldName(), javaType.getContentType().getRawClass());
    }/*from  w  w w .j ava  2  s .  com*/

    if (javaType.getKeyType() != null) {
        addHeader(properties, getKeyClassIdFieldName(), javaType.getKeyType().getRawClass());
    }
}

From source file:com.addthis.codec.jackson.CodecIntrospector.java

@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//from  w  ww .java  2s  .c  o m
     */
    if (!containerType.isContainerType()) {
        throw new IllegalArgumentException(
                "Must call method with a container type (got " + containerType + ")");
    }
    return _findTypeResolver(config, am, containerType);
}

From source file:com.addthis.codec.jackson.CodecIntrospector.java

@Override
public TypeResolverBuilder<?> findPropertyTypeResolver(MapperConfig<?> config, AnnotatedMember am,
        JavaType baseType) {
    /* As per definition of @JsonTypeInfo, should only apply to contents of container
     * (collection, map) types, not container types themselves:
     *//* w w  w . ja  v a2s. c o  m*/
    if (baseType.isContainerType())
        return null;
    // No per-member type overrides (yet)
    return _findTypeResolver(config, am, baseType);
}

From source file:org.linkedin.util.json.jackson.MetadataStyleSerializerFactory.java

@Override
@SuppressWarnings("unchecked")
public JsonSerializer<Object> createSerializer(SerializerProvider prov, JavaType origType)
        throws JsonMappingException {
    JsonSerializer<Object> serializer = (JsonSerializer<Object>) SERIALIZERS
            .get(origType.getRawClass().getName());

    if (serializer != null)
        return serializer;

    if (origType.isContainerType())
        return super.createSerializer(prov, origType);
    else//from w  ww. j a va  2s.  c o m
        return ToStringSerializer.instance;
}

From source file:org.mongojack.internal.MongoAnnotationIntrospector.java

public JsonDeserializer findObjectIdDeserializer(JavaType type) {
    if (type.getRawClass() == String.class) {
        return new ObjectIdDeserializers.ToStringDeserializer();
    } else if (type.getRawClass() == byte[].class) {
        return new ObjectIdDeserializers.ToByteArrayDeserializer();
    } else if (type.getRawClass() == DBRef.class) {
        JavaType dbRefType;/* w  ww.j  av a 2 s . co m*/
        if (type.isContainerType()) {
            if (type.isCollectionLikeType()) {
                dbRefType = type.containedType(0);
            } else if (type.isMapLikeType()) {
                dbRefType = type.containedType(1);
            } else {
                return null;
            }
        } else {
            dbRefType = type;
        }
        JsonDeserializer keyDeserializer = findObjectIdDeserializer(dbRefType.containedType(1));
        return new DBRefDeserializer(dbRefType.containedType(0), dbRefType.containedType(1), keyDeserializer);
    } else if (type.getRawClass() == org.bson.types.ObjectId.class) {
        // Don't know why someone would annotated an ObjectId with
        // @ObjectId, but handle it
        return new ObjectIdDeserializers.ToObjectIdDeserializer();
    }
    return null;
}

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

public Property resolveProperty(Type type, ModelConverterContext context, Annotation[] annotations,
        Iterator<ModelConverter> next) {
    JavaType propType = null;

    /**//from   www  . j av  a2s. c o  m
     * See java doc of NestedComplexType. This unwrapping makes sure that a
     * real type of field is passed to _mapper
     */
    if (type instanceof NestedComplexType) {
        propType = pMapper.constructType(((NestedComplexType) type).getOriginalType());
    } else {
        propType = pMapper.constructType(type);
    }

    log.debug("resolveProperty {}", propType);

    Property property = null;
    if (propType.isContainerType()) {
        JavaType keyType = propType.getKeyType();
        JavaType valueType = propType.getContentType();
        if (keyType != null && valueType != null) {
            property = new MapProperty()
                    .additionalProperties(context.resolveProperty(valueType, new Annotation[] {}));
        } else if (valueType != null) {
            ArrayProperty arrayProperty = new ArrayProperty()
                    .items(context.resolveProperty(valueType, new Annotation[] {}));
            if (pIsSetType(propType.getRawClass())) {
                arrayProperty.setUniqueItems(true);
            }
            property = arrayProperty;
        }
    } else {
        property = PrimitiveType.createProperty(propType);
    }

    if (property == null) {
        if (propType.isEnumType()) {
            property = new StringProperty();
            pAddEnumProps(propType.getRawClass(), property);
        } else if (pIsOptionalType(propType)) {
            property = context.resolveProperty(propType.containedType(0), null);
        } else {
            // complex type
            Model innerModel = context.resolve(type);
            if (innerModel instanceof ModelImpl) {
                ModelImpl mi = (ModelImpl) innerModel;
                property = new RefProperty(
                        StringUtils.isNotEmpty(mi.getReference()) ? mi.getReference() : mi.getName());
            }
        }
    }

    return property;
}

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 w  w  w. j ava  2 s .  c  o  m

    /**
     * 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;
}