Using Jongo only for Object Mapping

54 views
Skip to first unread message

Christian F

unread,
Feb 6, 2017, 12:27:15 PM2/6/17
to Jongo
Hello,
I am using the official java mongodb driver with some minimalistic inhouse framework. Sadly I cannot use jongo for accessing the mongodb. But I would like to use Jongo for mapping Objects into DBObjects and back.

Do you know, if that is possible?
I only need methods like 
DBObject writeDBObject(Object) 
and 
Object readDBObject(DBObject)

I tried to use jackson. But then I have to write some custom mapping for Dates and Ids and so on. 

Thank you very much,
Chris

Christian F

unread,
Feb 7, 2017, 7:31:24 AM2/7/17
to Jongo
Hello,
I am doing this now:
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

Benoît Guérout

unread,
Feb 14, 2017, 6:03:09 AM2/14/17
to jongo...@googlegroups.com
Hello,

Jongo 1.4 is pending since few months but this version should allow Jongo to deal with native mongo driver : 


Let me know if this is what you need.



--
You received this message because you are subscribed to the Google Groups "Jongo" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jongo-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages