use of org.bson.BsonBinaryReader in project immutables by immutables.
the class BsonParserTest method jacksonThenBson.
/**
* write with Jackson read with Bson.
* Inverse of {@link #bsonThenJackson(String)}
*/
private void jacksonThenBson(String json) throws IOException {
ObjectNode toWrite = maybeWrap(mapper.readTree(json));
BasicOutputBuffer buffer = new BasicOutputBuffer();
BsonWriter writer = new BsonBinaryWriter(buffer);
BsonGenerator generator = new BsonGenerator(0, writer);
// write with jackson
mapper.writeValue(generator, toWrite);
BsonBinaryReader reader = new BsonBinaryReader(ByteBuffer.wrap(buffer.toByteArray()));
// read with BSON
BsonDocument actual = MongoClientSettings.getDefaultCodecRegistry().get(BsonDocument.class).decode(reader, DecoderContext.builder().build());
// compare results
BsonDocument expected = BsonDocument.parse(toWrite.toString());
if (!expected.equals(actual)) {
check(maybeUnwrap(actual)).is(maybeUnwrap(expected));
Assertions.fail("Should have failed before");
}
}
use of org.bson.BsonBinaryReader in project immutables by immutables.
the class Jsons method asBsonReader.
static org.bson.BsonReader asBsonReader(JsonObject gson) throws IOException {
BasicOutputBuffer buffer = new BasicOutputBuffer();
BsonWriter writer = new BsonWriter(new BsonBinaryWriter(buffer));
TypeAdapters.JSON_ELEMENT.write(writer, gson);
return new BsonBinaryReader(ByteBuffer.wrap(buffer.toByteArray()));
}
use of org.bson.BsonBinaryReader in project immutables by immutables.
the class BsonParserTest method compare.
/**
* Converts string to json
*/
private void compare(String string) throws IOException {
JsonNode expected = mapper.readTree(string);
// BSON likes encoding full document (not simple elements like BsonValue)
if (!expected.isObject()) {
ObjectNode temp = mapper.createObjectNode();
temp.set("ignore", expected);
expected = temp;
}
BasicOutputBuffer buffer = new BasicOutputBuffer();
BsonWriter writer = new BsonBinaryWriter(buffer);
BsonGenerator generator = new BsonGenerator(0, mapper, writer);
// write
mapper.writeValue(generator, expected);
BsonBinaryReader reader = new BsonBinaryReader(ByteBuffer.wrap(buffer.toByteArray()));
IOContext ioContext = new IOContext(new BufferRecycler(), null, false);
BsonParser parser = new BsonParser(ioContext, 0, reader);
// read
JsonNode actual = mapper.readValue(parser, JsonNode.class);
check(actual).is(expected);
}
use of org.bson.BsonBinaryReader in project mongo-java-driver by mongodb.
the class CodecTestUtil method prepareReaderWithObjectToBeDecoded.
static <T> BsonBinaryReader prepareReaderWithObjectToBeDecoded(final T objectToDecode, final Codec<T> codec) {
BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
byte[] documentAsByteArrayForReader;
try {
codec.encode(writer, objectToDecode, EncoderContext.builder().build());
documentAsByteArrayForReader = outputBuffer.toByteArray();
} finally {
writer.close();
}
return new BsonBinaryReader(new ByteBufferBsonInput(new ByteBufNIO(wrap(documentAsByteArrayForReader))));
}
use of org.bson.BsonBinaryReader in project mongo-java-driver by mongodb.
the class InternalStreamConnection method sendCommandMessageAsync.
private <T> void sendCommandMessageAsync(final int messageId, final Decoder<T> decoder, final SessionContext sessionContext, final SingleResultCallback<T> callback, final ByteBufferBsonOutput bsonOutput, final CommandEventSender commandEventSender, final boolean responseExpected) {
sendMessageAsync(bsonOutput.getByteBuffers(), messageId, new SingleResultCallback<Void>() {
@Override
public void onResult(final Void result, final Throwable t) {
bsonOutput.close();
if (t != null) {
commandEventSender.sendFailedEvent(t);
callback.onResult(null, t);
} else if (!responseExpected) {
commandEventSender.sendSucceededEventForOneWayCommand();
callback.onResult(null, null);
} else {
readAsync(MESSAGE_HEADER_LENGTH, new MessageHeaderCallback(new SingleResultCallback<ResponseBuffers>() {
@Override
public void onResult(final ResponseBuffers responseBuffers, final Throwable t) {
if (t != null) {
commandEventSender.sendFailedEvent(t);
callback.onResult(null, t);
return;
}
try {
updateSessionContext(sessionContext, responseBuffers);
boolean commandOk = isCommandOk(new BsonBinaryReader(new ByteBufferBsonInput(responseBuffers.getBodyByteBuffer())));
responseBuffers.reset();
if (!commandOk) {
MongoException commandFailureException = getCommandFailureException(responseBuffers.getResponseDocument(messageId, new BsonDocumentCodec()), description.getServerAddress());
commandEventSender.sendFailedEvent(commandFailureException);
throw commandFailureException;
}
commandEventSender.sendSucceededEvent(responseBuffers);
T result = getCommandResult(decoder, responseBuffers, messageId);
callback.onResult(result, null);
} catch (Throwable localThrowable) {
callback.onResult(null, localThrowable);
} finally {
responseBuffers.close();
}
}
}));
}
}
});
}
Aggregations