Java 类org.bson.BsonBinaryWriter 实例源码

项目:bsoncodec    文件:AbstractTest.java   
@SuppressWarnings("resource")
protected <T> void writeReadCompare(T source, Codec<T> codec) {
    BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
    BsonBinaryWriter writer = new BsonBinaryWriter(bsonOutput);
    writer.writeStartDocument();
    writer.writeName("name");
    codec.encode(writer, source, EncoderContext.builder().build());
    writer.writeEndDocument();
    writer.close();

    BsonBinaryReader reader = new BsonBinaryReader(
            ByteBuffer.wrap(bsonOutput.toByteArray()));
    reader.readStartDocument();
    assertThat(reader.readName()).isEqualTo("name");
    T readNow = codec.decode(reader, DecoderContext.builder().build());

    assertThat(readNow).isEqualTo(source);
}
项目:bson-codecs-jsr310    文件:CodecsTests.java   
private <T> void encodeValue(BsonOutput output, Codec<T> codec, T value) {
    try (BsonBinaryWriter writer = new BsonBinaryWriter(output)) {

        writer.writeStartDocument();
        writer.writeName("value");

        codec.encode(writer, value, EncoderContext.builder().build());

        writer.writeEndDocument();
        writer.close();
    }
}
项目:BsonMapper    文件:DefaultBsonMapper.java   
@Override
public BsonOutput writeToBsonOutput(Object object) {
    if (object == null) {
        return null;
    }
    BsonOutput bsonOutput = new BasicOutputBuffer();
    BsonWriter bsonBinaryWriter = new BsonBinaryWriter(new BsonWriterSettings(bsonMapperConfig.getMaxMapperLayerNum()),
            new BsonBinaryWriterSettings(bsonMapperConfig.getMaxDocumentSizeForBsonWriter()), bsonOutput);
    BsonValueConverterRepertory.getBsonDocumentConverter().encode(bsonBinaryWriter, object);
    return bsonOutput;
}
项目:BsonMapper    文件:BsonDocumentConverterTest.java   
@Test
public void testByteIOEncode() throws Exception {
    BasicOutputBuffer bsonOutput = new BasicOutputBuffer();
    BsonWriter bsonWriter = new BsonBinaryWriter(bsonOutput);
    BsonValueConverterRepertory.getBsonDocumentConverter().encode(bsonWriter, getObject());
    BsonReader bsonReader = new BsonBinaryReader(new ByteBufferBsonInput(bsonOutput.getByteBuffers().get(0)));
    BsonTest bsonTest = BsonValueConverterRepertory.getBsonDocumentConverter().decode(bsonReader, BsonTest.class, BsonMapperConfig.DEFALUT);
    System.out.println(bsonTest);
}
项目:java-code-gen    文件:TestCodecs.java   
@Before
public void before() {
    buffer = new BasicOutputBuffer();
    writer = new BsonBinaryWriter(buffer);
    writer.writeStartDocument();
    writer.writeName("test");
}
项目:lumongo    文件:LumongoUtil.java   
public static byte[] mongoDocumentToByteArray(Document mongoDocument) {
    BasicOutputBuffer outputBuffer = new BasicOutputBuffer();
    BsonBinaryWriter writer = new BsonBinaryWriter(outputBuffer);
    new DocumentCodec().encode(writer, mongoDocument, EncoderContext.builder().isEncodingCollectibleDocument(true).build());
    return outputBuffer.toByteArray();
}
项目:immutables    文件:Jsons.java   
static org.bson.BsonReader asBsonReader(JsonObject gson) throws IOException {
  BasicOutputBuffer buffer = new BasicOutputBuffer();
  BsonWriter writer = new BsonWriter(new BsonBinaryWriter(buffer));
  TypeAdapters.JSON_ELEMENT.write(writer, gson);
  return new BsonBinaryReader(ByteBuffer.wrap(buffer.toByteArray()));
}
项目:immutables    文件:Jsons.java   
static JsonReader asGsonReader(BsonDocument bson) {
  BasicOutputBuffer output = new BasicOutputBuffer();
  new BsonDocumentCodec().encode(new BsonBinaryWriter(output), bson, EncoderContext.builder().build());
  return new BsonReader(new BsonBinaryReader(ByteBuffer.wrap(output.toByteArray())));
}
项目:vertx-native-mongo    文件:EncoderPerfTest.java   
@Benchmark
public Object encodeMongoTest() throws Exception {
    BasicOutputBuffer out = new BasicOutputBuffer(512);
    new BsonDocumentCodec().encode(new BsonBinaryWriter(out), doc1, EncoderContext.builder().build());
    return out.getByteBuffers();
}