Search in sources :

Example 1 with CustomConverterSerializer

use of io.micronaut.data.document.serde.CustomConverterSerializer in project micronaut-data by micronaut-projects.

the class DataEncoderContext method findCustomSerializer.

@Override
public <T, D extends Serializer<? extends T>> D findCustomSerializer(Class<? extends D> serializerClass) throws SerdeException {
    if (serializerClass == IdSerializer.class) {
        IdSerializer idSerializer = new IdSerializer() {

            @Override
            public Serializer<Object> createSpecific(EncoderContext encoderContext, Argument<?> type) throws SerdeException {
                boolean isGeneratedObjectIdAsString = type.isAssignableFrom(String.class) && type.isAnnotationPresent(GeneratedValue.class);
                if (isGeneratedObjectIdAsString) {
                    Serializer<? super ObjectId> objectIdSerializer = findSerializer(OBJECT_ID);
                    return (encoder, encoderContext2, stringType, value) -> {
                        String stringId = (String) value;
                        objectIdSerializer.serialize(encoder, encoderContext2, OBJECT_ID, new ObjectId(stringId));
                    };
                }
                return (Serializer<Object>) findSerializer(type);
            }

            @Override
            public void serialize(Encoder encoder, EncoderContext context, Argument<?> type, Object value) {
                throw new IllegalStateException("Create specific call is required!");
            }
        };
        return (D) idSerializer;
    }
    if (serializerClass == CustomConverterSerializer.class) {
        CustomConverterSerializer customConverterSerializer = new CustomConverterSerializer() {

            @Override
            public Serializer<Object> createSpecific(EncoderContext encoderContext, Argument<?> type) throws SerdeException {
                Class<?> converterClass = type.getAnnotationMetadata().classValue(MappedProperty.class, "converter").orElseThrow(IllegalStateException::new);
                Class<Object> converterPersistedType = type.getAnnotationMetadata().classValue(MappedProperty.class, "converterPersistedType").orElseThrow(IllegalStateException::new);
                Argument<Object> convertedType = Argument.of(converterPersistedType);
                Serializer<? super Object> serializer = findSerializer(convertedType);
                AttributeConverter<Object, Object> converter = attributeConverterRegistry.getConverter(converterClass);
                return new Serializer<Object>() {

                    @Override
                    public void serialize(Encoder encoder, EncoderContext context, Argument<?> type, Object value) throws IOException {
                        if (value == null) {
                            encoder.encodeNull();
                            return;
                        }
                        Object converted = converter.convertToPersistedValue(value, ConversionContext.of(type));
                        if (converted == null) {
                            encoder.encodeNull();
                            return;
                        }
                        serializer.serialize(encoder, context, convertedType, converted);
                    }
                };
            }

            @Override
            public void serialize(Encoder encoder, EncoderContext context, Argument<?> type, Object value) {
                throw new IllegalStateException("Create specific call is required!");
            }
        };
        return (D) customConverterSerializer;
    }
    return parent.findCustomSerializer(serializerClass);
}
Also used : IdPropertyNamingStrategy(io.micronaut.data.document.serde.IdPropertyNamingStrategy) PropertyReference(io.micronaut.serde.reference.PropertyReference) PropertyNamingStrategy(io.micronaut.serde.config.naming.PropertyNamingStrategy) IOException(java.io.IOException) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Internal(io.micronaut.core.annotation.Internal) Encoder(io.micronaut.serde.Encoder) CustomConverterSerializer(io.micronaut.data.document.serde.CustomConverterSerializer) SerializationReference(io.micronaut.serde.reference.SerializationReference) ConversionContext(io.micronaut.core.convert.ConversionContext) Serializer(io.micronaut.serde.Serializer) MappedProperty(io.micronaut.data.annotation.MappedProperty) CodecBsonDecoder(io.micronaut.serde.bson.custom.CodecBsonDecoder) GeneratedValue(io.micronaut.data.annotation.GeneratedValue) IdSerializer(io.micronaut.data.document.serde.IdSerializer) Codec(org.bson.codecs.Codec) ObjectId(org.bson.types.ObjectId) IterableCodec(org.bson.codecs.IterableCodec) Argument(io.micronaut.core.type.Argument) RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) ConversionService(io.micronaut.core.convert.ConversionService) AttributeConverterRegistry(io.micronaut.data.model.runtime.AttributeConverterRegistry) AttributeConverter(io.micronaut.data.model.runtime.convert.AttributeConverter) SerdeException(io.micronaut.serde.exceptions.SerdeException) Argument(io.micronaut.core.type.Argument) ObjectId(org.bson.types.ObjectId) IdSerializer(io.micronaut.data.document.serde.IdSerializer) GeneratedValue(io.micronaut.data.annotation.GeneratedValue) Encoder(io.micronaut.serde.Encoder) MappedProperty(io.micronaut.data.annotation.MappedProperty) CustomConverterSerializer(io.micronaut.data.document.serde.CustomConverterSerializer) CustomConverterSerializer(io.micronaut.data.document.serde.CustomConverterSerializer) Serializer(io.micronaut.serde.Serializer) IdSerializer(io.micronaut.data.document.serde.IdSerializer)

Aggregations

Internal (io.micronaut.core.annotation.Internal)1 ConversionContext (io.micronaut.core.convert.ConversionContext)1 ConversionService (io.micronaut.core.convert.ConversionService)1 Argument (io.micronaut.core.type.Argument)1 GeneratedValue (io.micronaut.data.annotation.GeneratedValue)1 MappedProperty (io.micronaut.data.annotation.MappedProperty)1 CustomConverterSerializer (io.micronaut.data.document.serde.CustomConverterSerializer)1 IdPropertyNamingStrategy (io.micronaut.data.document.serde.IdPropertyNamingStrategy)1 IdSerializer (io.micronaut.data.document.serde.IdSerializer)1 AttributeConverterRegistry (io.micronaut.data.model.runtime.AttributeConverterRegistry)1 RuntimePersistentEntity (io.micronaut.data.model.runtime.RuntimePersistentEntity)1 AttributeConverter (io.micronaut.data.model.runtime.convert.AttributeConverter)1 Encoder (io.micronaut.serde.Encoder)1 Serializer (io.micronaut.serde.Serializer)1 CodecBsonDecoder (io.micronaut.serde.bson.custom.CodecBsonDecoder)1 PropertyNamingStrategy (io.micronaut.serde.config.naming.PropertyNamingStrategy)1 SerdeException (io.micronaut.serde.exceptions.SerdeException)1 PropertyReference (io.micronaut.serde.reference.PropertyReference)1 SerializationReference (io.micronaut.serde.reference.SerializationReference)1 IOException (java.io.IOException)1