org.bson.BsonBinaryWriter Java Examples

The following examples show how to use org.bson.BsonBinaryWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BsonParserTest.java    From immutables with Apache License 2.0 6 votes vote down vote up
/**
 * 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");
  }
}
 
Example #2
Source File: BsonParserTest.java    From immutables with Apache License 2.0 6 votes vote down vote up
/**
 * 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");
  }
}
 
Example #3
Source File: BsonParserTest.java    From immutables with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #4
Source File: CoreDocumentSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
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();
  }
}
 
Example #5
Source File: HashUtils.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #6
Source File: BsonParserTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #7
Source File: JacksonCodecsTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #8
Source File: Parsers.java    From immutables with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example #9
Source File: LumongoUtil.java    From lumongo with Apache License 2.0 4 votes vote down vote up
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();
}
 
Example #10
Source File: Jsons.java    From immutables with Apache License 2.0 4 votes vote down vote up
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()));
}
 
Example #11
Source File: Jsons.java    From immutables with Apache License 2.0 4 votes vote down vote up
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())));
}