Search in sources :

Example 6 with BsonReaderMark

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

the class DocumentReaderTest method mark.

@Test
public void mark() {
    setup(new Document("key", "value").append("nested", "detsen"));
    step(BsonReader::readStartDocument);
    BsonReaderMark docMark = reader.getMark();
    step(r -> assertEquals(r.readName(), "key"));
    step(r -> assertEquals(r.readString(), "value"));
    docMark.reset();
    step(r -> assertEquals(r.readName(), "key"));
    step(r -> assertEquals(r.readString(), "value"));
    step(r -> assertEquals(r.readName(), "nested"));
    step(r -> assertEquals(r.readString(), "detsen"));
    step(BsonReader::readEndDocument);
}
Also used : BsonReaderMark(org.bson.BsonReaderMark) BsonReader(org.bson.BsonReader) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 7 with BsonReaderMark

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

the class ObjectCodec method decode.

@Override
public Object decode(BsonReader reader, DecoderContext decoderContext) {
    BsonType bsonType = reader.getCurrentBsonType();
    Class<?> clazz;
    if (bsonType == BsonType.DOCUMENT) {
        clazz = Document.class;
        String discriminatorField = datastore.getMapper().getOptions().getDiscriminatorKey();
        BsonReaderMark mark = reader.getMark();
        reader.readStartDocument();
        while (clazz.equals(Document.class) && reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            if (reader.readName().equals(discriminatorField)) {
                try {
                    clazz = datastore.getMapper().getClass(reader.readString());
                } catch (CodecConfigurationException e) {
                    throw new MappingException(e.getMessage(), e);
                }
            } else {
                reader.skipValue();
            }
        }
        mark.reset();
    } else {
        clazz = bsonTypeClassMap.get(bsonType);
    }
    return datastore.getCodecRegistry().get(clazz).decode(reader, decoderContext);
}
Also used : BsonType(org.bson.BsonType) BsonReaderMark(org.bson.BsonReaderMark) CodecConfigurationException(org.bson.codecs.configuration.CodecConfigurationException) Document(org.bson.Document) MappingException(dev.morphia.mapping.MappingException)

Example 8 with BsonReaderMark

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

the class KeyCodec method decode.

@Override
public Key decode(BsonReader reader, DecoderContext decoderContext) {
    reader.readStartDocument();
    final String ref = reader.readString("$ref");
    reader.readName();
    final BsonReaderMark mark = reader.getMark();
    Object idValue = null;
    EntityModel model = null;
    final Iterator<EntityModel> iterator = datastore.getMapper().getClassesMappedToCollection(ref).iterator();
    while (idValue == null && iterator.hasNext()) {
        model = iterator.next();
        try {
            final PropertyModel idField = model.getIdProperty();
            if (idField != null) {
                final Class<?> idType = idField.getType();
                idValue = datastore.getCodecRegistry().get(idType).decode(reader, decoderContext);
            }
        } catch (Exception e) {
            mark.reset();
        }
    }
    if (idValue == null) {
        throw new MappingException("Could not map the Key to a type.");
    }
    reader.readEndDocument();
    return new Key<>(model.getType(), ref, idValue);
}
Also used : BsonReaderMark(org.bson.BsonReaderMark) EntityModel(dev.morphia.mapping.codec.pojo.EntityModel) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) MappingException(dev.morphia.mapping.MappingException) Key(dev.morphia.Key) MappingException(dev.morphia.mapping.MappingException)

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