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