Search in sources :

Example 1 with MorphiaInstanceCreator

use of dev.morphia.mapping.codec.MorphiaInstanceCreator in project morphia by mongodb.

the class EntityDecoder method decode.

@Override
@SuppressWarnings("unchecked")
public T decode(BsonReader reader, DecoderContext decoderContext) {
    T entity;
    if (decoderContext.hasCheckedDiscriminator()) {
        MorphiaInstanceCreator instanceCreator = getInstanceCreator();
        decodeProperties(reader, decoderContext, instanceCreator, classModel);
        return (T) instanceCreator.getInstance();
    } else {
        entity = getCodecFromDocument(reader, classModel.useDiscriminator(), classModel.getDiscriminatorKey(), morphiaCodec.getRegistry(), morphiaCodec.getDiscriminatorLookup(), morphiaCodec).decode(reader, DecoderContext.builder().checkedDiscriminator(true).build());
    }
    return entity;
}
Also used : MorphiaInstanceCreator(dev.morphia.mapping.codec.MorphiaInstanceCreator)

Example 2 with MorphiaInstanceCreator

use of dev.morphia.mapping.codec.MorphiaInstanceCreator in project morphia by mongodb.

the class LifecycleDecoder method decode.

@Override
@SuppressWarnings("unchecked")
public T decode(BsonReader reader, DecoderContext decoderContext) {
    Document document = getMorphiaCodec().getRegistry().get(Document.class).decode(reader, decoderContext);
    EntityModel model = getMorphiaCodec().getEntityModel();
    if (model.useDiscriminator()) {
        String discriminator = document.getString(model.getDiscriminatorKey());
        if (discriminator != null) {
            Class<?> discriminatorClass = getMorphiaCodec().getDiscriminatorLookup().lookup(discriminator);
            // need to load the codec to initialize cachedCodecs in field models
            Codec<?> codec = getMorphiaCodec().getRegistry().get(discriminatorClass);
            if (codec instanceof MorphiaCodec) {
                model = ((MorphiaCodec<?>) codec).getEntityModel();
            } else {
                throw new CodecConfigurationException(format("Non-entity class used as discriminator: '%s'.", discriminator));
            }
        }
    }
    final MorphiaInstanceCreator instanceCreator = model.getInstanceCreator();
    T entity = (T) instanceCreator.getInstance();
    model.callLifecycleMethods(PreLoad.class, entity, document, getMorphiaCodec().getDatastore());
    decodeProperties(new DocumentReader(document), decoderContext, instanceCreator, model);
    model.callLifecycleMethods(PostLoad.class, entity, document, getMorphiaCodec().getDatastore());
    return entity;
}
Also used : CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) MorphiaInstanceCreator(dev.morphia.mapping.codec.MorphiaInstanceCreator) DocumentReader(dev.morphia.mapping.codec.reader.DocumentReader) Document(org.bson.Document)

Aggregations

MorphiaInstanceCreator (dev.morphia.mapping.codec.MorphiaInstanceCreator)2 DocumentReader (dev.morphia.mapping.codec.reader.DocumentReader)1 Document (org.bson.Document)1 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)1