@SuppressWarnings("unchecked") public <T> BsonValue toBson(Object value, Class<T> clazz) { if(value == null) { return new BsonNull(); } if(value instanceof BsonValue) { return (BsonValue) value; } final Codec<T> codec = registry.get(clazz); final String key = "value"; final BsonDocument document = new BsonDocument(); final BsonWriter writer = new BsonDocumentWriter(document); writer.writeStartDocument(); writer.writeName(key); codec.encode(writer, (T) value, EncoderContext.builder().build()); writer.writeEndDocument(); return document.get(key); }
@Test public void testArrayOfDocumentType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); BsonWriter bw = new BsonDocumentWriter(bsonDoc); bw.writeStartDocument(); bw.writeName("a"); bw.writeString("MongoDB"); bw.writeName("b"); bw.writeStartArray(); bw.writeStartDocument(); bw.writeName("c"); bw.writeInt32(1); bw.writeEndDocument(); bw.writeEndArray(); bw.writeEndDocument(); bw.flush(); writer.reset(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); FieldReader reader = writer.getMapVector().getReader(); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) reader; FieldReader reader3 = mapReader.reader("b"); assertEquals("MongoDB", mapReader.reader("a").readText().toString()); }
@Test public void testRecursiveDocuments() throws IOException { BsonDocument topDoc = new BsonDocument(); final int count = 3; for (int i = 0; i < count; ++i) { BsonDocument bsonDoc = new BsonDocument(); BsonWriter bw = new BsonDocumentWriter(bsonDoc); bw.writeStartDocument(); bw.writeName("k1" + i); bw.writeString("drillMongo1" + i); bw.writeName("k2" + i); bw.writeString("drillMongo2" + i); bw.writeEndDocument(); bw.flush(); topDoc.append("doc" + i, bsonDoc); } writer.reset(); bsonReader.write(writer, new BsonDocumentReader(topDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); for (int i = 0; i < count; ++i) { SingleMapReaderImpl reader = (SingleMapReaderImpl) mapReader.reader("doc" + i); assertEquals("drillMongo1" + i, reader.reader("k1" + i).readText().toString()); assertEquals("drillMongo2" + i, reader.reader("k2" + i).readText().toString()); } }
@Test public void testArrayType() throws IOException { BsonDocument bsonDoc = new BsonDocument(); BsonWriter bw = new BsonDocumentWriter(bsonDoc); bw.writeStartDocument(); bw.writeName("arrayKey"); bw.writeStartArray(); bw.writeInt32(1); bw.writeInt32(2); bw.writeInt32(3); bw.writeEndArray(); bw.writeEndDocument(); bw.flush(); bsonReader.write(writer, new BsonDocumentReader(bsonDoc)); SingleMapReaderImpl mapReader = (SingleMapReaderImpl) writer.getMapVector().getReader(); FieldReader reader = mapReader.reader("arrayKey"); assertEquals(3, reader.size()); }
/** * Writes a logical operation of the following form: * * <pre> * { $operator: [ { <operand1> }, { <operand2> } , ... , {<operandN> } ] } * </pre> * * @param operator the operator to write * @param operands the operands to write */ private void writeLogicalOperation(final MongoOperator operator, final List<Expression> operands) { this.writer.writeStartArray(operator.getLiteral()); for (Expression operand : operands) { final BsonDocument operandDocument = new BsonDocument(); try (final BsonDocumentWriter operandBsonWriter = new BsonDocumentWriter(operandDocument);) { try ( final BsonDocumentReader operandBsonReader = new BsonDocumentReader(operandDocument);) { final FilterExpressionEncoder operandEncoder = new FilterExpressionEncoder(this.queryMetadataClass, this.queryMetadataVarName, operandBsonWriter, this.encoderContext); operand.accept(operandEncoder); this.writer.pipe(operandBsonReader); } } } this.writer.writeEndArray(); }
BsonValue toBson() { @SuppressWarnings("unchecked") Encoder<T> encoder = BsonEncoding.encoderFor((Class<T>) value.getClass(), adapter); BsonDocument bson = new BsonDocument(); org.bson.BsonWriter writer = new BsonDocumentWriter(bson); // Bson doesn't allow to write directly scalars / primitives, they have to be embedded in a document. writer.writeStartDocument(); writer.writeName("$"); encoder.encode(writer, value, EncoderContext.builder().build()); writer.writeEndDocument(); writer.flush(); return bson.get("$"); }
@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 = new BsonDocument(); 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(); }
@SuppressWarnings("unchecked") protected final <T> BsonValue serializeWithCodec(Codec<T> codec, Object value) { checkArgument(codec != null, "Cannot find a valid codec to serialize: " + value); final BsonDocument document = new BsonDocument(); final String name = "result"; final BsonDocumentWriter writer = new BsonDocumentWriter(document); writer.writeStartDocument(); writer.writeName(name); codec.encode(writer, (T) value, EncoderContext.builder().build()); writer.writeEndDocument(); return document.get(name); }
/** * 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(); }
@SuppressWarnings("unchecked") private BsonDocument toBsonDocument(final String key, final Object value) { BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument()); writer.writeStartDocument(); writer.writeName(key); ((Encoder) database.getCodecRegistry().get(value.getClass())).encode(writer, value, ENCODER_CONTEXT); writer.writeEndDocument(); return writer.getDocument(); }