Search in sources :

Example 41 with BsonDocumentCodec

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());
}
Also used : BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 42 with BsonDocumentCodec

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());
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(org.bson.json.JsonWriter) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Test(org.junit.Test)

Example 43 with BsonDocumentCodec

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());
}
Also used : ServerAddressSelector(com.mongodb.internal.selector.ServerAddressSelector) NoOpFieldNameValidator(com.mongodb.internal.validator.NoOpFieldNameValidator) BsonDocument(org.bson.BsonDocument) BsonString(org.bson.BsonString) ServerAddress(com.mongodb.ServerAddress) BsonDouble(org.bson.BsonDouble) BsonString(org.bson.BsonString) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) Test(org.junit.Test)

Example 44 with BsonDocumentCodec

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();
        }
    }
}
Also used : BsonInt32(org.bson.BsonInt32) SocketStreamFactory(com.mongodb.connection.SocketStreamFactory) BsonDocument(org.bson.BsonDocument) Cluster(com.mongodb.internal.connection.Cluster) ClusterBinding(com.mongodb.internal.binding.ClusterBinding) AsyncClusterBinding(com.mongodb.internal.binding.AsyncClusterBinding) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Example 45 with BsonDocumentCodec

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();
}
Also used : StringWriter(java.io.StringWriter) JsonWriter(org.bson.json.JsonWriter) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec)

Aggregations

BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)56 BsonDocument (org.bson.BsonDocument)29 MongoNamespace (com.mongodb.MongoNamespace)9 BsonBinaryReader (org.bson.BsonBinaryReader)8 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)8 BsonBinaryWriter (org.bson.BsonBinaryWriter)5 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)5 JsonReader (org.bson.json.JsonReader)5 Test (org.junit.Test)5 CollectionHelper (com.mongodb.client.test.CollectionHelper)4 NoOpFieldNameValidator (com.mongodb.internal.validator.NoOpFieldNameValidator)4 StringWriter (java.io.StringWriter)4 BsonArray (org.bson.BsonArray)4 BsonString (org.bson.BsonString)4 JsonWriter (org.bson.json.JsonWriter)4 ByteBuffer (java.nio.ByteBuffer)3 BsonDocumentWriter (org.bson.BsonDocumentWriter)3 Before (org.junit.Before)3 MongoClientSettings (com.mongodb.MongoClientSettings)2 MongoCommandException (com.mongodb.MongoCommandException)2