use of org.bson.BsonDocumentWriter in project spring-data-mongodb by spring-projects.
the class ParameterBindingDocumentCodec method getDocumentId.
@Override
public BsonValue getDocumentId(final Document document) {
if (!documentHasId(document)) {
throw new IllegalStateException("The document does not contain an _id");
}
Object id = document.get(ID_FIELD_NAME);
if (id instanceof BsonValue) {
return (BsonValue) id;
}
BsonDocument idHoldingDocument = new BsonDocument();
BsonWriter writer = new BsonDocumentWriter(idHoldingDocument);
writer.writeStartDocument();
writer.writeName(ID_FIELD_NAME);
writeValue(writer, EncoderContext.builder().build(), id);
writer.writeEndDocument();
return idHoldingDocument.get(ID_FIELD_NAME);
}
use of org.bson.BsonDocumentWriter in project morphia by mongodb.
the class IndexHelper method toBsonDocument.
@SuppressWarnings("unchecked")
private BsonDocument toBsonDocument(final String key, final Object value) {
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
writer.writeStartDocument();
writer.writeName(key);
((Encoder) database.getCodecRegistry().get(value.getClass())).encode(writer, value, ENCODER_CONTEXT);
writer.writeEndDocument();
return writer.getDocument();
}
use of org.bson.BsonDocumentWriter in project mongo-java-driver by mongodb.
the class CommandMonitoringTest method getWritableCloneOfCommand.
private BsonDocument getWritableCloneOfCommand(final BsonDocument original) {
BsonDocument clone = new BsonDocument();
BsonDocumentWriter writer = new BsonDocumentWriter(clone);
new BsonDocumentCodec(CODEC_REGISTRY_HACK).encode(writer, original, EncoderContext.builder().build());
return clone;
}
use of org.bson.BsonDocumentWriter in project drill by axbaretto.
the class TestBsonRecordReader method testArrayOfDocumentType.
@Test
public void testArrayOfDocumentType() throws IOException {
BsonDocument bsonDoc = new BsonDocument();
BsonWriter bw = new BsonDocumentWriter(bsonDoc);
bw.writeStartDocument();
bw.writeName("a");
bw.writeString("MongoDB");
bw.writeName("b");
bw.writeStartArray();
bw.writeStartDocument();
bw.writeName("c");
bw.writeInt32(1);
bw.writeEndDocument();
bw.writeEndArray();
bw.writeEndDocument();
bw.flush();
writer.reset();
bsonReader.write(writer, new BsonDocumentReader(bsonDoc));
FieldReader reader = writer.getMapVector().getReader();
SingleMapReaderImpl mapReader = (SingleMapReaderImpl) reader;
FieldReader reader3 = mapReader.reader("b");
assertEquals("MongoDB", mapReader.reader("a").readText().toString());
}
use of org.bson.BsonDocumentWriter in project immutables by immutables.
the class BsonGeneratorTest method checkClosed.
@Test
void checkClosed() throws IOException {
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
BsonGenerator generator = generatorFor(writer);
check(!generator.isClosed());
generator.close();
check(generator.isClosed());
}
Aggregations