use of org.bson.BsonBinaryReader in project mongo-java-driver by mongodb.
the class ByteBufBsonDocument method toJson.
@Override
public String toJson(final JsonWriterSettings settings) {
StringWriter stringWriter = new StringWriter();
JsonWriter jsonWriter = new JsonWriter(stringWriter, settings);
ByteBuf duplicate = byteBuf.duplicate();
BsonBinaryReader reader = new BsonBinaryReader(new ByteBufferBsonInput(duplicate));
try {
jsonWriter.pipe(reader);
return stringWriter.toString();
} finally {
duplicate.release();
reader.close();
}
}
use of org.bson.BsonBinaryReader in project mongo-java-driver by mongodb.
the class ByteBufBsonDocument method findInDocument.
<T> T findInDocument(final Finder<T> finder) {
ByteBuf duplicateByteBuf = byteBuf.duplicate();
BsonBinaryReader bsonReader = new BsonBinaryReader(new ByteBufferBsonInput(byteBuf.duplicate()));
try {
bsonReader.readStartDocument();
while (bsonReader.readBsonType() != BsonType.END_OF_DOCUMENT) {
T found = finder.find(bsonReader);
if (found != null) {
return found;
}
}
bsonReader.readEndDocument();
} finally {
duplicateByteBuf.release();
bsonReader.close();
}
return finder.notFound();
}
use of org.bson.BsonBinaryReader in project immutables by immutables.
the class Jsons method asBsonReader.
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()));
}
use of org.bson.BsonBinaryReader in project immutables by immutables.
the class BsonParserTest method compare.
/**
* Converts string to json
*/
private void compare(String string) throws IOException {
JsonNode expected = mapper.readTree(string);
// BSON likes encoding full document (not simple elements like BsonValue)
if (!expected.isObject()) {
ObjectNode temp = mapper.createObjectNode();
temp.set("ignore", expected);
expected = temp;
}
BasicOutputBuffer buffer = new BasicOutputBuffer();
BsonWriter writer = new BsonBinaryWriter(buffer);
BsonGenerator generator = new BsonGenerator(0, mapper, writer);
// write
mapper.writeValue(generator, expected);
BsonBinaryReader reader = new BsonBinaryReader(ByteBuffer.wrap(buffer.toByteArray()));
IOContext ioContext = new IOContext(new BufferRecycler(), null, false);
BsonParser parser = new BsonParser(ioContext, 0, reader);
// read
JsonNode actual = mapper.readValue(parser, JsonNode.class);
check(actual).is(expected);
}
use of org.bson.BsonBinaryReader in project immutables by immutables.
the class BsonParserTest method jacksonThenBson.
/**
* write with Jackson read with Bson.
* Inverse of {@link #bsonThenJackson(String)}
*/
private void jacksonThenBson(String json) throws IOException {
ObjectNode toWrite = maybeWrap(mapper.readTree(json));
BasicOutputBuffer buffer = new BasicOutputBuffer();
BsonWriter writer = new BsonBinaryWriter(buffer);
BsonGenerator generator = new BsonGenerator(0, writer);
// write with jackson
mapper.writeValue(generator, toWrite);
BsonBinaryReader reader = new BsonBinaryReader(ByteBuffer.wrap(buffer.toByteArray()));
// read with BSON
BsonDocument actual = MongoClientSettings.getDefaultCodecRegistry().get(BsonDocument.class).decode(reader, DecoderContext.builder().build());
// compare results
BsonDocument expected = BsonDocument.parse(toWrite.toString());
if (!expected.equals(actual)) {
check(maybeUnwrap(actual)).is(maybeUnwrap(expected));
Assertions.fail("Should have failed before");
}
}
Aggregations