Search in sources :

Example 21 with Codec

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

the class ReferenceCodec method encode.

@Override
public void encode(BsonWriter writer, Object instance, EncoderContext encoderContext) {
    Object idValue = collectIdValues(instance);
    if (idValue != null) {
        final Codec codec = getDatastore().getCodecRegistry().get(idValue.getClass());
        codec.encode(writer, idValue, encoderContext);
    } else {
        throw new ReferenceException(Sofia.noIdForReference());
    }
}
Also used : ReferenceException(dev.morphia.mapping.lazy.proxy.ReferenceException) BaseReferenceCodec(dev.morphia.mapping.codec.BaseReferenceCodec) Codec(org.bson.codecs.Codec)

Example 22 with Codec

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

the class MorphiaCodec method specializePropertyCodecs.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void specializePropertyCodecs() {
    EntityModel entityModel = getEntityModel();
    for (PropertyModel propertyModel : entityModel.getProperties()) {
        Codec<?> specializeCodec = propertyModel.specializeCodec(datastore);
        Codec codec = specializeCodec != null ? specializeCodec : propertyCodecRegistry.get(propertyModel.getTypeData());
        if (codec != null) {
            propertyModel.codec(codec);
        }
    }
}
Also used : Codec(org.bson.codecs.Codec) CollectibleCodec(org.bson.codecs.CollectibleCodec)

Example 23 with Codec

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

the class ObjectCodec method encode.

@Override
public void encode(BsonWriter writer, Object value, EncoderContext encoderContext) {
    final Codec codec = datastore.getCodecRegistry().get(value.getClass());
    codec.encode(writer, value, encoderContext);
}
Also used : Codec(org.bson.codecs.Codec)

Example 24 with Codec

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

the class CodecStructureTest method evaluate.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void evaluate(Document expected, Object value) {
    DocumentWriter writer = new DocumentWriter(getMapper());
    ((Codec) getDs().getCodecRegistry().get(value.getClass())).encode(writer, value, EncoderContext.builder().build());
    Document actual = writer.getDocument();
    assertDocumentEquals(actual, expected);
}
Also used : Codec(org.bson.codecs.Codec) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Document(org.bson.Document)

Example 25 with Codec

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

the class TestBase method toDocument.

protected Document toDocument(Object entity) {
    final Class<?> type = getMapper().getEntityModel(entity.getClass()).getType();
    DocumentWriter writer = new DocumentWriter(getMapper());
    ((Codec) getDs().getCodecRegistry().get(type)).encode(writer, entity, EncoderContext.builder().build());
    return writer.getDocument();
}
Also used : Codec(org.bson.codecs.Codec) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter)

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