org.bson.codecs.BsonDocumentCodec Java Examples

The following examples show how to use org.bson.codecs.BsonDocumentCodec. 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: ResultDecoders.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
public RemoteUpdateResult decode(
    final BsonReader reader,
    final DecoderContext decoderContext) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  keyPresent(Fields.MATCHED_COUNT_FIELD, document);
  keyPresent(Fields.MODIFIED_COUNT_FIELD, document);
  final long matchedCount = document.getNumber(Fields.MATCHED_COUNT_FIELD).longValue();
  final long modifiedCount = document.getNumber(Fields.MODIFIED_COUNT_FIELD).longValue();
  if (!document.containsKey(Fields.UPSERTED_ID_FIELD)) {
    return new RemoteUpdateResult(matchedCount, modifiedCount, null);
  }

  return new RemoteUpdateResult(
      matchedCount,
      modifiedCount,
      document.get(Fields.UPSERTED_ID_FIELD));
}
 
Example #2
Source File: ResultDecoders.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public ChangeEvent<DocumentT> decode(
    final BsonReader reader,
    final DecoderContext decoderContext
) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  final ChangeEvent<BsonDocument> rawChangeEvent = ChangeEvent.fromBsonDocument(document);

  if (codec == null || codec.getClass().equals(BsonDocumentCodec.class)) {
    return (ChangeEvent<DocumentT>)rawChangeEvent;
  }
  return new ChangeEvent<>(
      rawChangeEvent.getId(),
      rawChangeEvent.getOperationType(),
      rawChangeEvent.getFullDocument() == null ? null : codec.decode(
          rawChangeEvent.getFullDocument().asBsonReader(),
          DecoderContext.builder().build()),
      rawChangeEvent.getNamespace(),
      rawChangeEvent.getDocumentKey(),
      rawChangeEvent.getUpdateDescription(),
      rawChangeEvent.hasUncommittedWrites());
}
 
Example #3
Source File: ResultDecoders.java    From stitch-android-sdk with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public CompactChangeEvent<DocumentT> decode(
    final BsonReader reader,
    final DecoderContext decoderContext
) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  final CompactChangeEvent<BsonDocument> rawChangeEvent =
      CompactChangeEvent.fromBsonDocument(document);

  if (codec == null || codec.getClass().equals(BsonDocumentCodec.class)) {
    return (CompactChangeEvent<DocumentT>)rawChangeEvent;
  }
  return new CompactChangeEvent<>(
      rawChangeEvent.getOperationType(),
      rawChangeEvent.getFullDocument() == null ? null : codec.decode(
          rawChangeEvent.getFullDocument().asBsonReader(),
          DecoderContext.builder().build()),
      rawChangeEvent.getDocumentKey(),
      rawChangeEvent.getUpdateDescription(),
      rawChangeEvent.getStitchDocumentVersion(),
      rawChangeEvent.getStitchDocumentHash(),
      rawChangeEvent.hasUncommittedWrites());
}
 
Example #4
Source File: ResultDecoders.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
public RemoteDeleteResult decode(
    final BsonReader reader,
    final DecoderContext decoderContext) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  keyPresent(Fields.DELETED_COUNT_FIELD, document);
  return new RemoteDeleteResult(document.getNumber(Fields.DELETED_COUNT_FIELD).longValue());
}
 
Example #5
Source File: ResultDecoders.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
public RemoteInsertOneResult decode(
    final BsonReader reader,
    final DecoderContext decoderContext
) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  keyPresent(Fields.INSERTED_ID_FIELD, document);
  return new RemoteInsertOneResult(document.get(Fields.INSERTED_ID_FIELD));
}
 
Example #6
Source File: ResultDecoders.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
public RemoteInsertManyResult decode(
    final BsonReader reader,
    final DecoderContext decoderContext
) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  keyPresent(Fields.INSERTED_IDS_FIELD, document);
  final BsonArray arr = document.getArray(Fields.INSERTED_IDS_FIELD);
  final Map<Long, BsonValue> insertedIds = new HashMap<>();
  for (int i = 0; i < arr.size(); i++) {
    insertedIds.put((long) i, arr.get(i));
  }

  return new RemoteInsertManyResult(insertedIds);
}
 
Example #7
Source File: NamespaceSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public NamespaceSynchronizationConfig decode(
    final BsonReader reader,
    final DecoderContext decoderContext
) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  return fromBsonDocument(document);
}
 
Example #8
Source File: NamespaceSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(
    final BsonWriter writer,
    final NamespaceSynchronizationConfig value,
    final EncoderContext encoderContext
) {
  new BsonDocumentCodec().encode(writer, value.toBsonDocument(), encoderContext);
}
 
Example #9
Source File: CoreDocumentSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
public CoreDocumentSynchronizationConfig decode(
    final BsonReader reader,
    final DecoderContext decoderContext
) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  return fromBsonDocument(document);
}
 
Example #10
Source File: CoreDocumentSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
public void encode(
    final BsonWriter writer,
    final CoreDocumentSynchronizationConfig value,
    final EncoderContext encoderContext
) {
  new BsonDocumentCodec().encode(writer, value.toBsonDocument(), encoderContext);
}
 
Example #11
Source File: InstanceSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public InstanceSynchronizationConfig decode(
    final BsonReader reader,
    final DecoderContext decoderContext
) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  return fromBsonDocument(document);
}
 
Example #12
Source File: InstanceSynchronizationConfig.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(
    final BsonWriter writer,
    final InstanceSynchronizationConfig value,
    final EncoderContext encoderContext
) {
  new BsonDocumentCodec().encode(writer, value.toBsonDocument(), encoderContext);
}
 
Example #13
Source File: ResultDecoders.java    From stitch-android-sdk with Apache License 2.0 5 votes vote down vote up
@Override
public FcmSendMessageResult decode(
    final BsonReader reader,
    final DecoderContext decoderContext
) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  keyPresent(Fields.SUCCESSES_FIELD, document);
  keyPresent(Fields.FAILURES_FIELD, document);

  final List<FcmSendMessageResultFailureDetail> failureDetails;
  if (document.containsKey(Fields.FAILURE_DETAILS_FIELD)) {
    final BsonArray detailsArr = document.getArray(Fields.FAILURE_DETAILS_FIELD);
    failureDetails = new ArrayList<>(detailsArr.size());
    for (final BsonValue detail: detailsArr) {
      final BsonDocument detailDoc = detail.asDocument();
      final String userId;
      if (detailDoc.containsKey(Fields.FAILURE_DETAIL_USER_ID_FIELD)) {
        userId = detailDoc.getString(Fields.FAILURE_DETAIL_USER_ID_FIELD).getValue();
      } else {
        userId = null;
      }
      failureDetails.add(new FcmSendMessageResultFailureDetail(
          detailDoc.getNumber(Fields.FAILURE_DETAIL_INDEX_FIELD).longValue(),
          detailDoc.getString(Fields.FAILURE_DETAIL_ERROR_FIELD).getValue(),
          userId));
    }
  } else {
    failureDetails = null;
  }

  return new FcmSendMessageResult(
      document.getNumber(Fields.SUCCESSES_FIELD).longValue(),
      document.getNumber(Fields.FAILURES_FIELD).longValue(),
      failureDetails);
}
 
Example #14
Source File: Jsons.java    From immutables with Apache License 2.0 4 votes vote down vote up
static BsonDocument toBson(JsonObject gson) throws IOException {
  return new BsonDocumentCodec().decode(asBsonReader(gson), DecoderContext.builder().build());
}
 
Example #15
Source File: TodoItem.java    From stitch-android-sdk with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(
    final BsonWriter writer, final TodoItem value, final EncoderContext encoderContext) {
  new BsonDocumentCodec().encode(writer, toBsonDocument(value), encoderContext);
}
 
Example #16
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())));
}
 
Example #17
Source File: JsonPoweredTestHelper.java    From mongo-java-driver-reactivestreams with Apache License 2.0 4 votes vote down vote up
public static BsonDocument getTestDocument(final File file) throws IOException {
    return new BsonDocumentCodec().decode(new JsonReader(getFileAsString(file)), DecoderContext.builder().build());
}
 
Example #18
Source File: JsonPoweredTestHelper.java    From mongo-java-driver-rx with Apache License 2.0 4 votes vote down vote up
public static BsonDocument getTestDocument(final File file) throws IOException {
    return new BsonDocumentCodec().decode(new JsonReader(getFileAsString(file)), DecoderContext.builder().build());
}
 
Example #19
Source File: TodoItem.java    From stitch-android-sdk with Apache License 2.0 4 votes vote down vote up
@Override
public TodoItem decode(
    final BsonReader reader, final DecoderContext decoderContext) {
  final BsonDocument document = (new BsonDocumentCodec()).decode(reader, decoderContext);
  return fromBsonDocument(document);
}