title: Deserializing MongoDB $oid using jackson library
body: <p>I have a very simple POJO that I write to MongoDB. It then returns me JSON that includes the _id property as an $oid object. I'd like to write it into my POJO, but so far have failed. I've seen many people struggle with it and suggesting different solutions, but I have yet to get any of them to work. Probably because nobody provides exact details which libraries they are actually using. There's codehaus.jackson.* there's com.fastxml.jackson.<em>, jongo, org.mongo.</em>, com.mongo.* etc.</p>
  
  <p>My object looks like this:</p>
  
  <pre><code>package com.blueplanetsoftware.metrics.rest;
  
  import javax.persistence.Id;
  
  import com.fasterxml.jackson.annotation.JsonProperty;
  
  public class MongoObject
  {
      @JsonProperty("_id")
      private String _id;
  
      @JsonProperty("userID")
      private String userID;
      @JsonProperty("appID")
      private String appID;  
      @JsonProperty("message")
      private String message = "";
      @JsonProperty("session")
      private String session;
  
      public MongoObject() {}
  
      /**
       * @return the id
       */
      @Id
      @JsonProperty("_id")
      public String getId()
      {
          return _id;
      }
  
      /**
       * @param id the id to set
       */
      @Id
      @JsonProperty("_id")
      public void setId(String id)
      {
          this._id = id;
      }
  }
  </code></pre>
  
  <p>The JSON that I get back looks like this:</p>
  
  <pre><code>{ 
    "userID" : "aap" ,
    "appID" : "noot" , 
    "message" : "JSON" , 
    "session" : "ea944555b5ea8ea6" , 
    "_id" : { "$oid" : "5245f1063004348555e54815"}
  }
  </code></pre>
  
  <p>when I try to deserialize that JSON into my class with this code:</p>
  
  <pre><code>com.fasterxml.jackson.databind.ObjectMapper mapper = new com.fasterxml.jackson.databind.ObjectMapper();
  MongoObject o = mapper.readValue(response, MongoObject.class);
  </code></pre>
  
  <p>it gives me an exception:</p>
  
  <pre><code>java.lang.AssertionError: Can not deserialize instance of java.lang.String out of START_OBJECT token
   at [Source: java.io.StringReader@456c1227; line: 1, column: 93] (through reference chain: com.blueplanetsoftware.metrics.rest.MongoObject["_id"])
  </code></pre>
  
  <p>So how to get the _id / $oid string into my _id property? I'm surprised there's isn't an easy way to do this. At least I haven't found any so far.</p>