Search in sources :

Example 6 with SerdeException

use of io.micronaut.serde.exceptions.SerdeException in project micronaut-serialization by micronaut-projects.

the class ObjectDeserializer method createDeserBean.

private <T> DeserBean<T> createDeserBean(Argument<T> type, DecoderContext decoderContext) {
    try {
        final BeanIntrospection<T> deserializableIntrospection = introspections.getDeserializableIntrospection(type);
        AnnotationMetadata annotationMetadata = new AnnotationMetadataHierarchy(type.getAnnotationMetadata(), deserializableIntrospection.getAnnotationMetadata());
        if (annotationMetadata.hasAnnotation(SerdeConfig.SerSubtyped.class)) {
            if (type.hasTypeVariables()) {
                final Map<String, Argument<?>> bounds = type.getTypeVariables();
                return new SubtypedDeserBean(annotationMetadata, deserializableIntrospection, decoderContext, this) {

                    @Override
                    protected Map<String, Argument<?>> getBounds() {
                        return bounds;
                    }
                };
            } else {
                return new SubtypedDeserBean<>(annotationMetadata, deserializableIntrospection, decoderContext, this);
            }
        } else {
            if (type.hasTypeVariables()) {
                final Map<String, Argument<?>> bounds = type.getTypeVariables();
                return new DeserBean(deserializableIntrospection, decoderContext, this) {

                    @Override
                    protected Map<String, Argument<?>> getBounds() {
                        return bounds;
                    }
                };
            } else {
                return new DeserBean<>(deserializableIntrospection, decoderContext, this);
            }
        }
    } catch (SerdeException e) {
        throw new IntrospectionException("Error creating deserializer for type [" + type + "]: " + e.getMessage(), e);
    }
}
Also used : Argument(io.micronaut.core.type.Argument) SerdeException(io.micronaut.serde.exceptions.SerdeException) IntrospectionException(io.micronaut.core.beans.exceptions.IntrospectionException) SerdeConfig(io.micronaut.serde.config.annotation.SerdeConfig) AnnotationMetadataHierarchy(io.micronaut.inject.annotation.AnnotationMetadataHierarchy) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata)

Example 7 with SerdeException

use of io.micronaut.serde.exceptions.SerdeException in project micronaut-serialization by micronaut-projects.

the class SimpleObjectDeserializer method deserialize.

@Override
public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argument<? super Object> beanType) throws IOException {
    if (decoder.decodeNull()) {
        return null;
    }
    deserBean.initialize(decoderContext);
    Object obj;
    try {
        obj = deserBean.introspection.instantiate();
    } catch (InstantiationException e) {
        throw new SerdeException("Unable to deserialize type [" + beanType + "]: " + e.getMessage(), e);
    }
    readProperties(decoder, decoderContext, beanType, obj);
    return obj;
}
Also used : SerdeException(io.micronaut.serde.exceptions.SerdeException) InstantiationException(io.micronaut.core.reflect.exception.InstantiationException)

Example 8 with SerdeException

use of io.micronaut.serde.exceptions.SerdeException in project micronaut-serialization by micronaut-projects.

the class FormattedNumberSerde method createDecimalFormat.

private DecimalFormat createDecimalFormat(Argument<?> type) throws SerdeException {
    final DecimalFormat decimalFormat;
    try {
        if (locale != null) {
            decimalFormat = (DecimalFormat) NumberFormat.getInstance(locale);
            decimalFormat.applyPattern(pattern);
        } else {
            decimalFormat = new DecimalFormat(pattern);
        }
    } catch (Exception e) {
        throw new SerdeException("Error decoding number of type " + type + ", pattern is invalid " + pattern + ":" + e.getMessage(), e);
    }
    return decimalFormat;
}
Also used : DecimalFormat(java.text.DecimalFormat) SerdeException(io.micronaut.serde.exceptions.SerdeException) IOException(java.io.IOException) SerdeException(io.micronaut.serde.exceptions.SerdeException)

Example 9 with SerdeException

use of io.micronaut.serde.exceptions.SerdeException in project micronaut-data by micronaut-projects.

the class DataDecoderContext method findCustomDeserializer.

@Override
public <T, D extends Deserializer<? extends T>> D findCustomDeserializer(Class<? extends D> deserializerClass) throws SerdeException {
    if (deserializerClass == OneRelationDeserializer.class) {
        OneRelationDeserializer oneRelationDeserializer = new OneRelationDeserializer() {

            @Override
            public Deserializer<Object> createSpecific(DecoderContext decoderContext, Argument<? super Object> type) throws SerdeException {
                Deserializer<?> relationDeser = findDeserializer(type);
                return new Deserializer<Object>() {

                    @Override
                    public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argument<? super Object> type) throws IOException {
                        if (decoder.decodeNull()) {
                            return null;
                        }
                        CodecBsonDecoder<BsonDocument> codecBsonDecoder = new CodecBsonDecoder<>(new BsonDocumentCodec(codecRegistry));
                        BsonDocument document = codecBsonDecoder.deserialize(decoder, decoderContext, type);
                        if (document == null || document.size() <= 1) {
                            return null;
                        }
                        return relationDeser.deserialize(new BsonReaderDecoder(document.asBsonReader()), decoderContext, type);
                    }
                };
            }

            @Override
            public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argument<? super Object> type) throws IOException {
                throw new IllegalStateException("Create specific call is required!");
            }
        };
        return (D) oneRelationDeserializer;
    }
    if (deserializerClass == IdDeserializer.class) {
        IdDeserializer idDeserializer = new IdDeserializer() {

            @Override
            public Deserializer<Object> createSpecific(DecoderContext decoderContext, Argument<? super Object> type) throws SerdeException {
                if (type.getType().isAssignableFrom(String.class) && type.isAnnotationPresent(GeneratedValue.class)) {
                    Deserializer<? extends ObjectId> deserializer = findDeserializer(OBJECT_ID);
                    return (decoder, decoderContext2, objectIdType) -> {
                        ObjectId objectId = deserializer.deserialize(decoder, decoderContext2, OBJECT_ID);
                        return objectId == null ? null : objectId.toHexString();
                    };
                }
                return (Deserializer<Object>) findDeserializer(type);
            }

            @Override
            public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argument<? super Object> type) throws IOException {
                throw new IllegalStateException("Create specific call is required!");
            }
        };
        return (D) idDeserializer;
    }
    if (deserializerClass == CustomConverterDeserializer.class) {
        CustomConverterDeserializer customConverterDeserializer = new CustomConverterDeserializer() {

            @Override
            public Deserializer<Object> createSpecific(DecoderContext decoderContext, Argument<? super Object> 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);
                AttributeConverter<Object, Object> converter = attributeConverterRegistry.getConverter(converterClass);
                Deserializer<?> deserializer = findDeserializer(convertedType);
                return new Deserializer<Object>() {

                    @Override
                    public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argument<? super Object> type) throws IOException {
                        if (decoder.decodeNull()) {
                            return null;
                        }
                        Object deserialized = deserializer.deserialize(decoder, decoderContext, convertedType);
                        return converter.convertToEntityValue(deserialized, ConversionContext.of(convertedType));
                    }
                };
            }

            @Override
            public Object deserialize(Decoder decoder, DecoderContext decoderContext, Argument<? super Object> type) throws IOException {
                throw new IllegalStateException("Create specific call is required!");
            }
        };
        return (D) customConverterDeserializer;
    }
    return parent.findCustomDeserializer(deserializerClass);
}
Also used : IdPropertyNamingStrategy(io.micronaut.data.document.serde.IdPropertyNamingStrategy) PropertyReference(io.micronaut.serde.reference.PropertyReference) CustomConverterDeserializer(io.micronaut.data.document.serde.CustomConverterDeserializer) CodecRegistry(org.bson.codecs.configuration.CodecRegistry) Internal(io.micronaut.core.annotation.Internal) Deserializer(io.micronaut.serde.Deserializer) BsonDocument(org.bson.BsonDocument) MappedProperty(io.micronaut.data.annotation.MappedProperty) BsonReaderDecoder(io.micronaut.serde.bson.BsonReaderDecoder) GeneratedValue(io.micronaut.data.annotation.GeneratedValue) IterableCodec(org.bson.codecs.IterableCodec) Argument(io.micronaut.core.type.Argument) AttributeConverterRegistry(io.micronaut.data.model.runtime.AttributeConverterRegistry) OneRelationDeserializer(io.micronaut.data.document.serde.OneRelationDeserializer) Collection(java.util.Collection) PropertyNamingStrategy(io.micronaut.serde.config.naming.PropertyNamingStrategy) IOException(java.io.IOException) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) IdDeserializer(io.micronaut.data.document.serde.IdDeserializer) ConversionContext(io.micronaut.core.convert.ConversionContext) CodecBsonDecoder(io.micronaut.serde.bson.custom.CodecBsonDecoder) Decoder(io.micronaut.serde.Decoder) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) Codec(org.bson.codecs.Codec) ObjectId(org.bson.types.ObjectId) RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) AttributeConverter(io.micronaut.data.model.runtime.convert.AttributeConverter) SerdeException(io.micronaut.serde.exceptions.SerdeException) Argument(io.micronaut.core.type.Argument) BsonReaderDecoder(io.micronaut.serde.bson.BsonReaderDecoder) BsonReaderDecoder(io.micronaut.serde.bson.BsonReaderDecoder) CodecBsonDecoder(io.micronaut.serde.bson.custom.CodecBsonDecoder) Decoder(io.micronaut.serde.Decoder) GeneratedValue(io.micronaut.data.annotation.GeneratedValue) CustomConverterDeserializer(io.micronaut.data.document.serde.CustomConverterDeserializer) IdDeserializer(io.micronaut.data.document.serde.IdDeserializer) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) ObjectId(org.bson.types.ObjectId) BsonDocument(org.bson.BsonDocument) CustomConverterDeserializer(io.micronaut.data.document.serde.CustomConverterDeserializer) Deserializer(io.micronaut.serde.Deserializer) OneRelationDeserializer(io.micronaut.data.document.serde.OneRelationDeserializer) IdDeserializer(io.micronaut.data.document.serde.IdDeserializer) MappedProperty(io.micronaut.data.annotation.MappedProperty) OneRelationDeserializer(io.micronaut.data.document.serde.OneRelationDeserializer) CodecBsonDecoder(io.micronaut.serde.bson.custom.CodecBsonDecoder)

Example 10 with SerdeException

use of io.micronaut.serde.exceptions.SerdeException in project micronaut-data by micronaut-projects.

the class DataSerdeRegistry method findCustomSerializer.

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

            @Override
            public Serializer<Object> createSpecific(EncoderContext encoderContext, Argument<?> type) throws SerdeException {
                RuntimePersistentEntity entity = runtimeEntityRegistry.getEntity(type.getType());
                if (entity.getIdentity() == null) {
                    throw new SerdeException("Cannot find ID of entity type: " + type);
                }
                BeanProperty property = entity.getIdentity().getProperty();
                Argument<?> idType = entity.getIdentity().getArgument();
                Serializer<Object> idSerializer = encoderContext.findCustomSerializer(IdSerializer.class).createSpecific(encoderContext, idType);
                return new Serializer<Object>() {

                    @Override
                    public void serialize(Encoder encoder, EncoderContext context, Argument<?> type, Object value) throws IOException {
                        Object id = property.get(value);
                        if (id == null) {
                            encoder.encodeNull();
                        } else {
                            Encoder en = encoder.encodeObject(type);
                            en.encodeKey(MongoUtils.ID);
                            idSerializer.serialize(en, context, idType, id);
                            en.finishStructure();
                        }
                    }
                };
            }

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

            @Override
            public void serialize(Encoder encoder, EncoderContext context, Argument<?> type, Object value) throws IOException {
                encoder.encodeNull();
            }
        };
        return (D) manyRelationSerializer;
    }
    return defaultSerdeRegistry.findCustomSerializer(serializerClass);
}
Also used : RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) Argument(io.micronaut.core.type.Argument) IdSerializer(io.micronaut.data.document.serde.IdSerializer) SerdeException(io.micronaut.serde.exceptions.SerdeException) OneRelationSerializer(io.micronaut.data.document.serde.OneRelationSerializer) BeanProperty(io.micronaut.core.beans.BeanProperty) ManyRelationSerializer(io.micronaut.data.document.serde.ManyRelationSerializer) Encoder(io.micronaut.serde.Encoder) Serializer(io.micronaut.serde.Serializer) IdSerializer(io.micronaut.data.document.serde.IdSerializer) OneRelationSerializer(io.micronaut.data.document.serde.OneRelationSerializer) ManyRelationSerializer(io.micronaut.data.document.serde.ManyRelationSerializer)

Aggregations

SerdeException (io.micronaut.serde.exceptions.SerdeException)20 Argument (io.micronaut.core.type.Argument)12 Encoder (io.micronaut.serde.Encoder)9 Serializer (io.micronaut.serde.Serializer)9 CustomizableSerializer (io.micronaut.serde.util.CustomizableSerializer)5 IOException (java.io.IOException)5 IntrospectionException (io.micronaut.core.beans.exceptions.IntrospectionException)4 Internal (io.micronaut.core.annotation.Internal)3 RuntimePersistentEntity (io.micronaut.data.model.runtime.RuntimePersistentEntity)3 Decoder (io.micronaut.serde.Decoder)3 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)2 ConversionContext (io.micronaut.core.convert.ConversionContext)2 InstantiationException (io.micronaut.core.reflect.exception.InstantiationException)2 GeneratedValue (io.micronaut.data.annotation.GeneratedValue)2 MappedProperty (io.micronaut.data.annotation.MappedProperty)2 IdPropertyNamingStrategy (io.micronaut.data.document.serde.IdPropertyNamingStrategy)2 IdSerializer (io.micronaut.data.document.serde.IdSerializer)2 AttributeConverterRegistry (io.micronaut.data.model.runtime.AttributeConverterRegistry)2 AttributeConverter (io.micronaut.data.model.runtime.convert.AttributeConverter)2 CodecBsonDecoder (io.micronaut.serde.bson.custom.CodecBsonDecoder)2