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());
}
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.");
}
}
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.");
}
}
Aggregations