Java Code Examples for org.bson.codecs.Codec#encode()

The following examples show how to use org.bson.codecs.Codec#encode() . 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: SetEntityOperator.java    From morphia with Apache License 2.0 6 votes vote down vote up
@Override
public OperationTarget toTarget(final PathTarget pathTarget) {
    return new OperationTarget(null, value()) {
        @Override
        public Object encode(final Mapper mapper) {
            MappedClass mappedClass = mapper.getMappedClass(getValue().getClass());
            if (mappedClass.getVersionField() == null) {
                return super.encode(mapper);
            }

            Codec codec = mapper.getCodecRegistry().get(getValue().getClass());
            DocumentWriter writer = new DocumentWriter();

            codec.encode(writer, getValue(), EncoderContext.builder().build());

            Document document = writer.getDocument();
            document.remove(mappedClass.getVersionField().getMappedFieldName());
            return document;
        }
    };
}
 
Example 2
Source File: MongoSession.java    From immutables with Apache License 2.0 5 votes vote down vote up
/**
 * Convert generic object to a BsonValue
 */
private BsonValue toBsonValue(Object value) {
  BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
  writer.writeStartDocument();
  writer.writeName("value");
  Codec<Object> codec = (Codec<Object>) collection.getCodecRegistry().get(value.getClass());
  codec.encode(writer, value, EncoderContext.builder().build());
  writer.writeEndDocument();
  return writer.getDocument().get("value");
}
 
Example 3
Source File: ArrayCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public void encode(final BsonWriter writer, final Object value, final EncoderContext encoderContext) {
    writer.writeStartArray();
    int length = Array.getLength(value);
    for (int i = 0; i < length; i++) {
        Object element = Array.get(value, i);
        Codec codec = mapper.getCodecRegistry().get(element.getClass());
        codec.encode(writer, element, encoderContext);
    }
    writer.writeEndArray();
}
 
Example 4
Source File: ReferenceCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(final BsonWriter writer, final Object instance, final EncoderContext encoderContext) {
    Object idValue = collectIdValues(instance);

    if (idValue != null) {
        final Codec codec = getDatastore().getMapper().getCodecRegistry().get(idValue.getClass());
        codec.encode(writer, idValue, encoderContext);
    } else {
        throw new ReferenceException(Sofia.noIdForReference());
    }
}
 
Example 5
Source File: KeyCodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(final BsonWriter writer, final Key value, final EncoderContext encoderContext) {
    writer.writeStartDocument();
    String collection = value.getCollection();
    if (collection == null) {
        collection = mapper.getMappedClass(value.getType()).getCollectionName();
    }
    writer.writeString("$ref", collection);
    writer.writeName("$id");
    Codec codec = mapper.getCodecRegistry().get(value.getId().getClass());
    codec.encode(writer, value.getId(), encoderContext);
    writer.writeEndDocument();
}
 
Example 6
Source File: Mapper.java    From morphia with Apache License 2.0 5 votes vote down vote up
/**
 * Converts an entity (POJO) to a Document.  A special field will be added to keep track of the class type.
 *
 * @param entity The POJO
 * @return the Document
 * @morphia.internal
 */
public Document toDocument(final Object entity) {

    final MappedClass mc = getMappedClass(entity.getClass());

    DocumentWriter writer = new DocumentWriter();
    Codec codec = getCodecRegistry().get(mc.getType());
    codec.encode(writer, entity,
        EncoderContext.builder()
                      .build());

    return writer.getDocument();
}
 
Example 7
Source File: GeoWithinFilter.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public final void encode(final Mapper mapper, final BsonWriter writer, final EncoderContext context) {
    writer.writeStartDocument(field(mapper));
    writer.writeStartDocument(getFilterName());
    writer.writeName("$geometry");

    Object shape = getValue();
    Codec codec = mapper.getCodecRegistry().get(shape.getClass());
    codec.encode(writer, shape, context);

    writer.writeEndDocument();
    writer.writeEndDocument();
}
 
Example 8
Source File: OperationTarget.java    From morphia with Apache License 2.0 5 votes vote down vote up
/**
 * Encodes this target
 * @param mapper the mapper
 * @return the encoded form
 * @morphia.internal
 */
public Object encode(final Mapper mapper) {
    if (target == null) {
        return value;
    }
    MappedField mappedField = this.target.getTarget();
    Object mappedValue = value;

    FieldModel<?> model = mappedField != null
                          ? mappedField.getDeclaringClass()
                                       .getEntityModel()
                                       .getFieldModelByName(mappedField.getJavaFieldName())
                          : null;

    Codec cachedCodec = model != null && !(mappedValue instanceof LegacyQuery)
                        ? model.getCachedCodec()
                        : null;
    if (cachedCodec instanceof PropertyHandler) {
        mappedValue = ((PropertyHandler) cachedCodec).encode(mappedValue);
    } else if (mappedValue != null) {
        Codec codec = mapper.getCodecRegistry().get(mappedValue.getClass());
        DocumentWriter writer = new DocumentWriter();
        writer.writeStartDocument();
        writer.writeName("mapped");
        codec.encode(writer, mappedValue, EncoderContext.builder().build());
        writer.writeEndDocument();
        mappedValue = writer.getDocument().get("mapped");
    }
    return new Document(target.translatedPath(), mappedValue);
}
 
Example 9
Source File: ReactiveMongoOperations.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private static BsonDocument getBsonDocument(ReactiveMongoCollection collection, Object entity) {
    BsonDocument document = new BsonDocument();
    Codec codec = collection.getCodecRegistry().get(entity.getClass());
    codec.encode(new BsonDocumentWriter(document), entity, EncoderContext.builder().build());
    return document;
}
 
Example 10
Source File: MongoOperations.java    From quarkus with Apache License 2.0 4 votes vote down vote up
private static BsonDocument getBsonDocument(MongoCollection collection, Object entity) {
    BsonDocument document = new BsonDocument();
    Codec codec = collection.getCodecRegistry().get(entity.getClass());
    codec.encode(new BsonDocumentWriter(document), entity, EncoderContext.builder().build());
    return document;
}
 
Example 11
Source File: MorphiaReferenceCodec.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(final BsonWriter writer, final MorphiaReference value, final EncoderContext encoderContext) {
    Object ids = value.getId(mapper, getDatastore(), getFieldMappedClass());
    Codec codec = mapper.getCodecRegistry().get(ids.getClass());
    codec.encode(writer, ids, encoderContext);
}
 
Example 12
Source File: ObjectCodec.java    From morphia with Apache License 2.0 4 votes vote down vote up
@Override
public void encode(final BsonWriter writer, final Object value, final EncoderContext encoderContext) {
    final Codec codec = mapper.getCodecRegistry().get(value.getClass());
    codec.encode(writer, value, encoderContext);
}