Search in sources :

Example 1 with Deserializer

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

the class JacksonJsonMapper method readValue0.

@SuppressWarnings({ "rawtypes", "unchecked" })
private <T> T readValue0(JsonParser parser, Argument<?> type) throws IOException {
    parser.setCodec(objectCodecImpl);
    Deserializer deserializer = registry.findDeserializer(type).createSpecific(decoderContext, (Argument) type);
    if (!parser.hasCurrentToken()) {
        parser.nextToken();
    }
    // for jackson compat we need to support deserializing null, but most deserializers don't support it.
    if (parser.currentToken() == JsonToken.VALUE_NULL && !deserializer.allowNull()) {
        return null;
    }
    final Decoder decoder = JacksonDecoder.create(parser, view);
    return (T) deserializer.deserialize(decoder, decoderContext, type);
}
Also used : Deserializer(io.micronaut.serde.Deserializer) UpdatingDeserializer(io.micronaut.serde.UpdatingDeserializer) Decoder(io.micronaut.serde.Decoder)

Example 2 with Deserializer

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

the class DefaultSerdeRegistry method findDeserializer.

@Override
public <T> Deserializer<? extends T> findDeserializer(Argument<? extends T> type) {
    Objects.requireNonNull(type, "Type cannot be null");
    final TypeKey key = new TypeKey(type);
    final Deserializer<?> deserializer = deserializerMap.get(key);
    if (deserializer != null) {
        return (Deserializer<? extends T>) deserializer;
    } else {
        final Argument<Deserializer> deserializerArgument = Argument.of(Deserializer.class, type);
        final Collection<BeanRegistration<Deserializer>> beanRegistrations = beanContext.getBeanRegistrations(deserializerArgument, null);
        Deserializer<?> deser = null;
        if (beanRegistrations.size() == 1) {
            deser = beanRegistrations.iterator().next().bean();
        } else if (!beanRegistrations.isEmpty()) {
            final List<BeanRegistration<Deserializer>> results = beanRegistrations.stream().filter((r) -> {
                final Class<?>[] typeParameters = r.getBeanDefinition().getTypeParameters(Deserializer.class);
                return typeParameters.length == 1 && typeParameters[0].equals(type.getType());
            }).collect(Collectors.toList());
            if (results.size() == 1) {
                deser = results.iterator().next().bean();
            } else {
                deser = beanContext.findBean(deserializerArgument).orElse(null);
            }
        }
        if (deser != null) {
            deserializerMap.put(key, deser);
            return (Deserializer<? extends T>) deser;
        }
    }
    if (key.getType().isArray()) {
        deserializerMap.put(key, objectArraySerde);
        return (Deserializer<? extends T>) objectArraySerde;
    } else {
        deserializerMap.put(key, objectDeserializer);
        return (Deserializer<? extends T>) objectDeserializer;
    }
}
Also used : ObjectDeserializer(io.micronaut.serde.support.deserializers.ObjectDeserializer) Deserializer(io.micronaut.serde.Deserializer) NullableDeserializer(io.micronaut.serde.util.NullableDeserializer) BeanRegistration(io.micronaut.context.BeanRegistration) TypeKey(io.micronaut.serde.support.util.TypeKey) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with Deserializer

use of io.micronaut.serde.Deserializer 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 4 with Deserializer

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

the class JacksonJsonMapper method updateValueFromTree.

@Override
public void updateValueFromTree(Object value, JsonNode tree) throws IOException {
    if (tree != null && value != null) {
        Argument<Object> type = (Argument<Object>) Argument.of(value.getClass());
        Deserializer deserializer = registry.findDeserializer(type).createSpecific(decoderContext, type);
        if (deserializer instanceof UpdatingDeserializer) {
            try (JsonParser parser = treeCodec.treeAsTokens(tree)) {
                parser.setCodec(objectCodecImpl);
                if (!parser.hasCurrentToken()) {
                    parser.nextToken();
                }
                // for jackson compat we need to support deserializing null, but most deserializers don't support it.
                if (parser.currentToken() != JsonToken.VALUE_NULL) {
                    final Decoder decoder = JacksonDecoder.create(parser, view);
                    ((UpdatingDeserializer<Object>) deserializer).deserializeInto(decoder, decoderContext, type, value);
                }
            }
        }
    }
}
Also used : Argument(io.micronaut.core.type.Argument) Deserializer(io.micronaut.serde.Deserializer) UpdatingDeserializer(io.micronaut.serde.UpdatingDeserializer) UpdatingDeserializer(io.micronaut.serde.UpdatingDeserializer) Decoder(io.micronaut.serde.Decoder) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

Deserializer (io.micronaut.serde.Deserializer)4 Decoder (io.micronaut.serde.Decoder)3 Argument (io.micronaut.core.type.Argument)2 UpdatingDeserializer (io.micronaut.serde.UpdatingDeserializer)2 JsonParser (com.fasterxml.jackson.core.JsonParser)1 BeanRegistration (io.micronaut.context.BeanRegistration)1 Internal (io.micronaut.core.annotation.Internal)1 BeanIntrospection (io.micronaut.core.beans.BeanIntrospection)1 ConversionContext (io.micronaut.core.convert.ConversionContext)1 GeneratedValue (io.micronaut.data.annotation.GeneratedValue)1 MappedProperty (io.micronaut.data.annotation.MappedProperty)1 CustomConverterDeserializer (io.micronaut.data.document.serde.CustomConverterDeserializer)1 IdDeserializer (io.micronaut.data.document.serde.IdDeserializer)1 IdPropertyNamingStrategy (io.micronaut.data.document.serde.IdPropertyNamingStrategy)1 OneRelationDeserializer (io.micronaut.data.document.serde.OneRelationDeserializer)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 BsonReaderDecoder (io.micronaut.serde.bson.BsonReaderDecoder)1 CodecBsonDecoder (io.micronaut.serde.bson.custom.CodecBsonDecoder)1