use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class TestDocumentWriter method subdocuments.
@Test
public void subdocuments() {
DocumentWriter writer = new DocumentWriter(getMapper());
document(writer, () -> {
writer.writeName("subdoc");
document(writer, () -> writer.writeInt32("nested", 42));
});
Assert.assertEquals(writer.getDocument(), new Document("subdoc", new Document("nested", 42)));
}
use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class TestDocumentWriter method duplicateKeys.
@Test
public void duplicateKeys() {
DocumentWriter writer = new DocumentWriter(getMapper());
document(writer, () -> {
document(writer, "id", () -> writer.writeInt32("first", 1));
document(writer, "id", () -> writer.writeInt32("second", 2));
});
Document document = (Document) writer.getDocument().get("id");
Assert.assertTrue(document.containsKey("first"), document.toString());
Assert.assertTrue(document.containsKey("second"), document.toString());
}
use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class ReferenceCodec method encode.
@Override
public Object encode(Object value) {
try {
DocumentWriter writer = new DocumentWriter(mapper);
document(writer, () -> {
writer.writeName("ref");
encode(writer, value, EncoderContext.builder().build());
});
return writer.getDocument().get("ref");
} catch (ReferenceException e) {
return value;
}
}
use of dev.morphia.mapping.codec.writer.DocumentWriter in project morphia by mongodb.
the class MorphiaReferenceCodec method encode.
@Override
public Object encode(@Nullable Object value) {
if (value != null) {
MorphiaReference<Object> wrap;
if (value instanceof MorphiaReference) {
wrap = (MorphiaReference<Object>) value;
} else {
wrap = MorphiaReference.wrap(value);
}
DocumentWriter writer = new DocumentWriter(mapper);
document(writer, () -> {
writer.writeName("ref");
encode(writer, wrap, EncoderContext.builder().build());
});
return writer.getDocument().get("ref");
} else {
throw new NullPointerException();
}
}
use of dev.morphia.mapping.codec.writer.DocumentWriter 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);
}
Aggregations