Search in sources :

Example 1 with OneRelationSerializer

use of io.micronaut.data.document.serde.OneRelationSerializer in project micronaut-data by micronaut-projects.

the class DataSerdeRegistry method findCustomSerializer.

@Override
public <T, D extends Serializer<? extends T>> D findCustomSerializer(Class<? extends D> serializerClass) throws SerdeException {
    if (serializerClass == OneRelationSerializer.class) {
        OneRelationSerializer oneRelationSerializer = new OneRelationSerializer() {

            @Override
            public Serializer<Object> createSpecific(EncoderContext encoderContext, Argument<?> type) throws SerdeException {
                RuntimePersistentEntity entity = runtimeEntityRegistry.getEntity(type.getType());
                if (entity.getIdentity() == null) {
                    throw new SerdeException("Cannot find ID of entity type: " + type);
                }
                BeanProperty property = entity.getIdentity().getProperty();
                Argument<?> idType = entity.getIdentity().getArgument();
                Serializer<Object> idSerializer = encoderContext.findCustomSerializer(IdSerializer.class).createSpecific(encoderContext, idType);
                return new Serializer<Object>() {

                    @Override
                    public void serialize(Encoder encoder, EncoderContext context, Argument<?> type, Object value) throws IOException {
                        Object id = property.get(value);
                        if (id == null) {
                            encoder.encodeNull();
                        } else {
                            Encoder en = encoder.encodeObject(type);
                            en.encodeKey(MongoUtils.ID);
                            idSerializer.serialize(en, context, idType, id);
                            en.finishStructure();
                        }
                    }
                };
            }

            @Override
            public void serialize(Encoder encoder, EncoderContext context, Argument<? extends Object> type, Object value) throws IOException {
                throw new IllegalStateException("Create specific call is required!");
            }
        };
        return (D) oneRelationSerializer;
    }
    if (serializerClass == ManyRelationSerializer.class) {
        ManyRelationSerializer manyRelationSerializer = new ManyRelationSerializer() {

            @Override
            public void serialize(Encoder encoder, EncoderContext context, Argument<?> type, Object value) throws IOException {
                encoder.encodeNull();
            }
        };
        return (D) manyRelationSerializer;
    }
    return defaultSerdeRegistry.findCustomSerializer(serializerClass);
}
Also used : RuntimePersistentEntity(io.micronaut.data.model.runtime.RuntimePersistentEntity) Argument(io.micronaut.core.type.Argument) IdSerializer(io.micronaut.data.document.serde.IdSerializer) SerdeException(io.micronaut.serde.exceptions.SerdeException) OneRelationSerializer(io.micronaut.data.document.serde.OneRelationSerializer) BeanProperty(io.micronaut.core.beans.BeanProperty) ManyRelationSerializer(io.micronaut.data.document.serde.ManyRelationSerializer) Encoder(io.micronaut.serde.Encoder) Serializer(io.micronaut.serde.Serializer) IdSerializer(io.micronaut.data.document.serde.IdSerializer) OneRelationSerializer(io.micronaut.data.document.serde.OneRelationSerializer) ManyRelationSerializer(io.micronaut.data.document.serde.ManyRelationSerializer)

Aggregations

BeanProperty (io.micronaut.core.beans.BeanProperty)1 Argument (io.micronaut.core.type.Argument)1 IdSerializer (io.micronaut.data.document.serde.IdSerializer)1 ManyRelationSerializer (io.micronaut.data.document.serde.ManyRelationSerializer)1 OneRelationSerializer (io.micronaut.data.document.serde.OneRelationSerializer)1 RuntimePersistentEntity (io.micronaut.data.model.runtime.RuntimePersistentEntity)1 Encoder (io.micronaut.serde.Encoder)1 Serializer (io.micronaut.serde.Serializer)1 SerdeException (io.micronaut.serde.exceptions.SerdeException)1