Search in sources :

Example 16 with DocumentWriter

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)));
}
Also used : DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 17 with DocumentWriter

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());
}
Also used : DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Document(org.bson.Document) Test(org.testng.annotations.Test)

Example 18 with DocumentWriter

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;
    }
}
Also used : ReferenceException(dev.morphia.mapping.lazy.proxy.ReferenceException) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter)

Example 19 with DocumentWriter

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();
    }
}
Also used : DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter)

Example 20 with DocumentWriter

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);
}
Also used : Codec(org.bson.codecs.Codec) DocumentWriter(dev.morphia.mapping.codec.writer.DocumentWriter) Document(org.bson.Document)

Aggregations

DocumentWriter (dev.morphia.mapping.codec.writer.DocumentWriter)23 Document (org.bson.Document)18 Test (org.testng.annotations.Test)9 Codec (org.bson.codecs.Codec)5 Datastore (dev.morphia.Datastore)3 PropertyModel (dev.morphia.mapping.codec.pojo.PropertyModel)2 OperationTarget (dev.morphia.query.OperationTarget)2 Filter (dev.morphia.query.experimental.filters.Filter)2 Sample (dev.morphia.aggregation.experimental.stages.Sample)1 Skip (dev.morphia.aggregation.experimental.stages.Skip)1 Stage (dev.morphia.aggregation.experimental.stages.Stage)1 EntityModel (dev.morphia.mapping.codec.pojo.EntityModel)1 PropertyHandler (dev.morphia.mapping.codec.pojo.PropertyHandler)1 ReferenceException (dev.morphia.mapping.lazy.proxy.ReferenceException)1 NearFilter (dev.morphia.query.experimental.filters.NearFilter)1 User (dev.morphia.test.models.User)1 ArrayList (java.util.ArrayList)1 EncoderContext (org.bson.codecs.EncoderContext)1 CodecRegistry (org.bson.codecs.configuration.CodecRegistry)1