Search in sources :

Example 11 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.

the class AbstractUuidRepresentationTest method shouldEncodeDbObjectWithUuidRepresentation.

@Test
public void shouldEncodeDbObjectWithUuidRepresentation() {
    if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
        try {
            dbObjectCollection.insertOne(new BasicDBObject("_id", uuid));
            fail();
        } catch (CodecConfigurationException e) {
        // all good
        }
    } else {
        dbObjectCollection.insertOne(new BasicDBObject("_id", uuid));
        BsonDocument document = bsonDocumentCollection.find().first();
        assertNotNull(document);
        BsonBinary uuidAsBinary = document.getBinary("_id");
        assertEquals(subType.getValue(), uuidAsBinary.getType());
        assertEquals(subType.getValue(), uuidAsBinary.getType());
        assertArrayEquals(encodedValue, uuidAsBinary.getData());
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) Test(org.junit.Test)

Example 12 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.

the class AbstractUuidRepresentationTest method shouldEncodePojoWithUuidRepresentation.

@Test
public void shouldEncodePojoWithUuidRepresentation() {
    if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
        try {
            uuidIdPojoCollection.insertOne(new UuidIdPojo(uuid));
            fail();
        } catch (CodecConfigurationException e) {
        // all good
        }
    } else {
        uuidIdPojoCollection.insertOne(new UuidIdPojo(uuid));
        BsonDocument document = bsonDocumentCollection.find().first();
        assertNotNull(document);
        BsonBinary uuidAsBinary = document.getBinary("_id");
        assertEquals(subType.getValue(), uuidAsBinary.getType());
        assertEquals(subType.getValue(), uuidAsBinary.getType());
        assertArrayEquals(encodedValue, uuidAsBinary.getData());
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) Test(org.junit.Test)

Example 13 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.

the class AbstractUuidRepresentationTest method shouldEncodeDocumentWithUuidRepresentation.

@Test
public void shouldEncodeDocumentWithUuidRepresentation() {
    if (uuidRepresentation == UuidRepresentation.UNSPECIFIED) {
        try {
            documentCollection.insertOne(new Document("_id", uuid));
            fail();
        } catch (CodecConfigurationException e) {
        // all good
        }
    } else {
        documentCollection.insertOne(new Document("_id", uuid));
        BsonDocument document = bsonDocumentCollection.find().first();
        assertNotNull(document);
        BsonBinary uuidAsBinary = document.getBinary("_id");
        assertEquals(subType.getValue(), uuidAsBinary.getType());
        assertArrayEquals(encodedValue, uuidAsBinary.getData());
    }
}
Also used : BsonDocument(org.bson.BsonDocument) BsonBinary(org.bson.BsonBinary) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) Test(org.junit.Test)

Example 14 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.

the class LazyPropertyModelCodec method getCodecFromPropertyRegistry.

@SuppressWarnings("unchecked")
private Codec<T> getCodecFromPropertyRegistry(final PropertyModel<T> propertyModel) {
    Codec<T> localCodec;
    try {
        localCodec = propertyCodecRegistry.get(propertyModel.getTypeData());
    } catch (CodecConfigurationException e) {
        return new LazyMissingCodec<>(propertyModel.getTypeData().getType(), e);
    }
    if (localCodec == null) {
        localCodec = new LazyMissingCodec<>(propertyModel.getTypeData().getType(), new CodecConfigurationException("Unexpected missing codec for: " + propertyModel.getName()));
    }
    BsonType representation = propertyModel.getBsonRepresentation();
    if (representation != null) {
        if (localCodec instanceof RepresentationConfigurable) {
            return ((RepresentationConfigurable<T>) localCodec).withRepresentation(representation);
        }
        throw new CodecConfigurationException("Codec must implement RepresentationConfigurable to support BsonRepresentation");
    }
    return localCodec;
}
Also used : BsonType(org.bson.BsonType) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) RepresentationConfigurable(org.bson.codecs.RepresentationConfigurable)

Example 15 with CodecConfigurationException

use of org.bson.codecs.configuration.CodecConfigurationException in project mongo-java-driver by mongodb.

the class PojoCodecImpl method getCodecFromDocument.

@SuppressWarnings("unchecked")
private Codec<T> getCodecFromDocument(final BsonReader reader, final boolean useDiscriminator, final String discriminatorKey, final CodecRegistry registry, final DiscriminatorLookup discriminatorLookup, final Codec<T> defaultCodec) {
    Codec<T> codec = defaultCodec;
    if (useDiscriminator) {
        BsonReaderMark mark = reader.getMark();
        reader.readStartDocument();
        boolean discriminatorKeyFound = false;
        while (!discriminatorKeyFound && reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            String name = reader.readName();
            if (discriminatorKey.equals(name)) {
                discriminatorKeyFound = true;
                try {
                    Class<?> discriminatorClass = discriminatorLookup.lookup(reader.readString());
                    if (!codec.getEncoderClass().equals(discriminatorClass)) {
                        codec = (Codec<T>) registry.get(discriminatorClass);
                    }
                } catch (Exception e) {
                    throw new CodecConfigurationException(format("Failed to decode '%s'. Decoding errored with: %s", classModel.getName(), e.getMessage()), e);
                }
            } else {
                reader.skipValue();
            }
        }
        mark.reset();
    }
    return codec;
}
Also used : BsonReaderMark(org.bson.BsonReaderMark) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) BsonInvalidOperationException(org.bson.BsonInvalidOperationException) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException)

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