Search in sources :

Example 1 with BsonInput

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

the class DocumentCodecTest method testNestedDocumentEncoding.

@Test
public void testNestedDocumentEncoding() throws IOException {
    DocumentCodec documentCodec = new DocumentCodec();
    Document doc = new Document();
    doc.put("nested", new Document("x", 1));
    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)

Example 2 with BsonInput

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

the class DocumentCodecTest method testPrimitiveBSONTypeCodecs.

@Test
public void testPrimitiveBSONTypeCodecs() throws IOException {
    DocumentCodec documentCodec = new DocumentCodec();
    Document doc = new Document();
    doc.put("oid", new ObjectId());
    doc.put("integer", 1);
    doc.put("long", 2L);
    doc.put("string", "hello");
    doc.put("double", 3.2);
    doc.put("decimal", Decimal128.parse("0.100"));
    doc.put("binary", new Binary(BsonBinarySubType.USER_DEFINED, new byte[] { 0, 1, 2, 3 }));
    doc.put("date", new Date(1000));
    doc.put("boolean", true);
    doc.put("code", new Code("var i = 0"));
    doc.put("minkey", new MinKey());
    doc.put("maxkey", new MaxKey());
    //        doc.put("pattern", Pattern.compile("^hello"));  // TODO: Pattern doesn't override equals method!
    doc.put("null", null);
    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 : MinKey(org.bson.types.MinKey) BsonObjectId(org.bson.BsonObjectId) ObjectId(org.bson.types.ObjectId) BsonInput(org.bson.io.BsonInput) ByteBufferBsonInput(org.bson.io.ByteBufferBsonInput) MaxKey(org.bson.types.MaxKey) BsonBinaryReader(org.bson.BsonBinaryReader) Binary(org.bson.types.Binary) Document(org.bson.Document) Code(org.bson.types.Code) Date(java.util.Date) Test(org.junit.Test)

Example 3 with BsonInput

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

the class DocumentCodecTest method testIterableEncoding.

@Test
public void testIterableEncoding() throws IOException {
    DocumentCodec documentCodec = new DocumentCodec();
    Document doc = new Document().append("list", asList(1, 2, 3, 4, 5)).append("set", new HashSet<Integer>(asList(1, 2, 3, 4)));
    documentCodec.encode(writer, doc, EncoderContext.builder().build());
    BsonInput bsonInput = createInputBuffer();
    Document decodedDocument = documentCodec.decode(new BsonBinaryReader(bsonInput), DecoderContext.builder().build());
    assertEquals(new Document().append("list", asList(1, 2, 3, 4, 5)).append("set", asList(1, 2, 3, 4)), 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)

Example 4 with BsonInput

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

the class BsonBinaryWriter method pipe.

@Override
public void pipe(final BsonReader reader) {
    if (reader instanceof BsonBinaryReader) {
        BsonBinaryReader binaryReader = (BsonBinaryReader) reader;
        if (getState() == State.VALUE) {
            bsonOutput.writeByte(BsonType.DOCUMENT.getValue());
            writeCurrentName();
        }
        BsonInput bsonInput = binaryReader.getBsonInput();
        int size = bsonInput.readInt32();
        if (size < 5) {
            throw new BsonSerializationException("Document size must be at least 5");
        }
        bsonOutput.writeInt32(size);
        byte[] bytes = new byte[size - 4];
        bsonInput.readBytes(bytes);
        bsonOutput.writeBytes(bytes);
        binaryReader.setState(AbstractBsonReader.State.TYPE);
        if (getContext() == null) {
            setState(State.DONE);
        } else {
            if (getContext().getContextType() == BsonContextType.JAVASCRIPT_WITH_SCOPE) {
                // size of the JavaScript with scope value
                backpatchSize();
                setContext(getContext().getParentContext());
            }
            setState(getNextState());
        }
    } else {
        super.pipe(reader);
    }
}
Also used : BsonInput(org.bson.io.BsonInput)

Example 5 with BsonInput

use of org.bson.io.BsonInput 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)

Aggregations

BsonInput (org.bson.io.BsonInput)6 BsonBinaryReader (org.bson.BsonBinaryReader)5 Document (org.bson.Document)5 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)5 Test (org.junit.Test)5 Arrays.asList (java.util.Arrays.asList)1 Date (java.util.Date)1 List (java.util.List)1 BsonObjectId (org.bson.BsonObjectId)1 Binary (org.bson.types.Binary)1 Code (org.bson.types.Code)1 MaxKey (org.bson.types.MaxKey)1 MinKey (org.bson.types.MinKey)1 ObjectId (org.bson.types.ObjectId)1