Search in sources :

Example 11 with BsonDocumentWriter

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());
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) BsonInt32(org.bson.BsonInt32) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 12 with BsonDocumentWriter

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"));
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 13 with BsonDocumentWriter

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());
}
Also used : ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

Example 14 with BsonDocumentWriter

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);
}
Also used : BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test)

Example 15 with BsonDocumentWriter

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");
}
Also used : BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument)

Aggregations

BsonDocument (org.bson.BsonDocument)34 BsonDocumentWriter (org.bson.BsonDocumentWriter)34 Test (org.junit.Test)13 BsonWriter (org.bson.BsonWriter)9 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)6 BsonDocumentReader (org.bson.BsonDocumentReader)6 FieldReader (org.apache.drill.exec.vector.complex.reader.FieldReader)4 BsonValue (org.bson.BsonValue)4 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)4 BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)4 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)4 Test (org.junit.jupiter.api.Test)4 BaseTest (org.apache.drill.test.BaseTest)3 BsonInt32 (org.bson.BsonInt32)2 Codec (org.bson.codecs.Codec)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1