Search in sources :

Example 21 with BsonBinaryReader

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");
    }
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BsonDocument(org.bson.BsonDocument) BsonBinaryReader(org.bson.BsonBinaryReader) BsonWriter(org.bson.BsonWriter) BsonBinaryWriter(org.bson.BsonBinaryWriter) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 22 with BsonBinaryReader

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()));
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) BsonWriter(org.immutables.mongo.bson4gson.BsonWriter) BsonBinaryWriter(org.bson.BsonBinaryWriter) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 23 with BsonBinaryReader

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);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) BsonBinaryReader(org.bson.BsonBinaryReader) BufferRecycler(com.fasterxml.jackson.core.util.BufferRecycler) BsonWriter(org.bson.BsonWriter) IOContext(com.fasterxml.jackson.core.io.IOContext) JsonNode(com.fasterxml.jackson.databind.JsonNode) BsonBinaryWriter(org.bson.BsonBinaryWriter) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 24 with BsonBinaryReader

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))));
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) ByteBufNIO(org.bson.ByteBufNIO) BasicOutputBuffer(org.bson.io.BasicOutputBuffer) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 25 with BsonBinaryReader

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();
                        }
                    }
                }));
            }
        }
    });
}
Also used : MongoException(com.mongodb.MongoException) BsonBinaryReader(org.bson.BsonBinaryReader) SingleResultCallback(com.mongodb.internal.async.SingleResultCallback) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Aggregations

BsonBinaryReader (org.bson.BsonBinaryReader)35 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)17 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)14 BsonBinaryWriter (org.bson.BsonBinaryWriter)12 Document (org.bson.Document)9 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)8 BsonWriter (org.bson.BsonWriter)7 ByteBuf (org.bson.ByteBuf)6 Test (org.junit.Test)6 IOContext (com.fasterxml.jackson.core.io.IOContext)5 BufferRecycler (com.fasterxml.jackson.core.util.BufferRecycler)5 BsonInput (org.bson.io.BsonInput)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 BsonDocument (org.bson.BsonDocument)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 MongoClientException (com.mongodb.MongoClientException)2 SingleResultCallback (com.mongodb.internal.async.SingleResultCallback)2 SplittablePayloadBsonWriter (com.mongodb.internal.connection.SplittablePayloadBsonWriter)2 List (java.util.List)2 Map (java.util.Map)2