Search in sources :

Example 26 with BsonDocumentWriter

use of org.bson.BsonDocumentWriter in project immutables by immutables.

the class MongoSession method toBsonValue.

/**
 * Convert generic object to a BsonValue
 */
private BsonValue toBsonValue(Object value) {
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    writer.writeStartDocument();
    writer.writeName("value");
    Codec<Object> codec = (Codec<Object>) collection.getCodecRegistry().get(value.getClass());
    codec.encode(writer, value, EncoderContext.builder().build());
    writer.writeEndDocument();
    return writer.getDocument().get("value");
}
Also used : Codec(org.bson.codecs.Codec) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument)

Example 27 with BsonDocumentWriter

use of org.bson.BsonDocumentWriter in project immutables by immutables.

the class BsonReaderTest method gsonToBson.

/**
 * Tests direct bson and gson mappings
 */
@Test
public void gsonToBson() throws Exception {
    JsonObject obj = new JsonObject();
    obj.addProperty("boolean", true);
    obj.addProperty("int32", 32);
    obj.addProperty("int64", 64L);
    obj.addProperty("double", 42.42D);
    obj.addProperty("string", "foo");
    obj.add("null", JsonNull.INSTANCE);
    obj.add("array", new JsonArray());
    obj.add("object", new JsonObject());
    BsonDocument doc = Jsons.toBson(obj);
    TypeAdapters.JSON_ELEMENT.write(new BsonWriter(new BsonDocumentWriter(doc)), obj);
    check(doc.keySet()).notEmpty();
    check(doc.get("boolean").getBsonType()).is(BsonType.BOOLEAN);
    check(doc.get("boolean").asBoolean());
    check(doc.get("int32").getBsonType()).is(BsonType.INT32);
    check(doc.get("int32").asInt32().getValue()).is(32);
    check(doc.get("int64").getBsonType()).is(BsonType.INT64);
    check(doc.get("int64").asInt64().getValue()).is(64L);
    check(doc.get("double").getBsonType()).is(BsonType.DOUBLE);
    check(doc.get("double").asDouble().getValue()).is(42.42D);
    check(doc.get("string").getBsonType()).is(BsonType.STRING);
    check(doc.get("string").asString().getValue()).is("foo");
    check(doc.get("null").getBsonType()).is(BsonType.NULL);
    check(doc.get("null").isNull());
    check(doc.get("array").getBsonType()).is(BsonType.ARRAY);
    check(doc.get("array").asArray()).isEmpty();
    check(doc.get("object").getBsonType()).is(BsonType.DOCUMENT);
    check(doc.get("object").asDocument().keySet()).isEmpty();
}
Also used : JsonArray(com.google.gson.JsonArray) BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) JsonObject(com.google.gson.JsonObject) Test(org.junit.Test)

Example 28 with BsonDocumentWriter

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

the class CodecTestCase method getEncodedValue.

<T> BsonValue getEncodedValue(final T value, final Encoder<T> encoder) {
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    writer.writeStartDocument();
    writer.writeName("val");
    encoder.encode(writer, value, EncoderContext.builder().build());
    writer.writeEndDocument();
    return writer.getDocument().get("val");
}
Also used : BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument)

Example 29 with BsonDocumentWriter

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

the class DBObjectCodec method getDocumentId.

@Override
public BsonValue getDocumentId(final DBObject document) {
    if (!documentHasId(document)) {
        throw new IllegalStateException("The document does not contain an _id");
    }
    Object id = document.get(ID_FIELD_NAME);
    if (id instanceof BsonValue) {
        return (BsonValue) id;
    }
    BsonDocument idHoldingDocument = new BsonDocument();
    BsonWriter writer = new BsonDocumentWriter(idHoldingDocument);
    writer.writeStartDocument();
    writer.writeName(ID_FIELD_NAME);
    writeValue(writer, EncoderContext.builder().build(), id);
    writer.writeEndDocument();
    return idHoldingDocument.get(ID_FIELD_NAME);
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonWriter(org.bson.BsonWriter) BSONObject(org.bson.BSONObject) BsonValue(org.bson.BsonValue)

Example 30 with BsonDocumentWriter

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

the class DBObjectCodecTest method shouldEncodedNestedMapsListsAndDocuments.

@Test
public void shouldEncodedNestedMapsListsAndDocuments() {
    // {"0" : 0, "1", 1}
    byte[] zeroOneDocumentBytes = new byte[] { 19, 0, 0, 0, 16, 48, 0, 0, 0, 0, 0, 16, 49, 0, 1, 0, 0, 0, 0 };
    Map<String, Object> zeroOneMap = new HashMap<String, Object>();
    zeroOneMap.put("0", 0);
    zeroOneMap.put("1", 1);
    DBObject zeroOneDBObject = new BasicDBObject();
    zeroOneDBObject.putAll(zeroOneMap);
    DBObject zeroOneDBList = new BasicDBList();
    zeroOneDBList.putAll(zeroOneMap);
    List<Integer> zeroOneList = asList(0, 1);
    DBObjectCodec dbObjectCodec = new DBObjectCodec(fromProviders(asList(new ValueCodecProvider(), new DBObjectCodecProvider(), new BsonValueCodecProvider())));
    DBObject doc = new BasicDBObject().append("map", zeroOneMap).append("dbDocument", zeroOneDBObject).append("dbList", zeroOneDBList).append("list", zeroOneList).append("array", new int[] { 0, 1 }).append("lazyDoc", new LazyDBObject(zeroOneDocumentBytes, new LazyBSONCallback())).append("lazyArray", new LazyDBList(zeroOneDocumentBytes, new LazyBSONCallback()));
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    dbObjectCodec.encode(writer, doc, EncoderContext.builder().build());
    BsonDocument zeroOneBsonDocument = new BsonDocument().append("0", new BsonInt32(0)).append("1", new BsonInt32(1));
    BsonArray zeroOneBsonArray = new BsonArray(asList(new BsonInt32(0), new BsonInt32(1)));
    assertEquals(new BsonDocument("map", zeroOneBsonDocument).append("dbDocument", zeroOneBsonDocument).append("dbList", zeroOneBsonArray).append("list", zeroOneBsonArray).append("array", zeroOneBsonArray).append("lazyDoc", zeroOneBsonDocument).append("lazyArray", zeroOneBsonArray), writer.getDocument());
}
Also used : HashMap(java.util.HashMap) ValueCodecProvider(org.bson.codecs.ValueCodecProvider) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) BsonInt32(org.bson.BsonInt32) BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) LazyBSONCallback(org.bson.LazyBSONCallback) BsonArray(org.bson.BsonArray) BsonValueCodecProvider(org.bson.codecs.BsonValueCodecProvider) Test(org.junit.Test)

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