Search in sources :

Example 1 with ObjectDeserializer

use of io.micronaut.serde.support.deserializers.ObjectDeserializer 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)

Aggregations

BeanRegistration (io.micronaut.context.BeanRegistration)1 Deserializer (io.micronaut.serde.Deserializer)1 ObjectDeserializer (io.micronaut.serde.support.deserializers.ObjectDeserializer)1 TypeKey (io.micronaut.serde.support.util.TypeKey)1 NullableDeserializer (io.micronaut.serde.util.NullableDeserializer)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1