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 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 GenericBsonDecimal128Test method encodeToHex.
private String encodeToHex(final BsonDocument decodedDocument) {
BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
new BsonDocumentCodec().encode(new BsonBinaryWriter(outputBuffer), decodedDocument, EncoderContext.builder().build());
return DatatypeConverter.printHexBinary(outputBuffer.toByteArray());
}
use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.
the class MessageHelper method encodeJson.
private static ByteBuf encodeJson(final String json) {
OutputBuffer outputBuffer = new BasicOutputBuffer();
JsonReader jsonReader = new JsonReader(json);
BsonDocumentCodec codec = new BsonDocumentCodec();
BsonDocument document = codec.decode(jsonReader, DecoderContext.builder().build());
BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
codec.encode(writer, document, EncoderContext.builder().build());
ByteBuffer documentByteBuffer = ByteBuffer.allocate(outputBuffer.size());
documentByteBuffer.put(outputBuffer.toByteArray());
return new ByteBufNIO(documentByteBuffer);
}
use of org.bson.codecs.BsonDocumentCodec in project mongo-java-driver by mongodb.
the class WriteProtocol method execute.
@Override
public WriteConcernResult execute(final InternalConnection connection) {
WriteConcernResult writeConcernResult = null;
RequestMessage requestMessage = null;
do {
long startTimeNanos = System.nanoTime();
RequestMessage.EncodingMetadata encodingMetadata;
int messageId;
boolean sentCommandStartedEvent = false;
ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(connection);
try {
if (requestMessage == null) {
requestMessage = createRequestMessage(getMessageSettings(connection.getDescription()));
}
encodingMetadata = requestMessage.encodeWithMetadata(bsonOutput);
sendStartedEvent(connection, requestMessage, encodingMetadata, bsonOutput);
sentCommandStartedEvent = true;
messageId = requestMessage.getId();
if (shouldAcknowledge(encodingMetadata.getNextMessage())) {
CommandMessage getLastErrorMessage = new CommandMessage(new MongoNamespace(getNamespace().getDatabaseName(), COMMAND_COLLECTION_NAME).getFullName(), createGetLastErrorCommandDocument(), false, getMessageSettings(connection.getDescription()));
getLastErrorMessage.encode(bsonOutput);
messageId = getLastErrorMessage.getId();
}
connection.sendMessage(bsonOutput.getByteBuffers(), messageId);
} catch (RuntimeException e) {
sendFailedEvent(connection, requestMessage, sentCommandStartedEvent, e, startTimeNanos);
throw e;
} finally {
bsonOutput.close();
}
if (shouldAcknowledge(encodingMetadata.getNextMessage())) {
ResponseBuffers responseBuffers = null;
try {
responseBuffers = connection.receiveMessage(messageId);
ReplyMessage<BsonDocument> replyMessage = new ReplyMessage<BsonDocument>(responseBuffers, new BsonDocumentCodec(), messageId);
writeConcernResult = getWriteResult(replyMessage.getDocuments().get(0), connection.getDescription().getServerAddress());
} catch (WriteConcernException e) {
sendSucceededEvent(connection, requestMessage, encodingMetadata.getNextMessage(), e, startTimeNanos);
if (writeConcern.isAcknowledged()) {
throw e;
} else if (ordered) {
break;
}
} catch (RuntimeException e) {
sendFailedEvent(connection, requestMessage, sentCommandStartedEvent, e, startTimeNanos);
throw e;
} finally {
if (responseBuffers != null) {
responseBuffers.close();
}
}
}
sendSucceededEvent(connection, requestMessage, encodingMetadata.getNextMessage(), writeConcernResult, startTimeNanos);
requestMessage = encodingMetadata.getNextMessage();
} while (requestMessage != null);
return writeConcern.isAcknowledged() ? writeConcernResult : WriteConcernResult.unacknowledged();
}
Aggregations