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());
}
}
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);
}
}
}
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);
}
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);
}
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();
}
Aggregations