You can use the MongoDB driver for Java to store the BSON object, and then convert it to String , which can then be wrapped using JSONObject .
For example, here's how I create a regular document:
BasicDBObject obj = new BasicDBObject(); obj.put("name", "Matt"); obj.put("date", new Date());
Then, to get the String representation of the object, simply call:
String bsonString = obj.toString();
Wrap it with JSONObject and get the date attribute, which should return it in a BSON compatible format.
JSONObject newObject = new JSONObject(bsonString); System.out.println(newObject.get("date"));
The resulting result looks something like this:
{"$date":"2012-08-10T05:22:53.872Z"}
Matt Quiros Jul 28 '12 at 10:30 2012-07-28 10:30
source share