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);
}
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);
}
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);
}
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);
}
}
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);
}
Aggregations