@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> T decodeValue(ByteBuffer byteBuffer, Codec<T> codec) { try (BsonBinaryReader reader = new BsonBinaryReader(byteBuffer)) { reader.readStartDocument(); String name = reader.readName(); assertEquals("value", name); return codec.decode(reader, DecoderContext.builder().build()); } }
@Override public <T> T readFrom(ByteBuffer byteBuffer, Class<T> targetClazz) { if (byteBuffer == null) { return null; } checkNotNull(targetClazz, NOT_NULL_MSG); checkIsSupportClazz(targetClazz, NOT_SUPPORT_CLAZZ_MSG + targetClazz.getName()); BsonBinaryReader bsonBinaryReader = new BsonBinaryReader(byteBuffer); return BsonValueConverterRepertory.getBsonDocumentConverter().decode(bsonBinaryReader, targetClazz, bsonMapperConfig); }
@Override public <T> T readFrom(BsonInput bsonInput, Class<T> targetClazz) { if (bsonInput == null) { return null; } checkNotNull(targetClazz, NOT_NULL_MSG); checkIsSupportClazz(targetClazz, NOT_SUPPORT_CLAZZ_MSG + targetClazz.getName()); BsonBinaryReader bsonBinaryReader = new BsonBinaryReader(bsonInput); return BsonValueConverterRepertory.getBsonDocumentConverter().decode(bsonBinaryReader, targetClazz, bsonMapperConfig); }
@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); }
private void prepareReader() { writer.writeEndDocument(); reader = new BsonBinaryReader(new ByteBufferBsonInput(buffer.getByteBuffers().get(0))); reader.readStartDocument(); reader.readName(); }
public static Document byteArrayToMongoDocument(byte[] byteArray) { BsonBinaryReader bsonReader = new BsonBinaryReader(ByteBuffer.wrap(byteArray)); return new DocumentCodec().decode(bsonReader, DecoderContext.builder().build()); }
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 decodeMongoTest() throws Exception { BsonBinaryReader reader = new BsonBinaryReader(ByteBuffer.wrap(encoded)); return new BsonDocumentCodec().decode(reader, DecoderContext.builder().build()); }