Пример #1
0
  @Override
  public void encode(BsonWriter writer, Ra t, EncoderContext ec) {
    writer.writeStartDocument();
    Codec<LocalDate> dateCodec = codecRegistry.get(LocalDate.class);
    writer.writeName("name");
    writer.writeString(t.getName());
    writer.writeName("pointsTaken");
    writer.writeDouble(t.getPointsTaken());
    writer.writeName("previousDutyDay");
    ec.encodeWithChildContext(dateCodec, writer, t.getPreviousDutyDay());

    // Write Blackout Date Array
    writer.writeName("blackoutDates");
    writer.writeStartArray();
    for (LocalDate blackoutDate : t.getBlackoutDates()) {
      ec.encodeWithChildContext(dateCodec, writer, blackoutDate);
    }
    writer.writeEndArray();

    // Write Duty Day Array
    writer.writeName("dutyDays");
    writer.writeStartArray();
    for (LocalDate dutyDay : t.getDutyDays()) {
      ec.encodeWithChildContext(dateCodec, writer, dutyDay);
    }
    writer.writeEndArray();
    writer.writeEndDocument();
  }
 private void beforeFields(
     final BsonWriter bsonWriter, final EncoderContext encoderContext, final BsonDocument value) {
   if (encoderContext.isEncodingCollectibleDocument() && value.containsKey(ID_FIELD_NAME)) {
     bsonWriter.writeName(ID_FIELD_NAME);
     writeValue(bsonWriter, encoderContext, value.get(ID_FIELD_NAME));
   }
 }
Пример #3
0
 private void beforeFields(
     final BsonWriter bsonWriter,
     final EncoderContext encoderContext,
     final Map<String, Object> document) {
   if (encoderContext.isEncodingCollectibleDocument() && document.containsKey(ID_FIELD_NAME)) {
     bsonWriter.writeName(ID_FIELD_NAME);
     writeValue(bsonWriter, encoderContext, document.get(ID_FIELD_NAME));
   }
 }
Пример #4
0
 public <G> void writeProperty(
     final BsonWriter writer,
     final String name,
     final G value,
     final TypeToken<G> typeToken,
     final EncoderContext encoderContext) {
   if (value != null) {
     writer.writeName(name);
     writeValue(writer, value, typeToken, encoderContext);
   }
 }
Пример #5
0
  private void writeMap(
      final BsonWriter writer, final Map<String, Object> map, final EncoderContext encoderContext) {
    writer.writeStartDocument();

    beforeFields(writer, encoderContext, map);

    for (final Map.Entry<String, Object> entry : map.entrySet()) {
      if (skipField(encoderContext, entry.getKey())) {
        continue;
      }
      writer.writeName(entry.getKey());
      writeValue(writer, encoderContext, entry.getValue());
    }
    writer.writeEndDocument();
  }
  @Override
  public void encode(
      final BsonWriter writer, final BsonDocument value, final EncoderContext encoderContext) {
    writer.writeStartDocument();

    beforeFields(writer, encoderContext, value);
    for (Map.Entry<String, BsonValue> entry : value.entrySet()) {
      if (skipField(encoderContext, entry.getKey())) {
        continue;
      }

      writer.writeName(entry.getKey());
      writeValue(writer, encoderContext, entry.getValue());
    }

    writer.writeEndDocument();
  }
Пример #7
0
  @Override
  public BsonValue getDocumentId(final Document document) {
    if (!documentHasId(document)) {
      throw new IllegalStateException("The document does not contain an _id");
    }

    Object id = document.get(ID_FIELD_NAME);
    if (id instanceof BsonValue) {
      return (BsonValue) id;
    }

    BsonDocument idHoldingDocument = new BsonDocument();
    BsonWriter writer = new BsonDocumentWriter(idHoldingDocument);
    writer.writeStartDocument();
    writer.writeName(ID_FIELD_NAME);
    writeValue(writer, EncoderContext.builder().build(), id);
    writer.writeEndDocument();
    return idHoldingDocument.get(ID_FIELD_NAME);
  }
  @Override
  public BsonValue getDocumentId(JsonObject json) {
    if (!documentHasId(json)) {
      throw new IllegalStateException("The document does not contain an _id");
    }

    Object id = json.getValue(ID_FIELD);
    if (id instanceof String) {
      return new BsonString((String) id);
    }

    BsonDocument idHoldingDocument = new BsonDocument();
    BsonWriter writer = new BsonDocumentWriter(idHoldingDocument);
    writer.writeStartDocument();
    writer.writeName(ID_FIELD);
    writeValue(writer, null, id, EncoderContext.builder().build());
    writer.writeEndDocument();
    return idHoldingDocument.get(ID_FIELD);
  }