title: How to serialize ObjectId to JSON?
body: <p>I want to serialize ObjectId of my <code>Product</code> class to JSON. I got the following JSON:</p>
  
  <pre><code>[{"name":"Play for Java: Covers Play 2","type":"Book","company":"Manning Publications","price":30.0,"imagePath":"public/images/play-for-java.png","rating":4.5,"category":"Computer","author":"Nicolas Leroux","publicationDate":1396224000000,"numPage":320,"_id":539da7a6370882f10d5c2777}]
  </code></pre>
  
  <p>You can notice that the <code>"_id"</code> didn't be properly serialized, it should be <code>"539da7a6370882f10d5c2777"</code> (with double quotes) and not just <code>539da7a6370882f10d5c2777</code>. </p>
  
  <p>Therefore, I have tried to implement my own <code>ObjectIdSerializer</code> as following:</p>
  
  <pre><code>import java.io.IOException;
  
  import org.bson.types.ObjectId;
  
  import com.fasterxml.jackson.core.JsonGenerator;
  import com.fasterxml.jackson.core.JsonProcessingException;
  import com.fasterxml.jackson.databind.JsonSerializer;
  import com.fasterxml.jackson.databind.SerializerProvider;
  
  public class ObjectIdSerializer extends JsonSerializer&lt;ObjectId&gt; {
      @Override
      public void serialize(ObjectId value, JsonGenerator jsonGen,SerializerProvider provider) throws IOException,
              JsonProcessingException {
          jsonGen.writeString(value.toString());
      }
  }
  </code></pre>
  
  <p>It gave me the different error: <code>java.lang.String cannot be cast to org.bson.types.ObjectId (through reference chain: models.Book["_id"])</code></p>
  
  <p>Here are my <code>Product</code> class and <code>Book</code> class:</p>
  
  <p><strong>Product.java</strong></p>
  
  <pre><code>@JsonTypeInfo(use= JsonTypeInfo.Id.CLASS,property="_class")
  public class Product {
      @ObjectId @Id
      @JsonSerialize(using = ObjectIdSerializer.class)
      protected String id;
      @JsonProperty("name")
      protected String name;
      @JsonProperty("type")
      protected String type;
      @JsonProperty("description")
      protected String description;
      @JsonProperty("company")
      protected String company;
      @JsonProperty("price")
      protected float price;
      @JsonProperty("imagePath")
      protected String imagePath;
      @JsonProperty("imageName")
      protected String imageName;
      @JsonProperty("rating")
      protected float rating;
  
      public Product() {
      }
      public String getId() {
          return id;
      }
      public void setId(String id) {
          this.id = id;
      }
      // Getters and setters
     ...    
  </code></pre>
  
  <p><strong>Book.java</strong></p>
  
  <pre><code>public class Book extends Product{
  
      @JsonProperty("category")
      private String category;
      @JsonProperty("author")
      private String author;
      @JsonProperty("publicationDate")
      private Date publicationDate;
      @JsonProperty("numPage")
      private int numPage;
  
      public Book() {
      }
      // Getters and setters   
     ...
  </code></pre>
  
  <p>Can you help me figure it out how can I properly serialize the <code>ObjectId</code> to JSON? </p>