org.bson.json.JsonMode Java Examples

The following examples show how to use org.bson.json.JsonMode. 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: BsonToJsonLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenBsonDocument_whenUsingRelaxedJsonTransformation_thenJsonDateIsObjectIsoDate() {
   
    String json = null;
    try (MongoClient mongoClient = new MongoClient()) {
        MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME);
        Document bson = mongoDatabase.getCollection("Books").find().first();
        json = bson.toJson(JsonWriterSettings
            .builder()
            .outputMode(JsonMode.RELAXED)
            .build());
    }
    
    String expectedJson = "{\"_id\": \"isbn\", " + 
        "\"className\": \"com.baeldung.bsontojson.Book\", " + 
        "\"title\": \"title\", " + 
        "\"author\": \"author\", " + 
        "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " + 
        "\"name\": \"publisher\"}, " + 
        "\"price\": 3.95, " + 
        "\"publishDate\": {\"$date\": \"2020-01-01T17:13:32Z\"}}";

    assertNotNull(json);
    
    assertEquals(expectedJson, json);
}
 
Example #2
Source File: MongoDbTable.java    From beam with Apache License 2.0 5 votes vote down vote up
@DoFn.ProcessElement
public void processElement(ProcessContext context) {
  context.output(
      context
          .element()
          .toJson(JsonWriterSettings.builder().outputMode(JsonMode.RELAXED).build()));
}
 
Example #3
Source File: JsonBsonDocumentWriter.java    From mongowp with Apache License 2.0 5 votes vote down vote up
@Override
public void writeInto(StringBuilder sink, BsonDocument doc) {
  String json = MongoBsonTranslator.translate(doc)
      .toJson(new JsonWriterSettings(JsonMode.STRICT));

  sink.append(json);
}
 
Example #4
Source File: JsonBsonDocumentWriter.java    From mongowp with Apache License 2.0 4 votes vote down vote up
public String writeIntoString(BsonDocument doc) {
  return MongoBsonTranslator.translate(doc)
      .toJson(new JsonWriterSettings(JsonMode.STRICT));
}