title: How to deserialize json document obtained from mongoDB into a POJO?... (Migrating from Morphia to Java Mongo Driver 3.0)
body: <p><strong>BACKGROUND</strong></p>
  
  <p>Initially, Morphia was being used to interact with a mongo database. This was fine and worked nicely, but there was one caveat... Some mapping exception was being spilled and clogging the logs. <a href="https://stackoverflow.com/questions/27939445/how-to-fix-this-morphia-mapping-issue-warning-org-mongodb-morphia-mappi">See my Unresolved Question on that particular issue</a>. &lt;=== This didn't stop the application from functioning correctly. Morphia helped in getting the documents mapped to their corresponding entities (<em>basically POJOs</em>) and these were easily rendered to the Views.</p>
  
  <p>However, after much deliberation and because that exception issue wasnt resolved, and thanks to the MongoDB Java Driver 3.0's new features, I have decided to move off from Morphia to MongoDB-Java-Driver3.0(MJD3.0)</p>
  
  <p><hr>
  On inspecting Documents <em>(previously stored by morphia)</em> I can see:</p>
  
  <pre><code>"_id" : ObjectId("55706df5e4b00146ea69622b"),
  "className" : "packageName.NewsCategory",
  "categoryName" : "Maritime",
  "uid" : NumberLong(11),
  "createdAt" : ISODate("2015-06-04T15:25:41.096Z"),
  "updatedAt" : ISODate("2015-06-04T15:25:41.096Z"),
  "active" : true
  </code></pre>
  
  <p>Also, the model, <strong><code>NewsCategory</code></strong>, looks something like this;</p>
  
  <pre><code>public class NewsCategory{
      private ObjectId id;
      private String categoryName;
      private Long uid;
      private Date createdAt;
      private Date updatedAt;
      private boolean active;
  
      //getters...
      //setters...
  }
  </code></pre>
  
  <p><hr>
  <strong>MY STRATEGY</strong></p>
  
  <p>Because I was formerly using Morphia, the Models (as POJOs) were being passed as attributes to the Views <strong><em>( .vm - velocity templates )</em></strong> where values are being read from the fields as: <strong><code>$!newsCategory.id</code></strong>, <strong><code>$!newsCategory.categoryName</code></strong>, <strong><code>$!newsCategory.createdAt</code></strong> e.g</p>
  
  <pre><code>&lt;a href="/news/categories/$!newsCategory.id"&gt; Edit &lt;/a&gt;
  </code></pre>
  
  <p>Now, because <strong><em>I do not want to modify the Servelet and/or View Layer</em></strong>, I ensure that in the business Layer, the <code>json</code> string gets deserialized into the corresponding Model using <code>gson</code>;</p>
  
  <p><code>NewsCategory newsCategory = new GsonBuilder().create().fromJson(json, NewsCategory.class);</code></p>
  
  <p><hr>
  <strong>CHALLENGE - 1</strong></p>
  
  <p>With <code>document</code> Type being <code>org.bson.Document</code> and using MongoDB-Java-Driver3.0 (MJD3.0), The <strong><code>NewsCategory</code></strong> document is correctly retrieved from MongoDB, no problem, and when I call </p>
  
  <p><code>String json = document.toJson();</code> ...here's what the json structure looks like; </p>
  
  <pre><code>{
      "_id": {
          "$oid": "54ad4568e451500a45f60200"
      },
      "className": "packageName.NewsCategory",
      "categoryName": "General",
      "uid": {
          "$numberLong": "1"
      },
      "createdAt": {
          "$date": 1420641640457
      },
      "updatedAt": {
          "$date": 1429285029337
      },
      "active": true
  }
  </code></pre>
  
  <p><hr>
  <strong>THE PROBLEM</strong> </p>
  
  <p>This <code>json</code> structure derived from <code>document.toJson()</code> cannot be easily deserialized to the corresponding <strong><code>NewsCategory</code></strong> model using my earlier described strategy, thanks to those field types present in the format.</p>
  
  <p><hr>
  <strong>CHALLENGE - 2</strong></p>
  
  <p>I directly converted the <code>document</code> to json using <code>gson</code> as follows;</p>
  
  <p><code>String json = new GsonBuilder().create().toJson(document);</code> ...and here's what the json structure looks like;</p>
  
  <pre><code>{
      "_id": {
          "timestamp": 1420641640,
          "machineIdentifier": 14963024,
          "processIdentifier": 2629,
          "counter": 16122368
      },
      "className": "packageName.NewsCategory",
      "categoryName": "General",
      "uid": 1,
      "createdAt": "Jan 7, 2015 3:40:40 PM",
      "updatedAt": "Apr 17, 2015 4:37:09 PM",
      "active": true
  }
  </code></pre>
  
  <p>This is way more desirable, and I can get the <strong><code>NewsCategory</code></strong> object using my usual strategy as;</p>
  
  <p><code>NewsCategory newsCategory = new GsonBuilder().create().fromJson(json, NewsCategory.class);</code></p>
  
  <p><hr>
  <strong>ANOTHER PROBLEM THOUGH</strong></p>
  
  <p>The <code>_id</code> field has been broken into it's <code>ObjectId</code> components, hence the id cannot be retrieved as <code>newsCategory.id</code> or even <code>newsCategory._id</code>. </p>
  
  <p>However while I was formerly using morphia, it could simply be retrieved by just using <code>newsCategory.id</code></p>
  
  <p><hr>
  <strong>MAIN QUESTIONS</strong></p>
  
  <p>What is the appropriate way to deserialize these json strings into the corresponding <strong><code>NewsCategory</code></strong> Model while still ensuring that all the necessary fields are still normally accessible in the Views using the regular dot(.) annotations (<strong><em>especially that <code>id</code> field</em></strong>)?</p>
  
  <p>An approach for both <strong>CHALLENGES 1 &amp; 2</strong> described above would be highly appreciated.</p>
  
  <p>Thanks!</p>
  
  <p><strong><em>EDIT:</em></strong> </p>
  
  <p>I've tried @cichystefan's approach and the challenge still remains how to access the value of the <code>_id</code> field </p>
  
  <p>With <code>newsCategory</code> now being of type <code>org.bson.Document</code>, for quite obvious reasons, <code>$!newsCategory.id</code> is apparently either empty or null, because when I inspect an html section in the view defined as <code>&lt;a href="/news/categories/$!newsCategory.id"&gt;</code> in google chrome here's what I see... <code>&lt;a href="/news/categories/"&gt;</code>... </p>
  
  <p>Now, instinct says to fetch the <code>_id</code> part by doing <code>$!newsCategory._id</code> but this just spills the whole document as a string and appends the <code>._id</code> part at the end of the string. </p>
  
  <p><code>&lt;a href="/news/categories/Document{{_id=54ad4568e451500a45f60200, className=packageName.NewsCategory, categoryName=General, uid=1, version=2, createdAt=Wed Jan 07 15:40:40 WAT 2015, updatedAt=Fri Apr 17 16:37:09 WAT 2015, active=true}}._id"&gt;</code></p>