Search in sources :

Example 1 with SerdeConfig

use of io.micronaut.serde.config.annotation.SerdeConfig in project micronaut-data by micronaut-projects.

the class MappedPropertyMapper method map.

@Override
public List<AnnotationValue<?>> map(AnnotationValue<MappedProperty> annotation, VisitorContext visitorContext) {
    AnnotationValueBuilder<SerdeConfig> builder = AnnotationValue.builder(SerdeConfig.class);
    annotation.stringValue().ifPresent(property -> {
        builder.member(SerdeConfig.PROPERTY, property);
    });
    annotation.stringValue("converter").ifPresent(val -> {
        visitorContext.getClassElement(val).ifPresent(attributeConverterClassElement -> {
            ClassElement genericType = attributeConverterClassElement.getGenericType();
            Map<String, ClassElement> typeArguments = genericType.getTypeArguments(AttributeConverter.class.getName());
            if (typeArguments.isEmpty()) {
                typeArguments = genericType.getTypeArguments("javax.persistence.AttributeConverter");
            }
            if (typeArguments.isEmpty()) {
                typeArguments = genericType.getTypeArguments("jakarta.persistence.AttributeConverter");
            }
            ClassElement converterPersistedType = typeArguments.get("Y");
            if (converterPersistedType != null) {
                builder.member(SerdeConfig.SERIALIZE_AS, new AnnotationClassValue<Object>(converterPersistedType.getName()));
                builder.member(SerdeConfig.DESERIALIZE_AS, new AnnotationClassValue<Object>(converterPersistedType.getName()));
            }
        });
        builder.member(SerdeConfig.SERIALIZER_CLASS, CustomConverterSerializer.class);
        builder.member(SerdeConfig.DESERIALIZER_CLASS, CustomConverterDeserializer.class);
    });
    return Collections.singletonList(builder.build());
}
Also used : AttributeConverter(io.micronaut.data.model.runtime.convert.AttributeConverter) ClassElement(io.micronaut.inject.ast.ClassElement) SerdeConfig(io.micronaut.serde.config.annotation.SerdeConfig)

Example 2 with SerdeConfig

use of io.micronaut.serde.config.annotation.SerdeConfig in project micronaut-serialization by micronaut-projects.

the class DefaultSerdeIntrospections method getDeserializableIntrospection.

@Override
public <T> BeanIntrospection<T> getDeserializableIntrospection(Argument<T> type) {
    final Class<T> rawType = type.getType();
    final BeanIntrospector beanIntrospector = getBeanIntrospector();
    final BeanIntrospection<T> introspection = beanIntrospector.findIntrospection(rawType).orElseGet(() -> {
        final Serdeable.Deserializable ann = rawType.getAnnotation(Serdeable.Deserializable.class);
        if (ann != null) {
            @SuppressWarnings("unchecked") final Class<T> as = (Class<T>) ann.as();
            if (as != void.class) {
                return beanIntrospector.getIntrospection(as);
            }
        }
        // rewthrow original
        return beanIntrospector.getIntrospection(rawType);
    });
    if (isEnabledForDeserialization(introspection, type)) {
        final AnnotationMetadata declaredMetadata = introspection.getDeclaredMetadata();
        final AnnotationValue<SerdeConfig> serdeConfig = declaredMetadata.getDeclaredAnnotation(SerdeConfig.class);
        Class<?> deserializeType = resolveDeserAsType(introspection.getBeanType(), serdeConfig, SerdeConfig.DESERIALIZE_AS);
        if (deserializeType != null) {
            Argument resolved = Argument.of(deserializeType, type.getName(), type.getAnnotationMetadata(), type.getTypeParameters());
            return getDeserializableIntrospection(resolved);
        } else {
            return introspection;
        }
    } else {
        throw new IntrospectionException("No deserializable introspection present for type: " + type + ". Consider adding Serdeable.Deserializable annotate to type " + type + ". Alternatively if you are not in control of the project's source code, you can use @SerdeImport(" + type.getSimpleName() + ".class) to enable deserialization of this type.");
    }
}
Also used : Argument(io.micronaut.core.type.Argument) IntrospectionException(io.micronaut.core.beans.exceptions.IntrospectionException) SerdeConfig(io.micronaut.serde.config.annotation.SerdeConfig) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Serdeable(io.micronaut.serde.annotation.Serdeable) BeanIntrospector(io.micronaut.core.beans.BeanIntrospector)

Example 3 with SerdeConfig

use of io.micronaut.serde.config.annotation.SerdeConfig in project micronaut-serialization by micronaut-projects.

the class DefaultSerdeIntrospections method getSerializableIntrospection.

@Override
public <T> BeanIntrospection<T> getSerializableIntrospection(Argument<T> type) {
    final BeanIntrospector beanIntrospector = getBeanIntrospector();
    final Optional<BeanIntrospection<T>> introspection = beanIntrospector.findIntrospection(type.getType());
    BeanIntrospection<T> result = null;
    if (introspection.isPresent()) {
        final BeanIntrospection<T> i = introspection.get();
        if (isEnabledForSerialization(i, type)) {
            result = i;
        }
    }
    if (result == null) {
        final Collection<BeanIntrospection<Object>> candidates = beanIntrospector.findIntrospections(reference -> reference.isPresent() && reference.getBeanType().isAssignableFrom(type.getType()) && isEnabledForSerialization(reference, type));
        if (CollectionUtils.isNotEmpty(candidates)) {
            if (candidates.size() == 1) {
                result = (BeanIntrospection<T>) candidates.iterator().next();
            } else {
                result = (BeanIntrospection<T>) OrderUtil.sort(candidates.stream()).findFirst().orElse(null);
            }
        }
    }
    if (result != null) {
        final AnnotationMetadata declaredMetadata = result.getDeclaredMetadata();
        final AnnotationValue<SerdeConfig> serdeConfig = declaredMetadata.getDeclaredAnnotation(SerdeConfig.class);
        final Class<T> beanType = type.getType();
        Class<?> serializeType = resolveDeserAsType(beanType, serdeConfig, SerdeConfig.SERIALIZE_AS);
        if (serializeType != null && !serializeType.equals(beanType)) {
            Argument resolved = Argument.of(serializeType, type.getName(), type.getAnnotationMetadata(), type.getTypeParameters());
            return getSerializableIntrospection(resolved);
        } else {
            return result;
        }
    } else {
        throw new IntrospectionException("No serializable introspection present for type " + type + ". Consider adding Serdeable. Serializable annotate to type " + type + ". Alternatively if you are not in control of the project's source code, you can use @SerdeImport(" + type.getSimpleName() + ".class) to enable serialization of this type.");
    }
}
Also used : Argument(io.micronaut.core.type.Argument) IntrospectionException(io.micronaut.core.beans.exceptions.IntrospectionException) SerdeConfig(io.micronaut.serde.config.annotation.SerdeConfig) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) BeanIntrospector(io.micronaut.core.beans.BeanIntrospector)

Aggregations

SerdeConfig (io.micronaut.serde.config.annotation.SerdeConfig)3 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)2 BeanIntrospector (io.micronaut.core.beans.BeanIntrospector)2 IntrospectionException (io.micronaut.core.beans.exceptions.IntrospectionException)2 Argument (io.micronaut.core.type.Argument)2 BeanIntrospection (io.micronaut.core.beans.BeanIntrospection)1 AttributeConverter (io.micronaut.data.model.runtime.convert.AttributeConverter)1 ClassElement (io.micronaut.inject.ast.ClassElement)1 Serdeable (io.micronaut.serde.annotation.Serdeable)1