当前位置: 首页>>代码示例>>Java>>正文


Java BsonDocumentWriter类代码示例

本文整理汇总了Java中org.bson.BsonDocumentWriter的典型用法代码示例。如果您正苦于以下问题:Java BsonDocumentWriter类的具体用法?Java BsonDocumentWriter怎么用?Java BsonDocumentWriter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


BsonDocumentWriter类属于org.bson包,在下文中一共展示了BsonDocumentWriter类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: toBson

import org.bson.BsonDocumentWriter; //导入依赖的package包/类
@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);
}
 
开发者ID:JPDSousa,项目名称:mongo-obj-framework,代码行数:19,代码来源:SmofParser.java

示例2: testArrayOfDocumentType

import org.bson.BsonDocumentWriter; //导入依赖的package包/类
@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());
}
 
开发者ID:axbaretto,项目名称:drill,代码行数:24,代码来源:TestBsonRecordReader.java

示例3: testRecursiveDocuments

import org.bson.BsonDocumentWriter; //导入依赖的package包/类
@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());
  }
}
 
开发者ID:axbaretto,项目名称:drill,代码行数:26,代码来源:TestBsonRecordReader.java

示例4: testArrayType

import org.bson.BsonDocumentWriter; //导入依赖的package包/类
@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());
}
 
开发者ID:axbaretto,项目名称:drill,代码行数:19,代码来源:TestBsonRecordReader.java

示例5: writeLogicalOperation

import org.bson.BsonDocumentWriter; //导入依赖的package包/类
/**
 * 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();
}
 
开发者ID:lambdamatic,项目名称:lambdamatic-project,代码行数:30,代码来源:FilterExpressionEncoder.java

示例6: toBson

import org.bson.BsonDocumentWriter; //导入依赖的package包/类
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("$");
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:14,代码来源:Support.java

示例7: gsonToBson

import org.bson.BsonDocumentWriter; //导入依赖的package包/类
@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();
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:36,代码来源:BsonReaderTest.java

示例8: serializeWithCodec

import org.bson.BsonDocumentWriter; //导入依赖的package包/类
@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);
}
 
开发者ID:JPDSousa,项目名称:mongo-obj-framework,代码行数:13,代码来源:AbstractBsonParser.java

示例9: gsonToBson

import org.bson.BsonDocumentWriter; //导入依赖的package包/类
/**
 * 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();
}
 
开发者ID:immutables,项目名称:immutables,代码行数:39,代码来源:BsonReaderTest.java

示例10: toBsonDocument

import org.bson.BsonDocumentWriter; //导入依赖的package包/类
@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();
}
 
开发者ID:mongodb,项目名称:morphia,代码行数:10,代码来源:IndexHelper.java


注:本文中的org.bson.BsonDocumentWriter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。