下面列出了com.fasterxml.jackson.core.util.BufferRecycler#org.bson.BsonBinaryWriter 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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");
}
}
/**
* write with BSON read with jackson.
* inverse of {@link #jacksonThenBson(String)}
*/
private void bsonThenJackson(String json) throws IOException {
ObjectNode toWrite = maybeWrap(mapper.readTree(json));
BasicOutputBuffer buffer = new BasicOutputBuffer();
BsonWriter writer = new BsonBinaryWriter(buffer);
// write with BSON
BsonDocument expected = BsonDocument.parse(toWrite.toString());
MongoClientSettings.getDefaultCodecRegistry().get(BsonDocument.class)
.encode(writer, expected, EncoderContext.builder().build());
BsonBinaryReader reader = new BsonBinaryReader(ByteBuffer.wrap(buffer.toByteArray()));
IOContext ioContext = new IOContext(new BufferRecycler(), null, false);
BsonParser parser = new BsonParser(ioContext, 0, reader);
// read with jackson
BsonDocument actual = BsonDocument.parse(mapper.readValue(parser, JsonNode.class).toString());
if (!actual.equals(expected)) {
check(maybeUnwrap(actual)).is(maybeUnwrap(expected));
Assertions.fail("Should have failed before");
}
}
/**
* Read and Write in Jackson API but using BSON reader/writer adapters
*/
private void jacksonThenJackson(String json) throws IOException {
ObjectNode expected = 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, 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 with Jackson
JsonNode actual = mapper.readValue(parser, JsonNode.class);
check(actual).is(expected);
}
BsonDocument toBsonDocument() {
docLock.readLock().lock();
try {
final BsonDocument asDoc = new BsonDocument();
asDoc.put(ConfigCodec.Fields.DOCUMENT_ID_FIELD, getDocumentId());
asDoc.put(ConfigCodec.Fields.SCHEMA_VERSION_FIELD, new BsonInt32(1));
asDoc.put(ConfigCodec.Fields.NAMESPACE_FIELD, new BsonString(getNamespace().toString()));
asDoc.put(ConfigCodec.Fields.LAST_RESOLUTION_FIELD, new BsonInt64(getLastResolution()));
if (getLastKnownRemoteVersion() != null) {
asDoc.put(ConfigCodec.Fields.LAST_KNOWN_REMOTE_VERSION_FIELD, getLastKnownRemoteVersion());
}
asDoc.put(ConfigCodec.Fields.LAST_KNOWN_HASH_FIELD, new BsonInt64(lastKnownHash));
if (lastUncommittedChangeEvent != null) {
final BsonDocument ceDoc = lastUncommittedChangeEvent.toBsonDocument();
final OutputBuffer outputBuffer = new BasicOutputBuffer();
final BsonWriter innerWriter = new BsonBinaryWriter(outputBuffer);
bsonDocumentCodec.encode(innerWriter, ceDoc, EncoderContext.builder().build());
final BsonBinary encoded = new BsonBinary(outputBuffer.toByteArray());
// TODO: This may put the doc above the 16MiB but ignore for now.
asDoc.put(ConfigCodec.Fields.LAST_UNCOMMITTED_CHANGE_EVENT, encoded);
}
asDoc.put(ConfigCodec.Fields.IS_STALE, new BsonBoolean(isStale));
asDoc.put(ConfigCodec.Fields.IS_PAUSED, new BsonBoolean(isPaused));
return asDoc;
} finally {
docLock.readLock().unlock();
}
}
public static byte[] toBytes(final BsonDocument doc) {
final BasicOutputBuffer buffer = new BasicOutputBuffer();
final BsonBinaryWriter writer = new BsonBinaryWriter(buffer);
BSON_DOCUMENT_CODEC.encode(writer, doc, EncoderContext.builder().build());
return buffer.toByteArray();
}
/**
* 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);
}
@SuppressWarnings("unchecked")
private static <T> T writeThenRead(CodecRegistry registry, ObjectMapper mapper, T value) throws IOException {
BasicOutputBuffer buffer = new BasicOutputBuffer();
BsonWriter writer = new BsonBinaryWriter(buffer);
registry.get((Class<T>) value.getClass()).encode(writer, value, EncoderContext.builder().build());
BsonBinaryReader reader = new BsonBinaryReader(ByteBuffer.wrap(buffer.toByteArray()));
IOContext ioContext = new IOContext(new BufferRecycler(), null, false);
BsonParser parser = new BsonParser(ioContext, 0, reader);
return mapper.readValue(parser, (Class<T>) value.getClass());
}
static BsonParser createParser(BsonDocument bson) {
BasicOutputBuffer buffer = new BasicOutputBuffer();
CodecRegistry registry = MongoClientSettings.getDefaultCodecRegistry();
registry.get(BsonDocument.class)
.encode(new BsonBinaryWriter(buffer), bson, EncoderContext.builder().build());
BsonBinaryReader reader = new BsonBinaryReader(ByteBuffer.wrap(buffer.toByteArray()));
IOContext ioContext = new IOContext(new BufferRecycler(), null, false);
return new BsonParser(ioContext, 0, reader);
}
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())));
}