Java 类org.bson.BsonDocumentWriter 实例源码

项目:mongo-obj-framework    文件:SmofParser.java   
@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);
}
项目:drill    文件:TestBsonRecordReader.java   
@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());
}
项目:drill    文件:TestBsonRecordReader.java   
@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());
  }
}
项目:drill    文件:TestBsonRecordReader.java   
@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());
}
项目:lambdamatic-project    文件:FilterExpressionEncoder.java   
/**
 * Writes a logical operation of the following form:
 * 
 * <pre>
 * { $operator: [ { &lt;operand1&gt; }, { &lt;operand2&gt; } , ... , {&lt;operandN&gt; } ] }
 * </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();
}
项目:GitHub    文件:Support.java   
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("$");
}
项目:GitHub    文件:BsonReaderTest.java   
@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();
}
项目:mongo-obj-framework    文件:AbstractBsonParser.java   
@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);
}
项目:immutables    文件:Support.java   
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("$");
}
项目:immutables    文件:BsonReaderTest.java   
/**
 * 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();
}
项目:morphia    文件:IndexHelper.java   
@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();
}