Search in sources :

Example 11 with Codec

use of org.bson.codecs.Codec in project morphia by mongodb.

the class KeyCodec method encode.

@Override
public void encode(BsonWriter writer, Key value, EncoderContext encoderContext) {
    document(writer, () -> {
        String collection = value.getCollection();
        if (collection == null) {
            collection = datastore.getMapper().getEntityModel(value.getType()).getCollectionName();
        }
        writer.writeString("$ref", collection);
        writer.writeName("$id");
        Codec codec = datastore.getCodecRegistry().get(value.getId().getClass());
        codec.encode(writer, value.getId(), encoderContext);
    });
}
Also used : Codec(org.bson.codecs.Codec)

Example 12 with Codec

use of org.bson.codecs.Codec in project morphia by mongodb.

the class MorphiaMapCodec method encode.

@Override
public void encode(BsonWriter writer, Map map, EncoderContext encoderContext) {
    document(writer, () -> {
        for (Entry<?, ?> entry : ((Map<?, ?>) map).entrySet()) {
            final Object key = entry.getKey();
            writer.writeName(Conversions.convert(key, String.class));
            if (entry.getValue() == null) {
                writer.writeNull();
            } else {
                Codec codec = datastore.getCodecRegistry().get(entry.getValue().getClass());
                codec.encode(writer, entry.getValue(), encoderContext);
            }
        }
    });
}
Also used : Codec(org.bson.codecs.Codec) MapCodec(org.bson.codecs.MapCodec) Map(java.util.Map)

Example 13 with Codec

use of org.bson.codecs.Codec in project mongo-java-driver by mongodb.

the class GeometryCollectionCodec method encodeGeometry.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void encodeGeometry(final BsonWriter writer, final Geometry geometry, final EncoderContext encoderContext) {
    Codec codec = registry.get(geometry.getClass());
    encoderContext.encodeWithChildContext(codec, writer, geometry);
}
Also used : Codec(org.bson.codecs.Codec)

Example 14 with Codec

use of org.bson.codecs.Codec in project morphia by mongodb.

the class OperationTarget method encode.

/**
 * Encodes this target
 *
 * @param datastore the datastore
 * @return the encoded form
 * @morphia.internal
 */
public Object encode(Datastore datastore) {
    if (target == null) {
        if (value == null) {
            throw new NullPointerException();
        }
        return value;
    }
    PropertyModel mappedField = this.target.getTarget();
    Object mappedValue = value;
    PropertyModel model = mappedField != null ? mappedField.getEntityModel().getProperty(mappedField.getName()) : null;
    Codec cachedCodec = null;
    if (model != null && !(mappedValue instanceof LegacyQuery)) {
        cachedCodec = model.specializeCodec(datastore);
    }
    if (cachedCodec instanceof PropertyHandler) {
        mappedValue = ((PropertyHandler) cachedCodec).encode(mappedValue);
    } else {
        DocumentWriter writer = new DocumentWriter(datastore.getMapper());
        Object finalMappedValue = mappedValue;
        document(writer, () -> value(datastore, writer, "mapped", finalMappedValue, EncoderContext.builder().build()));
        mappedValue = writer.getDocument().get("mapped");
    }
    return new Document(target.translatedPath(), mappedValue);
}
Also used : Codec(org.bson.codecs.Codec) PropertyHandler(dev.morphia.mapping.codec.pojo.PropertyHandler) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) PropertyModel(dev.morphia.mapping.codec.pojo.PropertyModel) Document(org.bson.Document)

Example 15 with Codec

use of org.bson.codecs.Codec in project morphia by mongodb.

the class Filter method writeUnnamedValue.

protected void writeUnnamedValue(@Nullable Object value, Datastore datastore, BsonWriter writer, EncoderContext encoderContext) {
    if (value != null) {
        Codec codec = datastore.getCodecRegistry().get(value.getClass());
        encoderContext.encodeWithChildContext(codec, writer, value);
    } else {
        writer.writeNull();
    }
}
Also used : Codec(org.bson.codecs.Codec)

Aggregations

Codec (org.bson.codecs.Codec)28 DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)5 Document (org.bson.Document)4 Stage (dev.morphia.aggregation.experimental.stages.Stage)3 Map (java.util.Map)3 BaseReferenceCodec (dev.morphia.mapping.codec.BaseReferenceCodec)2 ReferenceException (dev.morphia.mapping.lazy.proxy.ReferenceException)2 List (java.util.List)2 BsonDocument (org.bson.BsonDocument)2 BsonDocumentWriter (org.bson.BsonDocumentWriter)2 BsonTypeClassMap (org.bson.codecs.BsonTypeClassMap)2 CollectibleCodec (org.bson.codecs.CollectibleCodec)2 CoordinateReferenceSystem (com.mongodb.client.model.geojson.CoordinateReferenceSystem)1 ExpressionList (dev.morphia.aggregation.experimental.expressions.impls.ExpressionList)1 PropertyHandler (dev.morphia.mapping.codec.pojo.PropertyHandler)1 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1