io.micronaut.data.mongodb.serde.DataSerdeRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-data-mongodb Show documentation
Show all versions of micronaut-data-mongodb Show documentation
Data Repository Support for Micronaut
/*
* Copyright 2017-2022 original authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micronaut.data.mongodb.serde;
import io.micronaut.context.BeanContext;
import io.micronaut.core.annotation.Internal;
import io.micronaut.core.annotation.Order;
import io.micronaut.core.beans.BeanIntrospection;
import io.micronaut.core.beans.BeanProperty;
import io.micronaut.core.convert.ConversionService;
import io.micronaut.core.order.Ordered;
import io.micronaut.core.type.Argument;
import io.micronaut.data.document.serde.IdPropertyNamingStrategy;
import io.micronaut.data.document.serde.IdSerializer;
import io.micronaut.data.document.serde.ManyRelationSerializer;
import io.micronaut.data.document.serde.OneRelationSerializer;
import io.micronaut.data.model.runtime.AttributeConverterRegistry;
import io.micronaut.data.model.runtime.RuntimeEntityRegistry;
import io.micronaut.data.model.runtime.RuntimePersistentEntity;
import io.micronaut.data.mongodb.conf.MongoDataConfiguration;
import io.micronaut.data.mongodb.operations.MongoUtils;
import io.micronaut.serde.Deserializer;
import io.micronaut.serde.Encoder;
import io.micronaut.serde.SerdeRegistry;
import io.micronaut.serde.Serializer;
import io.micronaut.serde.config.naming.PropertyNamingStrategy;
import io.micronaut.serde.exceptions.SerdeException;
import io.micronaut.serde.support.DefaultSerdeRegistry;
import jakarta.inject.Singleton;
import org.bson.codecs.configuration.CodecRegistry;
import java.io.IOException;
import java.util.Collection;
/**
* Micronaut Data serde registry.
*
* @author Denis Stepanov
* @since 3.3
*/
@Singleton
@Order(Ordered.LOWEST_PRECEDENCE)
@Internal
final class DataSerdeRegistry implements SerdeRegistry {
public static final IdPropertyNamingStrategy ID_PROPERTY_NAMING_STRATEGY = element -> MongoUtils.ID;
private final DefaultSerdeRegistry defaultSerdeRegistry;
private final RuntimeEntityRegistry runtimeEntityRegistry;
private final AttributeConverterRegistry attributeConverterRegistry;
private final MongoDataConfiguration mongoDataConfiguration;
/**
* Default constructor.
*
* @param beanContext The bean context
* @param runtimeEntityRegistry The runtimeEntityRegistry
* @param attributeConverterRegistry The attributeConverterRegistry
* @param mongoDataConfiguration The Mongo configuration
*/
public DataSerdeRegistry(BeanContext beanContext,
RuntimeEntityRegistry runtimeEntityRegistry,
AttributeConverterRegistry attributeConverterRegistry,
MongoDataConfiguration mongoDataConfiguration) {
this.defaultSerdeRegistry = beanContext.createBean(DefaultSerdeRegistry.class);
this.runtimeEntityRegistry = runtimeEntityRegistry;
this.attributeConverterRegistry = attributeConverterRegistry;
this.mongoDataConfiguration = mongoDataConfiguration;
}
public Serializer.EncoderContext newEncoderContext(Class> view,
CodecRegistry codecRegistry) {
return new DataEncoderContext(mongoDataConfiguration, attributeConverterRegistry, newEncoderContext(view), codecRegistry);
}
public Deserializer.DecoderContext newDecoderContext(Class> view,
CodecRegistry codecRegistry) {
return new DataDecoderContext(mongoDataConfiguration, attributeConverterRegistry, newDecoderContext(view), codecRegistry);
}
@Override
public Serializer.EncoderContext newEncoderContext(Class> view) {
if (view != null) {
return new DefaultEncoderContext(this) {
@Override
public boolean hasView(Class>... views) {
for (Class> candidate : views) {
if (candidate.isAssignableFrom(view)) {
return true;
}
}
return false;
}
};
}
return new DefaultEncoderContext(this);
}
@Override
public Deserializer.DecoderContext newDecoderContext(Class> view) {
if (view != null) {
return new DefaultDecoderContext(this) {
@Override
public boolean hasView(Class>... views) {
for (Class> candidate : views) {
if (candidate.isAssignableFrom(view)) {
return true;
}
}
return false;
}
};
}
return new DefaultDecoderContext(this);
}
@Override
public > D findCustomSerializer(Class extends D> serializerClass) throws SerdeException {
if (serializerClass == OneRelationSerializer.class) {
OneRelationSerializer oneRelationSerializer = new OneRelationSerializer() {
@Override
public Serializer
© 2015 - 2025 Weber Informatics LLC | Privacy Policy