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