Search in sources :

Example 1 with TypeKey

use of io.micronaut.serde.support.util.TypeKey in project micronaut-serialization by micronaut-projects.

the class ObjectSerializer method getSerBean.

private Supplier<SerBean<Object>> getSerBean(Argument<? extends Object> type, Serializer.EncoderContext context) {
    TypeKey key = new TypeKey(type);
    Supplier<SerBean<Object>> serBeanSupplier = serBeanMap.get(key);
    if (serBeanSupplier == null) {
        serBeanSupplier = SupplierUtil.memoized(() -> create(type, context));
        serBeanMap.put(key, serBeanSupplier);
    }
    return serBeanSupplier;
}
Also used : TypeKey(io.micronaut.serde.support.util.TypeKey)

Example 2 with TypeKey

use of io.micronaut.serde.support.util.TypeKey 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 TypeKey

use of io.micronaut.serde.support.util.TypeKey in project micronaut-serialization by micronaut-projects.

the class DefaultSerdeRegistry method register.

private void register(SerdeRegistrar<?> serdeRegistrar) {
    for (Argument<?> type : serdeRegistrar.getTypes()) {
        final TypeKey typeEntry = new TypeKey(type);
        DefaultSerdeRegistry.this.deserializerMap.put(typeEntry, serdeRegistrar);
        DefaultSerdeRegistry.this.serializerMap.put(typeEntry, serdeRegistrar);
    }
}
Also used : TypeKey(io.micronaut.serde.support.util.TypeKey)

Example 4 with TypeKey

use of io.micronaut.serde.support.util.TypeKey in project micronaut-serialization by micronaut-projects.

the class ObjectDeserializer method getDeserializableBean.

@Override
public <T> DeserBean<T> getDeserializableBean(Argument<T> type, DecoderContext decoderContext) {
    TypeKey key = new TypeKey(type);
    DeserBean<T> deserBeanSupplier = (DeserBean) deserBeanMap.get(key);
    if (deserBeanSupplier == null) {
        deserBeanSupplier = createDeserBean(type, decoderContext);
        deserBeanMap.put(key, (DeserBean) deserBeanSupplier);
    }
    return deserBeanSupplier;
}
Also used : TypeKey(io.micronaut.serde.support.util.TypeKey)

Example 5 with TypeKey

use of io.micronaut.serde.support.util.TypeKey in project micronaut-serialization by micronaut-projects.

the class DefaultSerdeRegistry method registerPrimitiveSerdes.

private void registerPrimitiveSerdes() {
    this.deserializerMap.put(new TypeKey(Argument.BOOLEAN), (decoder, decoderContext, type) -> decoder.decodeBoolean());
    this.deserializerMap.put(new TypeKey(Argument.of(Boolean.class)), (NullableDeserializer<Boolean>) (decoder, decoderContext, type) -> decoder.decodeBoolean());
    this.deserializerMap.put(new TypeKey(Argument.CHAR), (decoder, decoderContext, type) -> decoder.decodeChar());
    this.deserializerMap.put(new TypeKey(Argument.of(Character.class)), (NullableDeserializer<Character>) (decoder, decoderContext, type) -> decoder.decodeChar());
}
Also used : OrderUtil(io.micronaut.core.order.OrderUtil) Arrays(java.util.Arrays) ObjectDeserializer(io.micronaut.serde.support.deserializers.ObjectDeserializer) BeanContext(io.micronaut.context.BeanContext) URL(java.net.URL) ArrayUtils(io.micronaut.core.util.ArrayUtils) SerdeRegistry(io.micronaut.serde.SerdeRegistry) BigDecimal(java.math.BigDecimal) Locale(java.util.Locale) Map(java.util.Map) SerdeIntrospections(io.micronaut.serde.SerdeIntrospections) BigInteger(java.math.BigInteger) URI(java.net.URI) TimeZone(java.util.TimeZone) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Singleton(jakarta.inject.Singleton) PropertyNamingStrategy(io.micronaut.serde.config.naming.PropertyNamingStrategy) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) StringUtils(io.micronaut.core.util.StringUtils) List(java.util.List) Stream(java.util.stream.Stream) Decoder(io.micronaut.serde.Decoder) BeanRegistration(io.micronaut.context.BeanRegistration) ObjectSerializer(io.micronaut.serde.support.serializers.ObjectSerializer) OptionalDouble(java.util.OptionalDouble) TypeKey(io.micronaut.serde.support.util.TypeKey) HashMap(java.util.HashMap) Encoder(io.micronaut.serde.Encoder) Deserializer(io.micronaut.serde.Deserializer) OptionalInt(java.util.OptionalInt) ArrayList(java.util.ArrayList) OptionalLong(java.util.OptionalLong) Serializer(io.micronaut.serde.Serializer) Charset(java.nio.charset.Charset) Nullable(io.micronaut.core.annotation.Nullable) Secondary(io.micronaut.context.annotation.Secondary) Argument(io.micronaut.core.type.Argument) NumberSerde(io.micronaut.serde.support.serdes.NumberSerde) ConfigurationException(io.micronaut.context.exceptions.ConfigurationException) Iterator(java.util.Iterator) NullableDeserializer(io.micronaut.serde.util.NullableDeserializer) IOException(java.io.IOException) ReflectionUtils(io.micronaut.core.reflect.ReflectionUtils) NonNull(io.micronaut.core.annotation.NonNull) Serde(io.micronaut.serde.Serde) CollectionUtils(io.micronaut.core.util.CollectionUtils) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) BeanDefinition(io.micronaut.inject.BeanDefinition) Collections(java.util.Collections) Order(io.micronaut.core.annotation.Order) SerdeException(io.micronaut.serde.exceptions.SerdeException) NullableSerde(io.micronaut.serde.util.NullableSerde) TypeKey(io.micronaut.serde.support.util.TypeKey)

Aggregations

TypeKey (io.micronaut.serde.support.util.TypeKey)7 ArrayList (java.util.ArrayList)4 List (java.util.List)4 BeanRegistration (io.micronaut.context.BeanRegistration)3 Argument (io.micronaut.core.type.Argument)3 BeanDefinition (io.micronaut.inject.BeanDefinition)3 Deserializer (io.micronaut.serde.Deserializer)3 Serializer (io.micronaut.serde.Serializer)3 SerdeException (io.micronaut.serde.exceptions.SerdeException)3 ObjectDeserializer (io.micronaut.serde.support.deserializers.ObjectDeserializer)3 ObjectSerializer (io.micronaut.serde.support.serializers.ObjectSerializer)3 NullableDeserializer (io.micronaut.serde.util.NullableDeserializer)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 BeanContext (io.micronaut.context.BeanContext)2 Secondary (io.micronaut.context.annotation.Secondary)2 ConfigurationException (io.micronaut.context.exceptions.ConfigurationException)2 NonNull (io.micronaut.core.annotation.NonNull)2 Nullable (io.micronaut.core.annotation.Nullable)2