use of org.bson.BsonDocumentWriter in project mongo-java-driver by mongodb.
the class DBObjectCodecTest method shouldEncodeIterableMapAsMap.
@Test
public void shouldEncodeIterableMapAsMap() {
IterableMap iterableMap = new IterableMap();
iterableMap.put("first", 1);
DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
DBObject doc = new BasicDBObject("map", iterableMap);
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
dbObjectCodec.encode(writer, doc, EncoderContext.builder().build());
assertEquals(new BsonDocument("map", new BsonDocument("first", new BsonInt32(1))), writer.getDocument());
}
use of org.bson.BsonDocumentWriter in project mongo-java-driver by mongodb.
the class DBObjectCodecTest method shouldRespectEncodeIdFirstPropertyInEncoderContext.
@Test
public void shouldRespectEncodeIdFirstPropertyInEncoderContext() {
DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
// given
DBObject doc = new BasicDBObject("x", 2).append("_id", 2);
// when
BsonDocument encodedDocument = new BsonDocument();
dbObjectCodec.encode(new BsonDocumentWriter(encodedDocument), doc, EncoderContext.builder().isEncodingCollectibleDocument(true).build());
// then
assertEquals(new ArrayList<String>(encodedDocument.keySet()), asList("_id", "x"));
// when
encodedDocument.clear();
dbObjectCodec.encode(new BsonDocumentWriter(encodedDocument), doc, EncoderContext.builder().isEncodingCollectibleDocument(false).build());
// then
assertEquals(new ArrayList<String>(encodedDocument.keySet()), asList("x", "_id"));
}
use of org.bson.BsonDocumentWriter in project mongo-java-driver by mongodb.
the class DBObjectCodecTest method shouldEncodeNull.
@Test
public void shouldEncodeNull() {
DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
DBObject doc = new BasicDBObject("null", null);
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
dbObjectCodec.encode(writer, doc, EncoderContext.builder().build());
assertEquals(new BsonDocument("null", BsonNull.VALUE), writer.getDocument());
}
use of org.bson.BsonDocumentWriter in project immutables by immutables.
the class BsonGeneratorTest method binary.
@Test
void binary() throws IOException {
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
BsonGenerator generator = generatorFor(writer);
check(generator.canWriteBinaryNatively());
check(writeAndReturnValue(gen -> gen.writeBinary(new byte[] {})).asBinary().getData()).isEmpty();
check(writeAndReturnValue(gen -> gen.writeBinary(new byte[] { 1 })).asBinary().getData()).isOf((byte) 1);
check(writeAndReturnValue(gen -> gen.writeBinary(new byte[] { 1, 2 })).asBinary().getData()).isOf((byte) 1, (byte) 2);
check(writeAndReturnValue(gen -> gen.writeBinary(new byte[] { 1, 2, 3 })).asBinary().getData()).isOf((byte) 1, (byte) 2, (byte) 3);
}
use of org.bson.BsonDocumentWriter in project immutables by immutables.
the class BsonGeneratorTest method writeAndReturnValue.
private static BsonValue writeAndReturnValue(IoConsumer<BsonGenerator> consumer) throws IOException {
BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
BsonGenerator generator = generatorFor(writer);
generator.writeStartObject();
generator.writeFieldName("value");
consumer.accept(generator);
generator.writeEndObject();
BsonDocument doc = writer.getDocument();
check(doc.keySet()).has("value");
return doc.get("value");
}
Aggregations