org.bson.BsonDocumentWriter Java Examples
The following examples show how to use
org.bson.BsonDocumentWriter.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BsonWriterTest.java From immutables with Apache License 2.0 | 6 votes |
/** * 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); }
Example #2
Source File: EntityEncoderBuilderTest.java From core-ng-project with Apache License 2.0 | 5 votes |
@Test void encode() { assertThat(builder.enumCodecFields.keySet()).containsExactly(TestEnum.class); TestEntity entity = new TestEntity(); entity.id = new ObjectId("5627b47d54b92d03adb9e9cf"); entity.booleanField = Boolean.TRUE; entity.longField = 325L; entity.stringField = "string"; entity.zonedDateTimeField = ZonedDateTime.of(LocalDateTime.of(2016, 9, 1, 11, 0, 0), ZoneId.of("America/New_York")); entity.child = new TestChildEntity(); entity.child.enumField = TestEnum.ITEM1; entity.child.enumListField = List.of(TestEnum.ITEM2); entity.listField = List.of("V1", "V2"); entity.mapField = Map.of("K1", "V1", "K2", "V2"); entity.enumMapField = Map.of(TestEnum.ITEM1, "V1"); entity.mapListField = Map.of("K1", List.of("V1"), "K2", List.of("V2", "V3")); var bson = new BsonDocument(); try (var writer = new BsonDocumentWriter(bson)) { encoder.encode(writer, entity); } var expectedBSON = new BsonDocument(); try (var writer = new BsonDocumentWriter(expectedBSON)) { writer.pipe(new JsonReader(ClasspathResources.text("mongo-test/entity.json"))); } assertThat(bson).isEqualTo(expectedBSON); }
Example #3
Source File: Support.java From immutables with Apache License 2.0 | 5 votes |
BsonValue toBson() { BsonDocument bson = new BsonDocument(); org.bson.BsonWriter writer = new BsonDocumentWriter(bson); // Bson doesn't allow to write directly scalars / primitives, they have to be embedded in a // document. writer.writeStartDocument(); writer.writeName("$"); encoder.encode(writer, value, EncoderContext.builder().build()); writer.writeEndDocument(); writer.flush(); return bson.get("$"); }
Example #4
Source File: GsonCodecsTest.java From immutables with Apache License 2.0 | 5 votes |
@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()); }
Example #5
Source File: BsonReaderTest.java From immutables with Apache License 2.0 | 5 votes |
/** * Tests direct bson and gson mappings */ @Test public void gsonToBson() throws Exception { JsonObject obj = new JsonObject(); obj.addProperty("boolean", true); obj.addProperty("int32", 32); obj.addProperty("int64", 64L); obj.addProperty("double", 42.42D); obj.addProperty("string", "foo"); obj.add("null", JsonNull.INSTANCE); obj.add("array", new JsonArray()); obj.add("object", new JsonObject()); BsonDocument doc = Jsons.toBson(obj); TypeAdapters.JSON_ELEMENT.write(new BsonWriter(new BsonDocumentWriter(doc)), obj); check(doc.keySet()).notEmpty(); check(doc.get("boolean").getBsonType()).is(BsonType.BOOLEAN); check(doc.get("boolean").asBoolean()); check(doc.get("int32").getBsonType()).is(BsonType.INT32); check(doc.get("int32").asInt32().getValue()).is(32); check(doc.get("int64").getBsonType()).is(BsonType.INT64); check(doc.get("int64").asInt64().getValue()).is(64L); check(doc.get("double").getBsonType()).is(BsonType.DOUBLE); check(doc.get("double").asDouble().getValue()).is(42.42D); check(doc.get("string").getBsonType()).is(BsonType.STRING); check(doc.get("string").asString().getValue()).is("foo"); check(doc.get("null").getBsonType()).is(BsonType.NULL); check(doc.get("null").isNull()); check(doc.get("array").getBsonType()).is(BsonType.ARRAY); check(doc.get("array").asArray()).isEmpty(); check(doc.get("object").getBsonType()).is(BsonType.DOCUMENT); check(doc.get("object").asDocument().keySet()).isEmpty(); }
Example #6
Source File: JavaTimeTest.java From immutables with Apache License 2.0 | 5 votes |
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; }
Example #7
Source File: MongoSession.java From immutables with Apache License 2.0 | 5 votes |
/** * Convert generic object to a BsonValue */ private BsonValue toBsonValue(Object value) { BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument()); writer.writeStartDocument(); writer.writeName("value"); Codec<Object> codec = (Codec<Object>) collection.getCodecRegistry().get(value.getClass()); codec.encode(writer, value, EncoderContext.builder().build()); writer.writeEndDocument(); return writer.getDocument().get("value"); }
Example #8
Source File: BsonGeneratorTest.java From immutables with Apache License 2.0 | 5 votes |
@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()); }
Example #9
Source File: BsonGeneratorTest.java From immutables with Apache License 2.0 | 5 votes |
@Test void binary() throws IOException { BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument()); BsonGenerator generator = generatorFor(writer); check(generator.canWriteBinaryNatively()); check(writeAndReturnValue(gen -> gen.writeBinary(new byte[] {})).asBinary().getData()).isEmpty(); check(writeAndReturnValue(gen -> gen.writeBinary(new byte[] {1})).asBinary().getData()).isOf((byte) 1); check(writeAndReturnValue(gen -> gen.writeBinary(new byte[] {1, 2})).asBinary().getData()).isOf((byte) 1, (byte) 2); check(writeAndReturnValue(gen -> gen.writeBinary(new byte[] {1, 2, 3})).asBinary().getData()).isOf((byte) 1, (byte) 2, (byte) 3); }
Example #10
Source File: BsonGeneratorTest.java From immutables with Apache License 2.0 | 5 votes |
@Test void checkClosed() throws IOException { BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument()); BsonGenerator generator = generatorFor(writer); check(!generator.isClosed()); generator.close(); check(generator.isClosed()); }
Example #11
Source File: BsonGeneratorTest.java From immutables with Apache License 2.0 | 5 votes |
/** * 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); }
Example #12
Source File: BsonGeneratorTest.java From immutables with Apache License 2.0 | 5 votes |
private static BsonValue writeAndReturnValue(IoConsumer<BsonGenerator> consumer) throws IOException { BsonDocumentWriter writer = new BsonDocumentWriter(new BsonDocument()); BsonGenerator generator = generatorFor(writer); generator.writeStartObject(); generator.writeFieldName("value"); consumer.accept(generator); generator.writeEndObject(); BsonDocument doc = writer.getDocument(); check(doc.keySet()).has("value"); return doc.get("value"); }
Example #13
Source File: ReactiveMongoOperations.java From quarkus with Apache License 2.0 | 4 votes |
private static BsonDocument getBsonDocument(ReactiveMongoCollection collection, Object entity) { BsonDocument document = new BsonDocument(); Codec codec = collection.getCodecRegistry().get(entity.getClass()); codec.encode(new BsonDocumentWriter(document), entity, EncoderContext.builder().build()); return document; }
Example #14
Source File: MongoOperations.java From quarkus with Apache License 2.0 | 4 votes |
private static BsonDocument getBsonDocument(MongoCollection collection, Object entity) { BsonDocument document = new BsonDocument(); Codec codec = collection.getCodecRegistry().get(entity.getClass()); codec.encode(new BsonDocumentWriter(document), entity, EncoderContext.builder().build()); return document; }