final ObjectMapper mapper = new ObjectMapper();
final StringWriter jsonOutput = new StringWriter();
try {
mapper.writeValue(jsonOutput, object);
final DBObject dbObject = (DBObject) JSON.parse(jsonOutput.toString());
final Identifiable m = (Identifiable) object;
if (m.getID() != null) {
db.getCollection(nameSpace).findAndModify(new BasicDBObject("_id", m.getID()), dbObject);
} else {
final ObjectId id = new ObjectId();
dbObject.put("_id", id);
db.getCollection(nameSpace).save(dbObject);
m.setID(id);
}
} catch (final Exception e) {
e.printStackTrace();
}
}