Пример #1
0
 private void writeIterable(
     final BsonWriter writer, final Iterable<Object> list, final EncoderContext encoderContext) {
   writer.writeStartArray();
   for (final Object value : list) {
     writeValue(writer, encoderContext, value);
   }
   writer.writeEndArray();
 }
Пример #2
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();
  }
 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));
   }
 }
 @Override
 protected void writeDateTime(BsonWriter writer, String name, Object value, EncoderContext ctx) {
   writer.writeDateTime(
       OffsetDateTime.parse(((JsonObject) value).getString(DATE_FIELD))
           .toInstant()
           .toEpochMilli());
 }
  @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();
  }
Пример #6
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));
   }
 }
Пример #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);
  }
Пример #9
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);
   }
 }
Пример #10
0
 private <G> void writeValue(
     final BsonWriter writer,
     final G value,
     final TypeToken<G> typeToken,
     final EncoderContext encoderContext) {
   if (value == null) {
     writer.writeNull();
   } else {
     TypeSafeCodec<G> codec = vertigoCodecRegistry.lookupCodec(typeToken);
     codec.encode(writer, value, typeToken, vertigoCodecRegistry, encoderContext);
   }
 }
Пример #11
0
 @SuppressWarnings({"unchecked", "rawtypes"})
 private void writeValue(
     final BsonWriter writer, final EncoderContext encoderContext, final Object value) {
   if (value == null) {
     writer.writeNull();
   } else if (value instanceof Iterable) {
     writeIterable(writer, (Iterable<Object>) value, encoderContext.getChildContext());
   } else if (value instanceof Map) {
     writeMap(writer, (Map<String, Object>) value, encoderContext.getChildContext());
   } else {
     Codec codec = registry.get(value.getClass());
     encoderContext.encodeWithChildContext(codec, writer, value);
   }
 }
Пример #12
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();
  }