@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); }
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(); } }
@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; }
@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); }
@Before public void before() { buffer = new BasicOutputBuffer(); writer = new BsonBinaryWriter(buffer); writer.writeStartDocument(); writer.writeName("test"); }
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(); }
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())); }
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()))); }
@Benchmark public Object encodeMongoTest() throws Exception { BasicOutputBuffer out = new BasicOutputBuffer(512); new BsonDocumentCodec().encode(new BsonBinaryWriter(out), doc1, EncoderContext.builder().build()); return out.getByteBuffers(); }