Search in sources :

Example 1 with JsonSubTypes

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonSubTypes in project torodb by torodb.

the class AbstractBackendSerializer method acceptJsonFormatVisitor.

@Override
public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType type) throws JsonMappingException {
    if (visitor == null) {
        return;
    }
    JsonObjectFormatVisitor v = visitor.expectObjectFormat(type);
    SerializerProvider prov = visitor.getProvider();
    final SerializationConfig config = prov.getConfig();
    BeanDescription beanDesc = config.introspect(type);
    JsonSubTypes jsonSubTypes;
    if (v != null) {
        for (BeanPropertyDefinition propDef : beanDesc.findProperties()) {
            if (propDef.isExplicitlyIncluded()) {
                jsonSubTypes = propDef.getPrimaryMember().getAnnotation(JsonSubTypes.class);
                if (jsonSubTypes != null) {
                    for (JsonSubTypes.Type jsonSubType : jsonSubTypes.value()) {
                        JavaType subType = TypeFactory.defaultInstance().constructType(jsonSubType.value());
                        depositSchemaProperty(v, jsonSubType.name(), subType);
                    }
                } else {
                    depositSchemaProperty(v, propDef.getName(), propDef.getPrimaryMember().getType(beanDesc.bindingsForBeanType()));
                }
            }
        }
    }
}
Also used : JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) JavaType(com.fasterxml.jackson.databind.JavaType) JsonObjectFormatVisitor(com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider)

Example 2 with JsonSubTypes

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonSubTypes in project typescript-generator by vojtechhabarta.

the class Jackson2Parser 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 propertyMember = beanPropertyWriter.getMember().getMember();
            checkMember(propertyMember, beanPropertyWriter.getName(), sourceClass.type);
            Type propertyType = getGenericType(propertyMember);
            if (propertyType == JsonNode.class) {
                propertyType = Object.class;
            }
            boolean isInAnnotationFilter = settings.includePropertyAnnotations.isEmpty();
            if (!isInAnnotationFilter) {
                for (Class<? extends Annotation> optionalAnnotation : settings.includePropertyAnnotations) {
                    if (beanPropertyWriter.getAnnotation(optionalAnnotation) != null) {
                        isInAnnotationFilter = true;
                        break;
                    }
                }
                if (!isInAnnotationFilter) {
                    System.out.println("Skipping " + sourceClass.type + "." + beanPropertyWriter.getName() + " because it is missing an annotation from includePropertyAnnotations!");
                    continue;
                }
            }
            final boolean optional = settings.optionalProperties == OptionalProperties.useLibraryDefinition ? !beanPropertyWriter.isRequired() : isAnnotatedPropertyOptional(new AnnotatedProperty() {

                @Override
                public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
                    return beanPropertyWriter.getAnnotation(annotationClass);
                }
            });
            // @JsonUnwrapped
            PropertyModel.PullProperties pullProperties = null;
            final Member originalMember = beanPropertyWriter.getMember().getMember();
            if (originalMember instanceof AccessibleObject) {
                final AccessibleObject accessibleObject = (AccessibleObject) originalMember;
                final JsonUnwrapped annotation = accessibleObject.getAnnotation(JsonUnwrapped.class);
                if (annotation != null && annotation.enabled()) {
                    pullProperties = new PropertyModel.PullProperties(annotation.prefix(), annotation.suffix());
                }
            }
            properties.add(processTypeAndCreateProperty(beanPropertyWriter.getName(), propertyType, optional, sourceClass.type, originalMember, pullProperties));
        }
    }
    if (sourceClass.type.isEnum()) {
        return new BeanModel(sourceClass.type, null, null, null, null, null, properties, null);
    }
    final String discriminantProperty;
    final String discriminantLiteral;
    final JsonTypeInfo jsonTypeInfo = sourceClass.type.getAnnotation(JsonTypeInfo.class);
    final JsonTypeInfo parentJsonTypeInfo;
    if (isSupported(jsonTypeInfo)) {
        // this is parent
        discriminantProperty = getDiscriminantPropertyName(jsonTypeInfo);
        discriminantLiteral = null;
    } else if (isSupported(parentJsonTypeInfo = getAnnotationRecursive(sourceClass.type, JsonTypeInfo.class))) {
        // this is child class
        discriminantProperty = getDiscriminantPropertyName(parentJsonTypeInfo);
        discriminantLiteral = getTypeName(parentJsonTypeInfo, sourceClass.type);
    } else {
        // not part of explicit hierarchy
        discriminantProperty = null;
        discriminantLiteral = null;
    }
    final List<Class<?>> taggedUnionClasses;
    final JsonSubTypes jsonSubTypes = sourceClass.type.getAnnotation(JsonSubTypes.class);
    if (jsonSubTypes != null) {
        taggedUnionClasses = new ArrayList<>();
        for (JsonSubTypes.Type type : jsonSubTypes.value()) {
            addBeanToQueue(new SourceType<>(type.value(), sourceClass.type, "<subClass>"));
            taggedUnionClasses.add(type.value());
        }
    } else {
        taggedUnionClasses = null;
    }
    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, taggedUnionClasses, discriminantProperty, discriminantLiteral, interfaces, properties, null);
}
Also used : AccessibleObject(java.lang.reflect.AccessibleObject) JsonUnwrapped(com.fasterxml.jackson.annotation.JsonUnwrapped) Member(java.lang.reflect.Member) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) Annotation(java.lang.annotation.Annotation) Type(java.lang.reflect.Type) AccessibleObject(java.lang.reflect.AccessibleObject)

Example 3 with JsonSubTypes

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonSubTypes in project typescript-generator by vojtechhabarta.

the class Jackson2Parser method getTypeName.

private String getTypeName(JsonTypeInfo parentJsonTypeInfo, final Class<?> cls) {
    // Id.CLASS
    if (parentJsonTypeInfo.use() == JsonTypeInfo.Id.CLASS) {
        return cls.getName();
    }
    // find @JsonTypeName recursively
    final JsonTypeName jsonTypeName = getAnnotationRecursive(cls, JsonTypeName.class);
    if (jsonTypeName != null && !jsonTypeName.value().isEmpty()) {
        return jsonTypeName.value();
    }
    // find @JsonSubTypes.Type recursively
    final JsonSubTypes jsonSubTypes = getAnnotationRecursive(cls, JsonSubTypes.class, new Predicate<JsonSubTypes>() {

        @Override
        public boolean test(JsonSubTypes types) {
            return getJsonSubTypeForClass(types, cls) != null;
        }
    });
    if (jsonSubTypes != null) {
        final JsonSubTypes.Type jsonSubType = getJsonSubTypeForClass(jsonSubTypes, cls);
        if (!jsonSubType.name().isEmpty()) {
            return jsonSubType.name();
        }
    }
    // use simplified class name if it's not an interface or abstract
    if (!cls.isInterface() && !Modifier.isAbstract(cls.getModifiers())) {
        return cls.getName().substring(cls.getName().lastIndexOf(".") + 1);
    }
    return null;
}
Also used : JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes)

Example 4 with JsonSubTypes

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonSubTypes in project dhis2-core by dhis2.

the class StAXNodeSerializer method isJsonSubTypeClass.

private boolean isJsonSubTypeClass(SimpleNode simpleNode) {
    checkNotNull(simpleNode);
    if (simpleNode.getValue() == null) {
        return false;
    }
    Class<?> aClass = simpleNode.getValue().getClass().getSuperclass();
    JsonSubTypes annotation = aClass.getAnnotation(JsonSubTypes.class);
    return annotation != null;
}
Also used : JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes)

Example 5 with JsonSubTypes

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonSubTypes in project AJSC by att.

the class Unmarshaller method determineImplementationClass.

/**
 * This method checks the specified bean property to locate the implementation class to be used. If the
 * implementation class supports polymorphic mappings, then the implementation class is further inspected for
 * JsonTypeInfo annotations. If the annotation is present, then the annotation directs the processing. Because the
 * processing of the annotation may be data-dependent, the mapped data to be unmarshalled must be provided as well.
 *
 * @param beanProperty
 *            The bean property we are mapping the contents into
 * @param obj
 *            The object that contains the mappings
 * @return The class to be constructed to contain the mappings
 * @throws UnmarshallException
 *             If the json type info uses unsupported metadata determination
 */
@SuppressWarnings("unchecked")
private Class<? extends ModelObject> determineImplementationClass(BeanProperty beanProperty, Object obj) throws UnmarshallException {
    Class<? extends ModelObject> implClass = null;
    JsonTypeInfo typeInfo = null;
    JsonSubTypes subTypes = null;
    Class<?> defaultImplClass = null;
    /*
         * First, try to determine the implementation class to be created based on the bean property type. If the bean
         * property is a scalar type, then use it as is. If it is a list, then use the first generic type. If it is a
         * map, use the second generic type (maps are always assumed to be keyed by strings).
         */
    implClass = (Class<? extends ModelObject>) beanProperty.getPropertyType();
    if (beanProperty.isList()) {
        implClass = (Class<? extends ModelObject>) beanProperty.getCollectionTypes()[0];
    } else if (beanProperty.isMap()) {
        implClass = (Class<? extends ModelObject>) beanProperty.getCollectionTypes()[1];
    }
    /*
         * HACK: If the implClass is Constraint, then we have a special case. We need to examine the first key in the
         * map to determine the type of constraint and return that name.
         */
    if (implClass.equals(Constraint.class)) {
        if (obj instanceof List) {
            Map<String, Object> map = (Map<String, Object>) ((List<Object>) obj).get(0);
            String constraintType = (String) map.keySet().toArray()[0];
            for (int index = 0; index < CONSTRAINT_TYPES.length; index++) {
                if (CONSTRAINT_TYPES[index].equals(constraintType)) {
                    implClass = CONSTRAINT_CLASSES[index];
                    return implClass;
                }
            }
        }
    }
    /*
         * If typeInfo annotation is present on the property type class, then check it to get the actual implementation
         * class. Otherwise, we will instantiate the property defined.
         */
    typeInfo = (JsonTypeInfo) ObjectHelper.getClassAnnotation(implClass, JsonTypeInfo.class);
    if (typeInfo != null) {
        subTypes = (JsonSubTypes) ObjectHelper.getClassAnnotation(implClass, JsonSubTypes.class);
        defaultImplClass = typeInfo.defaultImpl();
        JsonTypeInfo.Id use = typeInfo.use();
        JsonTypeInfo.As include = typeInfo.include();
        if (use.equals(JsonTypeInfo.Id.NAME) && include.equals(JsonTypeInfo.As.PROPERTY)) {
            if (obj instanceof Map) {
                Map<String, Object> map = (Map<String, Object>) obj;
                String property = typeInfo.property();
                String propertyValue = (String) map.get(property);
                implClass = (Class<? extends ModelObject>) defaultImplClass;
                if (propertyValue != null) {
                    JsonSubTypes.Type[] types = subTypes.value();
                    for (JsonSubTypes.Type type : types) {
                        if (type.name().equals(propertyValue)) {
                            implClass = (Class<? extends ModelObject>) type.value();
                            break;
                        }
                    }
                }
            }
        } else if (use.equals(JsonTypeInfo.Id.CUSTOM)) {
            JsonDeserialize deserializeAnnotation = (JsonDeserialize) ObjectHelper.getClassAnnotation(implClass, JsonDeserialize.class);
            Class<? extends JsonDeserializer> deserializer = deserializeAnnotation.using();
        } else {
            throw new UnmarshallException(String.format("Only JsonTypeInfo use=\"NAME\" and include=\"PROPERTY\" " + " or use=\"CUSTOM\" with custom deserializer are supported.  The mapping specified " + "use=\"%s\" and include=\"%s\"", use.name(), include.name()));
        }
    }
    return implClass;
}
Also used : JsonSubTypes(com.fasterxml.jackson.annotation.JsonSubTypes) JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) JsonDeserializer(com.fasterxml.jackson.databind.JsonDeserializer) PatternConstraint(com.att.cdp.openstack.heat.model.PatternConstraint) RangeConstraint(com.att.cdp.openstack.heat.model.RangeConstraint) Constraint(com.att.cdp.openstack.heat.model.Constraint) ValuesConstraint(com.att.cdp.openstack.heat.model.ValuesConstraint) CustomConstraint(com.att.cdp.openstack.heat.model.CustomConstraint) LengthConstraint(com.att.cdp.openstack.heat.model.LengthConstraint) ModelObject(com.att.cdp.openstack.heat.model.ModelObject) ArrayList(java.util.ArrayList) List(java.util.List) ModelObject(com.att.cdp.openstack.heat.model.ModelObject) UnmarshallException(com.att.cdp.openstack.exception.UnmarshallException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)5 JsonTypeInfo (com.fasterxml.jackson.annotation.JsonTypeInfo)2 UnmarshallException (com.att.cdp.openstack.exception.UnmarshallException)1 Constraint (com.att.cdp.openstack.heat.model.Constraint)1 CustomConstraint (com.att.cdp.openstack.heat.model.CustomConstraint)1 LengthConstraint (com.att.cdp.openstack.heat.model.LengthConstraint)1 ModelObject (com.att.cdp.openstack.heat.model.ModelObject)1 PatternConstraint (com.att.cdp.openstack.heat.model.PatternConstraint)1 RangeConstraint (com.att.cdp.openstack.heat.model.RangeConstraint)1 ValuesConstraint (com.att.cdp.openstack.heat.model.ValuesConstraint)1 JsonTypeName (com.fasterxml.jackson.annotation.JsonTypeName)1 JsonUnwrapped (com.fasterxml.jackson.annotation.JsonUnwrapped)1 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)1 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)1 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)1 JsonDeserialize (com.fasterxml.jackson.databind.annotation.JsonDeserialize)1 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)1 JsonObjectFormatVisitor (com.fasterxml.jackson.databind.jsonFormatVisitors.JsonObjectFormatVisitor)1