Search in sources :

Example 11 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 12 with BsonBinaryReader

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

the class DocumentCodecTest method testCodeWithScopeEncoding.

@Test
public void testCodeWithScopeEncoding() throws IOException {
    DocumentCodec documentCodec = new DocumentCodec();
    Document doc = new Document();
    doc.put("theCode", new CodeWithScope("javaScript code", new Document("fieldNameOfScope", "valueOfScope")));
    documentCodec.encode(writer, doc, EncoderContext.builder().build());
    Document decodedDocument = documentCodec.decode(new BsonBinaryReader(createInputBuffer()), DecoderContext.builder().build());
    assertEquals(doc, decodedDocument);
}
Also used : CodeWithScope(org.bson.types.CodeWithScope) BsonBinaryReader(org.bson.BsonBinaryReader) Document(org.bson.Document) Test(org.junit.Test)

Example 13 with BsonBinaryReader

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

the class DocumentCodecTest method testIterableContainingOtherIterableEncoding.

@Test
public void testIterableContainingOtherIterableEncoding() throws IOException {
    DocumentCodec documentCodec = new DocumentCodec();
    Document doc = new Document();
    @SuppressWarnings("unchecked") List<List<Integer>> listOfLists = asList(asList(1), asList(2));
    doc.put("array", listOfLists);
    documentCodec.encode(writer, doc, EncoderContext.builder().build());
    BsonInput bsonInput = createInputBuffer();
    Document decodedDocument = documentCodec.decode(new BsonBinaryReader(bsonInput), DecoderContext.builder().build());
    assertEquals(doc, decodedDocument);
}
Also used : BsonInput(org.bson.io.BsonInput) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) BsonBinaryReader(org.bson.BsonBinaryReader) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Document(org.bson.Document) Test(org.junit.Test)

Example 14 with BsonBinaryReader

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

the class DocumentCodecTest method testIterableContainingDocumentsEncoding.

@Test
public void testIterableContainingDocumentsEncoding() throws IOException {
    DocumentCodec documentCodec = new DocumentCodec();
    Document doc = new Document();
    List<Document> listOfDocuments = asList(new Document("intVal", 1), new Document("anotherInt", 2));
    doc.put("array", listOfDocuments);
    documentCodec.encode(writer, doc, EncoderContext.builder().build());
    BsonInput bsonInput = createInputBuffer();
    Document decodedDocument = documentCodec.decode(new BsonBinaryReader(bsonInput), DecoderContext.builder().build());
    assertEquals(doc, decodedDocument);
}
Also used : BsonInput(org.bson.io.BsonInput) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) BsonBinaryReader(org.bson.BsonBinaryReader) Document(org.bson.Document) Test(org.junit.Test)

Aggregations

BsonBinaryReader (org.bson.BsonBinaryReader)14 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)12 Document (org.bson.Document)7 Test (org.junit.Test)6 BsonInput (org.bson.io.BsonInput)5 ByteBuf (org.bson.ByteBuf)3 ByteBufNIO (org.bson.ByteBufNIO)3 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)3 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)3 BsonBinaryWriter (org.bson.BsonBinaryWriter)2 StringWriter (java.io.StringWriter)1 Arrays.asList (java.util.Arrays.asList)1 Date (java.util.Date)1 List (java.util.List)1 BsonObjectId (org.bson.BsonObjectId)1 RawBsonDocumentCodec (org.bson.codecs.RawBsonDocumentCodec)1 JsonWriter (org.bson.json.JsonWriter)1 Binary (org.bson.types.Binary)1 Code (org.bson.types.Code)1 CodeWithScope (org.bson.types.CodeWithScope)1