示例#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 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();
  }
  @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);
  }