Search in sources :

Example 1 with JsonTypeInfo

use of org.codehaus.jackson.annotate.JsonTypeInfo in project typescript-generator by vojtechhabarta.

the class Jackson1Parser method parseBean.

private BeanModel parseBean(SourceType<Class<?>> sourceClass) {
    final List<PropertyModel> properties = new ArrayList<>();
    final BeanHelper beanHelper = getBeanHelper(sourceClass.type);
    if (beanHelper != null) {
        for (final BeanPropertyWriter beanPropertyWriter : beanHelper.getProperties()) {
            final Member member = beanPropertyWriter.getMember().getMember();
            final PropertyMember propertyMember = wrapMember(settings.getTypeParser(), member, /*creatorIndex*/
            null, beanPropertyWriter::getAnnotation, beanPropertyWriter.getName(), sourceClass.type);
            if (propertyMember == null) {
                continue;
            }
            Type propertyType = beanPropertyWriter.getGenericPropertyType();
            if (!isAnnotatedPropertyIncluded(beanPropertyWriter::getAnnotation, sourceClass.type.getName() + "." + beanPropertyWriter.getName())) {
                continue;
            }
            final boolean optional = isPropertyOptional(propertyMember);
            properties.add(processTypeAndCreateProperty(beanPropertyWriter.getName(), propertyType, null, optional, null, sourceClass.type, member, null, null));
        }
    }
    final JsonTypeInfo jsonTypeInfo = sourceClass.type.getAnnotation(JsonTypeInfo.class);
    if (jsonTypeInfo != null && jsonTypeInfo.include() == JsonTypeInfo.As.PROPERTY) {
        if (!containsProperty(properties, jsonTypeInfo.property())) {
            properties.add(new PropertyModel(jsonTypeInfo.property(), String.class, false, null, null, null, null, null));
        }
    }
    final JsonSubTypes jsonSubTypes = sourceClass.type.getAnnotation(JsonSubTypes.class);
    if (jsonSubTypes != null) {
        for (JsonSubTypes.Type type : jsonSubTypes.value()) {
            addBeanToQueue(new SourceType<>(type.value(), sourceClass.type, "<subClass>"));
        }
    }
    final Type superclass = sourceClass.type.getGenericSuperclass() == Object.class ? null : sourceClass.type.getGenericSuperclass();
    if (superclass != null) {
        addBeanToQueue(new SourceType<>(superclass, sourceClass.type, "<superClass>"));
    }
    final List<Type> interfaces = Arrays.asList(sourceClass.type.getGenericInterfaces());
    for (Type aInterface : interfaces) {
        addBeanToQueue(new SourceType<>(aInterface, sourceClass.type, "<interface>"));
    }
    return new BeanModel(sourceClass.type, superclass, null, null, null, interfaces, properties, null);
}
Also used : JsonSubTypes(org.codehaus.jackson.annotate.JsonSubTypes) ArrayList(java.util.ArrayList) JsonTypeInfo(org.codehaus.jackson.annotate.JsonTypeInfo) Type(java.lang.reflect.Type) JavaType(org.codehaus.jackson.type.JavaType) BeanPropertyWriter(org.codehaus.jackson.map.ser.BeanPropertyWriter) Member(java.lang.reflect.Member) PropertyMember(cz.habarta.typescript.generator.util.PropertyMember) PropertyMember(cz.habarta.typescript.generator.util.PropertyMember)

Aggregations

PropertyMember (cz.habarta.typescript.generator.util.PropertyMember)1 Member (java.lang.reflect.Member)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 JsonSubTypes (org.codehaus.jackson.annotate.JsonSubTypes)1 JsonTypeInfo (org.codehaus.jackson.annotate.JsonTypeInfo)1 BeanPropertyWriter (org.codehaus.jackson.map.ser.BeanPropertyWriter)1 JavaType (org.codehaus.jackson.type.JavaType)1