org.bson.BsonBinaryReader Java Examples

The following examples show how to use org.bson.BsonBinaryReader. 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: 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 #5
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 #6
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 #7
Source File: CoreDocumentSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 4 votes vote down vote up
static CoreDocumentSynchronizationConfig fromBsonDocument(final BsonDocument document) {
  keyPresent(ConfigCodec.Fields.DOCUMENT_ID_FIELD, document);
  keyPresent(ConfigCodec.Fields.NAMESPACE_FIELD, document);
  keyPresent(ConfigCodec.Fields.SCHEMA_VERSION_FIELD, document);
  keyPresent(ConfigCodec.Fields.LAST_RESOLUTION_FIELD, document);
  keyPresent(ConfigCodec.Fields.IS_STALE, document);
  keyPresent(ConfigCodec.Fields.IS_PAUSED, document);

  final int schemaVersion =
      document.getNumber(ConfigCodec.Fields.SCHEMA_VERSION_FIELD).intValue();
  if (schemaVersion != 1) {
    throw new IllegalStateException(
        String.format(
            "unexpected schema version '%d' for %s",
            schemaVersion,
            CoreDocumentSynchronizationConfig.class.getSimpleName()));
  }

  final MongoNamespace namespace =
      new MongoNamespace(document.getString(ConfigCodec.Fields.NAMESPACE_FIELD).getValue());

  final BsonDocument lastVersion;
  if (document.containsKey(ConfigCodec.Fields.LAST_KNOWN_REMOTE_VERSION_FIELD)) {
    lastVersion = document.getDocument(ConfigCodec.Fields.LAST_KNOWN_REMOTE_VERSION_FIELD);
  } else {
    lastVersion = null;
  }

  final ChangeEvent<BsonDocument> lastUncommittedChangeEvent;
  if (document.containsKey(ConfigCodec.Fields.LAST_UNCOMMITTED_CHANGE_EVENT)) {
    final BsonBinary eventBin =
        document.getBinary(ConfigCodec.Fields.LAST_UNCOMMITTED_CHANGE_EVENT);
    final BsonReader innerReader = new BsonBinaryReader(ByteBuffer.wrap(eventBin.getData()));
    lastUncommittedChangeEvent = ResultDecoders.changeEventDecoder(BSON_DOCUMENT_CODEC)
        .decode(innerReader, DecoderContext.builder().build());
  } else {
    lastUncommittedChangeEvent = null;
  }

  return new CoreDocumentSynchronizationConfig(
      null,
      namespace,
      document.get(ConfigCodec.Fields.DOCUMENT_ID_FIELD),
      lastUncommittedChangeEvent,
      document.getNumber(ConfigCodec.Fields.LAST_RESOLUTION_FIELD).longValue(),
      lastVersion,
      new ReentrantReadWriteLock(),
      document.getBoolean(ConfigCodec.Fields.IS_STALE).getValue(),
      document.getBoolean(ConfigCodec.Fields.IS_PAUSED, new BsonBoolean(false)).getValue(),
      document.getInt64(ConfigCodec.Fields.LAST_KNOWN_HASH_FIELD, new BsonInt64(0))
        .getValue());
}
 
Example #8
Source File: LumongoUtil.java    From lumongo with Apache License 2.0 4 votes vote down vote up
public static Document byteArrayToMongoDocument(byte[] byteArray) {
	BsonBinaryReader bsonReader = new BsonBinaryReader(ByteBuffer.wrap(byteArray));
	return new DocumentCodec().decode(bsonReader, DecoderContext.builder().build());
}
 
Example #9
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 #10
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())));
}