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;
}
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);
}
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();
}
}
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;
}
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;
});
}
Aggregations