Search in sources :

Example 1 with JsonTypeName

use of com.fasterxml.jackson.annotation.JsonTypeName 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 2 with JsonTypeName

use of com.fasterxml.jackson.annotation.JsonTypeName in project kork by spinnaker.

the class ObjectMapperSubtypeConfigurer method findSubtypes.

private NamedType[] findSubtypes(Class<?> clazz, String pkg) {
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
    provider.addIncludeFilter(new AssignableTypeFilter(clazz));
    return provider.findCandidateComponents(pkg).stream().map(bean -> {
        Class<?> cls = ClassUtils.resolveClassName(bean.getBeanClassName(), ClassUtils.getDefaultClassLoader());
        JsonTypeName nameAnnotation = cls.getAnnotation(JsonTypeName.class);
        if (nameAnnotation == null || "".equals(nameAnnotation.value())) {
            String message = "Subtype " + cls.getSimpleName() + " does not have a JsonTypeName annotation";
            if (strictSerialization) {
                throw new InvalidSubtypeConfigurationException(message);
            }
            log.warn(message);
            return null;
        }
        return new NamedType(cls, nameAnnotation.value());
    }).filter(Objects::nonNull).toArray(NamedType[]::new);
}
Also used : JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) ClassPathScanningCandidateComponentProvider(org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) AssignableTypeFilter(org.springframework.core.type.filter.AssignableTypeFilter)

Example 3 with JsonTypeName

use of com.fasterxml.jackson.annotation.JsonTypeName in project atlasmap by atlasmap.

the class SimpleResolver method init.

@Override
public void init(JavaType baseType) {
    this.baseType = baseType;
    Class<?> baseClass = baseType.getRawClass();
    ServiceLoader<?> implementations = ServiceLoader.load(baseClass, this.classLoader != null ? this.classLoader : baseClass.getClassLoader());
    for (Object o : implementations) {
        Class<?> c = o.getClass();
        if (c != baseClass && baseClass.isAssignableFrom(c)) {
            JsonTypeName jsonAnnoation = c.getAnnotation(JsonTypeName.class);
            // Use short ids for classes in the same package..
            String id = null;
            if (c.getPackage().getName().equals(baseClass.getPackage().getName())) {
                if (jsonAnnoation != null && jsonAnnoation.value() != null) {
                    id = jsonAnnoation.value();
                } else {
                    id = c.getSimpleName();
                }
            } else {
                // All other extensions need fully qualified names.
                id = c.getName();
            }
            typeToId.put(c, id);
            idToType.put(id, typeFactory.constructSpecializedType(baseType, c));
        }
    }
}
Also used : JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName)

Aggregations

JsonTypeName (com.fasterxml.jackson.annotation.JsonTypeName)3 JsonSubTypes (com.fasterxml.jackson.annotation.JsonSubTypes)1 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)1 ClassPathScanningCandidateComponentProvider (org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider)1 AssignableTypeFilter (org.springframework.core.type.filter.AssignableTypeFilter)1