Example usage for org.springframework.data.mongodb.core.convert MongoConverter write

List of usage examples for org.springframework.data.mongodb.core.convert MongoConverter write

Introduction

In this page you can find the example usage for org.springframework.data.mongodb.core.convert MongoConverter write.

Prototype

void write(T source, S sink);

Source Link

Usage

From source file:org.oncoblocks.centromere.mongodb.ImportUtils.java

/**
 * Serializes an object into a string format that can be inserted into a MongoDB collection.
 * //from   w w w . j  av  a 2 s.  c om
 * @param entity
 * @return
 */
public String convertEntityToJson(Object entity) {
    MongoConverter converter = mongoTemplate.getConverter();
    DBObject dbObject = new BasicDBObject();
    converter.write(entity, dbObject);
    if (dbObject.containsField("_id") && dbObject.get("_id") == null) {
        dbObject.removeField("_id");
    }
    if (dbObject.containsField("_class")) {
        dbObject.removeField("_class");
    }
    return dbObject.toString();
}