Search in sources :

Example 6 with TypeKey

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

the class DefaultSerdeRegistry method registerBuiltInSerdes.

private void registerBuiltInSerdes() {
    this.deserializerMap.put(new TypeKey(Argument.STRING), (NullableDeserializer<String>) (decoder, decoderContext, type) -> decoder.decodeString());
    Stream.of(new IntegerSerde(), new LongSerde(), new ShortSerde(), new FloatSerde(), new ByteSerde(), new DoubleSerde(), new OptionalIntSerde(), new OptionalDoubleSerde(), new OptionalLongSerde(), new BigDecimalSerde(), new BigIntegerSerde(), new UUIDSerde(), new URLSerde(), new URISerde(), new CharsetSerde(), new TimeZoneSerde(), new LocaleSerde(), new IntArraySerde(), new LongArraySerde(), new FloatArraySerde(), new ShortArraySerde(), new DoubleArraySerde(), new BooleanArraySerde(), new ByteArraySerde(), new CharArraySerde()).forEach(this::register);
}
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)

Example 7 with TypeKey

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

the class DefaultSerdeRegistry method findSerializer.

@Override
public <T> Serializer<? super T> findSerializer(Argument<? extends T> type) throws SerdeException {
    Objects.requireNonNull(type, "Type cannot be null");
    final TypeKey key = new TypeKey(type);
    final Serializer<?> serializer = serializerMap.get(key);
    if (serializer != null) {
        // noinspection unchecked
        return (Serializer<? super T>) serializer;
    } else {
        List<BeanDefinition<Serializer>> possibles = serializerDefMap.get(type.getType());
        if (possibles == null) {
            for (Map.Entry<Class<?>, List<BeanDefinition<Serializer>>> entry : serializerDefMap.entrySet()) {
                final Class<?> targetType = entry.getKey();
                if (targetType.isAssignableFrom(type.getType())) {
                    possibles = entry.getValue();
                    final Argument<?>[] params = type.getTypeParameters();
                    if (ArrayUtils.isNotEmpty(params)) {
                        // narrow for generics
                        possibles = new ArrayList<>(possibles);
                        final Iterator<BeanDefinition<Serializer>> i = possibles.iterator();
                        while (i.hasNext()) {
                            final BeanDefinition<Serializer> bd = i.next();
                            final Argument<?>[] candidateParams = bd.getTypeArguments(Serializer.class).get(0).getTypeParameters();
                            if (candidateParams.length == params.length) {
                                for (int j = 0; j < params.length; j++) {
                                    Argument<?> param = params[j];
                                    final Argument<?> candidateParam = candidateParams[j];
                                    if (!((param.getType() == candidateParam.getType()) || (candidateParam.isTypeVariable() && candidateParam.getType().isAssignableFrom(param.getType())))) {
                                        i.remove();
                                    }
                                }
                            } else {
                                i.remove();
                            }
                        }
                    }
                    break;
                }
            }
        }
        if (possibles != null) {
            if (possibles.size() == 1) {
                final BeanDefinition<Serializer> definition = possibles.iterator().next();
                final Serializer locatedSerializer = beanContext.getBean(definition);
                serializerMap.put(key, locatedSerializer);
                return locatedSerializer;
            } else if (possibles.isEmpty()) {
                throw new SerdeException("No serializers found for type: " + type);
            } else {
                final BeanDefinition<Serializer> definition = lastChanceResolve(type, possibles);
                final Serializer locatedSerializer = beanContext.getBean(definition);
                serializerMap.put(key, locatedSerializer);
                return locatedSerializer;
            }
        } else {
            serializerMap.put(key, objectSerializer);
        }
    }
    return objectSerializer;
}
Also used : Argument(io.micronaut.core.type.Argument) SerdeException(io.micronaut.serde.exceptions.SerdeException) BeanDefinition(io.micronaut.inject.BeanDefinition) TypeKey(io.micronaut.serde.support.util.TypeKey) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ObjectSerializer(io.micronaut.serde.support.serializers.ObjectSerializer) Serializer(io.micronaut.serde.Serializer)

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