Search in sources :

Example 26 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException 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

CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)26 LineString (com.mongodb.client.model.geojson.LineString)11 MultiLineString (com.mongodb.client.model.geojson.MultiLineString)11 NamedCoordinateReferenceSystem (com.mongodb.client.model.geojson.NamedCoordinateReferenceSystem)8 CoordinateReferenceSystem (com.mongodb.client.model.geojson.CoordinateReferenceSystem)7 Position (com.mongodb.client.model.geojson.Position)5 ArrayList (java.util.ArrayList)4 BsonDocument (org.bson.BsonDocument)4 Document (org.bson.Document)4 MultiPoint (com.mongodb.client.model.geojson.MultiPoint)3 MultiPolygon (com.mongodb.client.model.geojson.MultiPolygon)3 PolygonCoordinates (com.mongodb.client.model.geojson.PolygonCoordinates)3 BsonBinary (org.bson.BsonBinary)3 BsonReaderMark (org.bson.BsonReaderMark)3 BsonType (org.bson.BsonType)3 Test (org.junit.Test)3 GeometryCollection (com.mongodb.client.model.geojson.GeometryCollection)2 Point (com.mongodb.client.model.geojson.Point)2 Polygon (com.mongodb.client.model.geojson.Polygon)2 MappingException (dev.morphia.mapping.MappingException)2