Search in sources :

Example 16 with BsonDocumentWriter

use of org.bson.BsonDocumentWriter in project immutables by immutables.

the class BsonGeneratorTest method emptyObject.

@Test
void emptyObject() throws IOException {
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    BsonGenerator generator = generatorFor(writer);
    generator.writeStartObject();
    generator.writeEndObject();
    check(writer.getDocument().keySet()).isEmpty();
    check(writer.getDocument()).is(new BsonDocument());
}
Also used : BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test)

Example 17 with BsonDocumentWriter

use of org.bson.BsonDocumentWriter in project immutables by immutables.

the class BsonGeneratorTest method nulls.

/**
 * Writing null values does not cause an exception
 */
@Test
void nulls() throws IOException {
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    BsonGenerator generator = generatorFor(writer);
    generator.writeStartObject();
    generator.writeFieldName("null");
    generator.writeNull();
    generator.writeNullField("nullField");
    generator.writeFieldName("string");
    generator.writeString((String) null);
    generator.writeFieldName("bigDecimal");
    generator.writeNumber((BigDecimal) null);
    generator.writeFieldName("bigInteger");
    generator.writeNumber((BigInteger) null);
    generator.writeEndObject();
    BsonDocument doc = writer.getDocument();
    check(doc.get("null")).is(BsonNull.VALUE);
    check(doc.get("nullField")).is(BsonNull.VALUE);
    check(doc.get("string")).is(BsonNull.VALUE);
    check(doc.get("bigDecimal")).is(BsonNull.VALUE);
    check(doc.get("bigInteger")).is(BsonNull.VALUE);
}
Also used : BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) Test(org.junit.jupiter.api.Test)

Example 18 with BsonDocumentWriter

use of org.bson.BsonDocumentWriter in project immutables by immutables.

the class BsonWriterTest method writeNulls.

/**
 * Check that writing nulls does not cause NPE
 */
@Test
public void writeNulls() throws IOException {
    BsonDocument doc = new BsonDocument();
    BsonWriter writer = new BsonWriter(new BsonDocumentWriter(doc));
    writer.beginObject();
    writer.name("nullString");
    writer.value((String) null);
    writer.name("nullBoolean");
    writer.value((Boolean) null);
    writer.name("nullNumber");
    writer.value((Long) null);
    writer.name("null");
    writer.nullValue();
    writer.endObject();
    writer.flush();
    check(doc.get("nullString")).is(BsonNull.VALUE);
    check(doc.get("nullBoolean")).is(BsonNull.VALUE);
    check(doc.get("nullNumber")).is(BsonNull.VALUE);
    check(doc.get("null")).is(BsonNull.VALUE);
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) Test(org.junit.Test)

Example 19 with BsonDocumentWriter

use of org.bson.BsonDocumentWriter in project immutables by immutables.

the class JavaTimeTest method writeAndReadBson.

private <T> BsonValue writeAndReadBson(T value) throws IOException {
    TypeAdapter<T> adapter = gson.getAdapter((Class<T>) value.getClass());
    BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument());
    writer.writeStartDocument();
    writer.writeName("value");
    adapter.write(Jsons.asGsonWriter(writer), value);
    writer.writeEndDocument();
    BsonValue bson = writer.getDocument().get("value");
    check(bson).notNull();
    return bson;
}
Also used : BsonDocumentWriter(org.bson.BsonDocumentWriter) BsonDocument(org.bson.BsonDocument) BsonValue(org.bson.BsonValue)

Example 20 with BsonDocumentWriter

use of org.bson.BsonDocumentWriter in project immutables by immutables.

the class GsonCodecsTest method dateCodec.

@Test
public void dateCodec() throws IOException {
    TypeAdapter<Date> adapter = GsonCodecs.typeAdapterFromCodec(new DateCodec());
    Date date = new Date();
    BsonDocument doc = new BsonDocument();
    BsonDocumentWriter writer = new BsonDocumentWriter(doc);
    writer.writeStartDocument();
    writer.writeName("$date");
    adapter.write(new BsonWriter(writer), date);
    writer.writeEndDocument();
    check(doc.keySet()).hasSize(1);
    check(doc.get("$date").getBsonType()).is(BsonType.DATE_TIME);
    check(doc.get("$date").asDateTime().getValue()).is(date.getTime());
}
Also used : BsonDocument(org.bson.BsonDocument) BsonDocumentWriter(org.bson.BsonDocumentWriter) DateCodec(org.bson.codecs.DateCodec) Date(java.util.Date) Test(org.junit.Test)

Aggregations

BsonDocument (org.bson.BsonDocument)34 BsonDocumentWriter (org.bson.BsonDocumentWriter)34 Test (org.junit.Test)13 BsonWriter (org.bson.BsonWriter)9 SingleMapReaderImpl (org.apache.drill.exec.vector.complex.impl.SingleMapReaderImpl)6 BsonDocumentReader (org.bson.BsonDocumentReader)6 FieldReader (org.apache.drill.exec.vector.complex.reader.FieldReader)4 BsonValue (org.bson.BsonValue)4 BsonDocumentCodec (org.bson.codecs.BsonDocumentCodec)4 BsonValueCodecProvider (org.bson.codecs.BsonValueCodecProvider)4 ValueCodecProvider (org.bson.codecs.ValueCodecProvider)4 Test (org.junit.jupiter.api.Test)4 BaseTest (org.apache.drill.test.BaseTest)3 BsonInt32 (org.bson.BsonInt32)2 Codec (org.bson.codecs.Codec)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 ChangeStreamDocument (com.mongodb.client.model.changestream.ChangeStreamDocument)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1