use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.
the class BsonHelper method toBson.
public static ByteBuffer toBson(final BsonDocument document) {
BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
new BsonDocumentCodec().encode(new BsonBinaryWriter(bsonOutput), document, EncoderContext.builder().build());
return ByteBuffer.wrap(bsonOutput.toByteArray());
}
use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.
the class BsonDocumentTest method toJsonShouldRespectDefaultJsonWriterSettings.
@Test
public void toJsonShouldRespectDefaultJsonWriterSettings() {
StringWriter writer = new StringWriter();
new BsonDocumentCodec().encode(new JsonWriter(writer), document, EncoderContext.builder().build());
assertEquals(writer.toString(), document.toJson());
}
use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.
the class SingleServerClusterTest method shouldSuccessfullyQueryASecondaryWithPrimaryReadPreference.
@Test
public void shouldSuccessfullyQueryASecondaryWithPrimaryReadPreference() {
// given
ServerAddress secondary = getSecondary();
setUpCluster(secondary);
String collectionName = getClass().getName();
Connection connection = cluster.selectServer(new ServerAddressSelector(secondary)).getServer().getConnection();
// when
BsonDocument result = connection.command(getDefaultDatabaseName(), new BsonDocument("count", new BsonString(collectionName)), new NoOpFieldNameValidator(), ReadPreference.primary(), new BsonDocumentCodec(), NoOpSessionContext.INSTANCE, getServerApi(), IgnorableRequestContext.INSTANCE);
// then
assertEquals(new BsonDouble(1.0).intValue(), result.getNumber("ok").intValue());
}
use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.
the class ClusterFixture method getConnectionString.
public static synchronized ConnectionString getConnectionString() {
if (connectionString != null) {
return connectionString;
}
ConnectionString mongoURIProperty = getConnectionStringFromSystemProperty(MONGODB_URI_SYSTEM_PROPERTY_NAME);
if (mongoURIProperty != null) {
return mongoURIProperty;
}
// Figure out what the connection string should be
Cluster cluster = createCluster(new ConnectionString(DEFAULT_URI), new SocketStreamFactory(SocketSettings.builder().build(), SslSettings.builder().build()));
try {
BsonDocument helloResult = new CommandReadOperation<BsonDocument>("admin", new BsonDocument(LEGACY_HELLO, new BsonInt32(1)), new BsonDocumentCodec()).execute(new ClusterBinding(cluster, ReadPreference.nearest(), ReadConcern.DEFAULT, getServerApi(), IgnorableRequestContext.INSTANCE));
if (helloResult.containsKey("setName")) {
connectionString = new ConnectionString(DEFAULT_URI + "/?replicaSet=" + helloResult.getString("setName").getValue());
} else {
connectionString = new ConnectionString(DEFAULT_URI);
ClusterFixture.cluster = cluster;
}
return connectionString;
} finally {
if (ClusterFixture.cluster == null) {
cluster.close();
}
}
}
use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.
the class MongoCommandException method getResponseAsJson.
private static String getResponseAsJson(final BsonDocument commandResponse) {
StringWriter writer = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(writer);
new BsonDocumentCodec().encode(jsonWriter, commandResponse, EncoderContext.builder().build());
return writer.toString();
}
Aggregations