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