private static final Mapper JONGO_MAPPER = new JacksonMapper.Builder().build();
public static <T> Document encode(final T pojo) {
// TODO: when jongo adds mongo 3.0 support adjust this statement - can be much shorter
return Document.parse(JSON.serialize(JONGO_MAPPER.getMarshaller().marshall(pojo).toDBObject()));
}
public static <T> T decode(final Document document, final Class<T> clazz) {
// TODO: when jongo adds mongo 3.0 support adjust this statement - can be much shorter
return JONGO_MAPPER.getUnmarshaller().unmarshall(Bson.createDocument((DBObject) JSON.parse(document.toJson())), clazz);
}
Now I can transform a pojo into a mongodb Document.
Sadly Jongo currently does not support Documents, only the old DBObjects. So I needed to transform the DBObject to json and then into a document.
If there is a better solution, it would be very nice to know.
Thank you,
Chris