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;
}
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;
}
}
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);
}
}
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;
}
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());
}
Aggregations