Mongo Java: How to serialize DBObject as JSON on file?

daydreamer picture daydreamer · Jul 23, 2012 · Viewed 20.3k times · Source

I have a document in MongoDB as

name: name
date_created: date
p_vars: {
   01: {
      a: a,
      b: b,
   }
   02: {
      a: a,
      b: b,
   }
   ....
}

represented as DBObject

  • All key, value pairs are of type String
  • I want to serialize this document using Java, Looking at the api, I did not find anything, How can I serialize a DBObject as JSON on file?

Answer

Parvin Gasimzade picture Parvin Gasimzade · Aug 24, 2012

It seems that BasicDBObject's toString() method returns the JSON serialization of the object.