use of dev.morphia.mapping.codec.reader.DocumentReader in project morphia by mongodb.
the class MorphiaKeyCursor method fromDocument.
private <I> I fromDocument(Class<I> type, Document document) {
Class<I> aClass = type;
Mapper mapper = datastore.getMapper();
if (document.containsKey(mapper.getOptions().getDiscriminatorKey())) {
aClass = mapper.getClass(document);
}
DocumentReader reader = new DocumentReader(document);
return datastore.getCodecRegistry().get(aClass).decode(reader, DecoderContext.builder().build());
}
use of dev.morphia.mapping.codec.reader.DocumentReader in project morphia by mongodb.
the class DocumentReaderTest method nestedDatabaseRead.
@Test
public void nestedDatabaseRead() {
getDs().getMapper().map(Parent.class, Child.class);
Parent parent = new Parent();
parent.child = new Child();
getDs().save(parent);
MongoCollection<Document> collection = getDocumentCollection(Parent.class);
Document first = collection.find().first();
Parent decode = getDs().getCodecRegistry().get(Parent.class).decode(new DocumentReader(first), DecoderContext.builder().build());
assertEquals(parent, decode);
}
use of dev.morphia.mapping.codec.reader.DocumentReader in project morphia by mongodb.
the class ReferenceCodec method processId.
/**
* Decodes an ID value
*
* @param datastore the Datastore to use
* @param decode the value to decode
* @param decoderContext the decoder context
* @return the decoded value
*/
@NonNull
public static Object processId(Datastore datastore, Object decode, DecoderContext decoderContext) {
Object id = decode;
if (id instanceof Iterable) {
Iterable<?> iterable = (Iterable<?>) id;
List<Object> ids = new ArrayList<>();
for (Object o : iterable) {
ids.add(processId(datastore, o, decoderContext));
}
id = ids;
} else if (id instanceof Document) {
Document document = (Document) id;
if (document.containsKey("$ref")) {
id = processId(datastore, new DBRef(document.getString("$db"), document.getString("$ref"), document.get("$id")), decoderContext);
} else if (document.containsKey(datastore.getMapper().getOptions().getDiscriminatorKey())) {
try {
id = datastore.getCodecRegistry().get(datastore.getMapper().getClass(document)).decode(new DocumentReader(document), decoderContext);
} catch (CodecConfigurationException e) {
throw new MappingException(Sofia.cannotFindTypeInDocument(), e);
}
}
} else if (id instanceof DBRef) {
DBRef ref = (DBRef) id;
Object refId = ref.getId();
if (refId instanceof Document) {
refId = datastore.getCodecRegistry().get(Object.class).decode(new DocumentReader((Document) refId), decoderContext);
}
id = new DBRef(ref.getDatabaseName(), ref.getCollectionName(), refId);
}
return id;
}
use of dev.morphia.mapping.codec.reader.DocumentReader in project morphia by mongodb.
the class LifecycleDecoder method decode.
@Override
@SuppressWarnings("unchecked")
public T decode(BsonReader reader, DecoderContext decoderContext) {
Document document = getMorphiaCodec().getRegistry().get(Document.class).decode(reader, decoderContext);
EntityModel model = getMorphiaCodec().getEntityModel();
if (model.useDiscriminator()) {
String discriminator = document.getString(model.getDiscriminatorKey());
if (discriminator != null) {
Class<?> discriminatorClass = getMorphiaCodec().getDiscriminatorLookup().lookup(discriminator);
// need to load the codec to initialize cachedCodecs in field models
Codec<?> codec = getMorphiaCodec().getRegistry().get(discriminatorClass);
if (codec instanceof MorphiaCodec) {
model = ((MorphiaCodec<?>) codec).getEntityModel();
} else {
throw new CodecConfigurationException(format("Non-entity class used as discriminator: '%s'.", discriminator));
}
}
}
final MorphiaInstanceCreator instanceCreator = model.getInstanceCreator();
T entity = (T) instanceCreator.getInstance();
model.callLifecycleMethods(PreLoad.class, entity, document, getMorphiaCodec().getDatastore());
decodeProperties(new DocumentReader(document), decoderContext, instanceCreator, model);
model.callLifecycleMethods(PostLoad.class, entity, document, getMorphiaCodec().getDatastore());
return entity;
}
use of dev.morphia.mapping.codec.reader.DocumentReader in project morphia by mongodb.
the class TestBase method fromDocument.
protected <T> T fromDocument(Class<T> type, Document document) {
Class<T> aClass = type;
Mapper mapper = getMapper();
if (document.containsKey(mapper.getOptions().getDiscriminatorKey())) {
aClass = mapper.getClass(document);
}
DocumentReader reader = new DocumentReader(document);
return getDs().getCodecRegistry().get(aClass).decode(reader, DecoderContext.builder().build());
}
Aggregations