Search in sources :

Example 16 with BsonBinaryReader

use of org.bson.BsonBinaryReader in project immutables by immutables.

the class Jsons method asGsonReader.

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())));
}
Also used : BsonReader(org.immutables.mongo.bson4gson.BsonReader) BsonBinaryReader(org.bson.BsonBinaryReader) BsonBinaryWriter(org.bson.BsonBinaryWriter) BsonDocumentCodec(org.bson.codecs.BsonDocumentCodec) BasicOutputBuffer(org.bson.io.BasicOutputBuffer)

Example 17 with BsonBinaryReader

use of org.bson.BsonBinaryReader in project mongo-flink by mongo-flink.

the class DocumentBulkSerializer method deserializeV1.

private DocumentBulk deserializeV1(DataInputDeserializer in) throws IOException {
    DocumentBulk bulk = new DocumentBulk();
    int len;
    while ((len = in.readInt()) != END_OF_INPUT) {
        byte[] docBuffer = new byte[len];
        in.read(docBuffer);
        BsonBinaryReader bsonReader = new BsonBinaryReader(ByteBuffer.wrap(docBuffer));
        bulk.add(DOCUMENT_CODEC.decode(bsonReader, DecoderContext.builder().build()));
    }
    return bulk;
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader)

Example 18 with BsonBinaryReader

use of org.bson.BsonBinaryReader in project zuliasearch by zuliaio.

the class ZuliaUtil method byteArrayToMongoDocument.

public static Document byteArrayToMongoDocument(byte[] byteArray) {
    if (byteArray != null && byteArray.length != 0) {
        BsonBinaryReader bsonReader = new BsonBinaryReader(ByteBuffer.wrap(byteArray));
        DecoderContext decoderContext = DecoderContext.builder().build();
        if (pojoCodecRegistry != null) {
            return new DocumentCodec(pojoCodecRegistry).decode(bsonReader, decoderContext);
        } else {
            return new DocumentCodec().decode(bsonReader, decoderContext);
        }
    }
    return new Document();
}
Also used : DecoderContext(org.bson.codecs.DecoderContext) BsonBinaryReader(org.bson.BsonBinaryReader) DocumentCodec(org.bson.codecs.DocumentCodec) Document(org.bson.Document)

Example 19 with BsonBinaryReader

use of org.bson.BsonBinaryReader in project agileway by fangjinuo.

the class Bsons method deserialize.

public static <T> T deserialize(byte[] bytes, @NonNull Class<T> targetClass) {
    Preconditions.checkNotNull(targetClass);
    ByteBuffer buffer = ByteBuffer.wrap(bytes);
    BsonBinaryReader reader = new BsonBinaryReader(buffer);
    Codec<T> codec = codecRegistry.get(targetClass);
    DecoderContext decoderContext = DecoderContext.builder().build();
    T t = codec.decode(reader, decoderContext);
    return t;
}
Also used : DecoderContext(org.bson.codecs.DecoderContext) BsonBinaryReader(org.bson.BsonBinaryReader) ByteBuffer(java.nio.ByteBuffer)

Example 20 with BsonBinaryReader

use of org.bson.BsonBinaryReader in project LanternPowerMonitor by MarkBryanMilligan.

the class DaoSerializer method fromBson.

public static DaoEntity fromBson(byte[] _btBson) {
    if (_btBson == null)
        return null;
    BsonBinaryReader reader = null;
    try {
        reader = new BsonBinaryReader(ByteBuffer.wrap(_btBson).order(ByteOrder.LITTLE_ENDIAN));
        Document doc = new DocumentCodec().decode(reader, DecoderContext.builder().build());
        if (doc == null)
            return null;
        return new DaoEntity(doc);
    } catch (Throwable t) {
        LOG.error("Failed to convert bson to DaoEntity", t);
        return null;
    } finally {
        if (reader != null)
            reader.close();
    }
}
Also used : BsonBinaryReader(org.bson.BsonBinaryReader) DocumentCodec(org.bson.codecs.DocumentCodec) Document(org.bson.Document)

Aggregations

BsonBinaryReader (org.bson.BsonBinaryReader)35 ByteBufferBsonInput (org.bson.io.ByteBufferBsonInput)17 BasicOutputBuffer (org.bson.io.BasicOutputBuffer)14 BsonBinaryWriter (org.bson.BsonBinaryWriter)12 Document (org.bson.Document)9 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)8 BsonWriter (org.bson.BsonWriter)7 ByteBuf (org.bson.ByteBuf)6 Test (org.junit.Test)6 IOContext (com.fasterxml.jackson.core.io.IOContext)5 BufferRecycler (com.fasterxml.jackson.core.util.BufferRecycler)5 BsonInput (org.bson.io.BsonInput)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 BsonDocument (org.bson.BsonDocument)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 MongoClientException (com.mongodb.MongoClientException)2 SingleResultCallback (com.mongodb.internal.async.SingleResultCallback)2 SplittablePayloadBsonWriter (com.mongodb.internal.connection.SplittablePayloadBsonWriter)2 List (java.util.List)2 Map (java.util.Map)2