Search in sources :

Example 31 with BsonBinaryReader

use of org.bson.BsonBinaryReader in project mongo-java-driver by mongodb.

the class ByteBufBsonDocument method toJson.

@Override
public String toJson(final JsonWriterSettings settings) {
    StringWriter stringWriter = new StringWriter();
    JsonWriter jsonWriter = new JsonWriter(stringWriter, settings);
    ByteBuf duplicate = byteBuf.duplicate();
    BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(duplicate));
    try {
        jsonWriter.pipe(reader);
        return stringWriter.toString();
    } finally {
        duplicate.release();
        reader.close();
    }
}
Also used : StringWriter(java.io.StringWriter) BsonBinaryReader(org.bson.BsonBinaryReader) ByteBuf(org.bson.ByteBuf) JsonWriter(org.bson.json.JsonWriter) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 32 with BsonBinaryReader

use of org.bson.BsonBinaryReader in project mongo-java-driver by mongodb.

the class ByteBufBsonDocument method findInDocument.

<T> T findInDocument(final Finder<T> finder) {
    ByteBuf duplicateByteBuf = byteBuf.duplicate();
    BsonBinaryReader bsonReader = new BsonBinaryReader(new ByteBufferBsonInput(byteBuf.duplicate()));
    try {
        bsonReader.readStartDocument();
        while (bsonReader.readBsonType() != BsonType.END_OF_DOCUMENT) {
            T found = finder.find(bsonReader);
            if (found != null) {
                return found;
            }
        }
        bsonReader.readEndDocument();
    } finally {
        duplicateByteBuf.release();
        bsonReader.close();
    }
    return finder.notFound();
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) ByteBuf(org.bson.ByteBuf) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput)

Example 33 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 34 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 35 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)

Aggregations

BsonBinaryReader (org.bson.BsonBinaryReader)36 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