Search in sources :

Example 1 with BsonReaderMark

use of org.bson.BsonReaderMark in project mongo-java-driver by mongodb.

the class GeometryDecoderHelper method decodeGeometry.

private static Geometry decodeGeometry(final BsonReader reader) {
    String type = null;
    BsonReaderMark mark = reader.getMark();
    validateIsDocument(reader);
    reader.readStartDocument();
    while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
        String key = reader.readName();
        if (key.equals("type")) {
            type = reader.readString();
            break;
        } else {
            reader.skipValue();
        }
    }
    mark.reset();
    if (type == null) {
        throw new CodecConfigurationException("Invalid Geometry item, document contained no type information.");
    }
    Geometry geometry = null;
    if (type.equals("Point")) {
        geometry = decodePoint(reader);
    } else if (type.equals("MultiPoint")) {
        geometry = decodeMultiPoint(reader);
    } else if (type.equals("Polygon")) {
        geometry = decodePolygon(reader);
    } else if (type.equals("MultiPolygon")) {
        geometry = decodeMultiPolygon(reader);
    } else if (type.equals("LineString")) {
        geometry = decodeLineString(reader);
    } else if (type.equals("MultiLineString")) {
        geometry = decodeMultiLineString(reader);
    } else if (type.equals("GeometryCollection")) {
        geometry = decodeGeometryCollection(reader);
    } else {
        throw new CodecConfigurationException(format("Invalid Geometry item, found type '%s'.", type));
    }
    return geometry;
}
Also used : Geometry(com.mongodb.client.model.geojson.Geometry) BsonReaderMark(org.bson.BsonReaderMark) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) LineString(com.mongodb.client.model.geojson.LineString) MultiLineString(com.mongodb.client.model.geojson.MultiLineString)

Example 2 with BsonReaderMark

use of org.bson.BsonReaderMark in project morphia by mongodb.

the class DocumentReaderTest method testBookmarkingAndSkips.

@Test
public void testBookmarkingAndSkips() {
    setup(Document.parse("{ key: { subKey: 3 }, second: 2 }"));
    step(r -> assertEquals(r.getCurrentBsonType(), BsonType.DOCUMENT));
    step(BsonReader::readStartDocument);
    step(r -> assertEquals(r.readName(), "key"));
    BsonReaderMark mark = reader.getMark();
    step(BsonReader::readStartDocument);
    step(r -> assertEquals(r.readName(), "subKey"));
    step(r -> assertEquals(r.readInt32(), 3));
    step(BsonReader::readEndDocument);
    mark.reset();
    reader.skipValue();
    step(r -> assertEquals(r.readName(), "second"));
    step(r -> assertEquals(r.readInt32(), 2));
    step(BsonReader::readEndDocument);
    setup(Document.parse("{ key: [ 1, 2, 3 ], second: 8 }"));
    step(r -> assertEquals(r.getCurrentBsonType(), BsonType.DOCUMENT));
    step(BsonReader::readStartDocument);
    step(r -> assertEquals(r.readName(), "key"));
    mark = reader.getMark();
    step(BsonReader::readStartArray);
    step(r -> assertEquals(r.readInt32(), 1));
    step(r -> assertEquals(r.readInt32(), 2));
    mark.reset();
    reader.skipValue();
    step(r -> assertEquals(r.readName(), "second"));
    step(r -> assertEquals(r.readInt32(), 8));
    step(BsonReader::readEndDocument);
}
Also used : BsonReaderMark(org.bson.BsonReaderMark) BsonReader(org.bson.BsonReader) Test(org.testng.annotations.Test)

Example 3 with BsonReaderMark

use of org.bson.BsonReaderMark in project morphia by mongodb.

the class EntityDecoder method decodeModel.

protected void decodeModel(BsonReader reader, DecoderContext decoderContext, MorphiaInstanceCreator instanceCreator, @Nullable PropertyModel model) {
    if (model != null) {
        final BsonReaderMark mark = reader.getMark();
        try {
            if (reader.getCurrentBsonType() == BsonType.NULL) {
                reader.readNull();
            } else {
                Object value = decoderContext.decodeWithChildContext(model.getCodec(), reader);
                instanceCreator.set(value, model);
            }
        } catch (BsonInvalidOperationException e) {
            mark.reset();
            final Object value = morphiaCodec.getDatastore().getCodecRegistry().get(Object.class).decode(reader, decoderContext);
            instanceCreator.set(convert(value, model.getTypeData().getType()), model);
        }
    } else {
        reader.skipValue();
    }
}
Also used : BsonReaderMark(org.bson.BsonReaderMark) BsonInvalidOperationException(org.bson.BsonInvalidOperationException)

Example 4 with BsonReaderMark

use of org.bson.BsonReaderMark 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)

Example 5 with BsonReaderMark

use of org.bson.BsonReaderMark in project mongo-java-driver by mongodb.

the class JsonReaderTest method testMultipleMarks.

@Test
public void testMultipleMarks() {
    String json = "{a : { b : 1 }}";
    testStringAndStream(json, bsonReader -> {
        bsonReader.readStartDocument();
        BsonReaderMark markOne = bsonReader.getMark();
        bsonReader.readName("a");
        bsonReader.readStartDocument();
        BsonReaderMark markTwo = bsonReader.getMark();
        bsonReader.readName("b");
        bsonReader.readInt32();
        bsonReader.readEndDocument();
        markTwo.reset();
        bsonReader.readName("b");
        markOne.reset();
        bsonReader.readName("a");
        return null;
    });
}
Also used : BsonReaderMark(org.bson.BsonReaderMark) Test(org.junit.Test)

Aggregations

BsonReaderMark (org.bson.BsonReaderMark)8 CodecConfigurationException (org.bson.codecs.configuration.CodecConfigurationException)3 MappingException (dev.morphia.mapping.MappingException)2 BsonInvalidOperationException (org.bson.BsonInvalidOperationException)2 BsonReader (org.bson.BsonReader)2 Document (org.bson.Document)2 Test (org.testng.annotations.Test)2 Geometry (com.mongodb.client.model.geojson.Geometry)1 LineString (com.mongodb.client.model.geojson.LineString)1 MultiLineString (com.mongodb.client.model.geojson.MultiLineString)1 Key (dev.morphia.Key)1 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)1 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)1 BsonType (org.bson.BsonType)1 Test (org.junit.Test)1