title: How to deserialize a subclass with Gson?
body: <p>I have a <code>Message</code> class and a <code>HistoryMessage</code> class which extends <code>Message</code>. How I can  deserialize <code>HistoryMessage</code> using Gson?</p>
  
  <pre><code>public class Message
  {
      @SerializedName("id")
      private int id;
  
          @SerializedName("date")
          private long date;
  
          @SerializedName("out")
          private int out;
  
          @SerializedName("user_id")
          private int userId;
          public Message(int id, long date, int userId, int out)
          {
              this.id = id;
              this.date = date;
              this.userId = userId;
              this.out = out;
          }
      }
          public class MessageHistory extends Message
          {          
              @SerializedName("from_id")
              private int fromId;
              public MessageHistory(int id, long date, int userId, int out, int fromId)
              {
                  super(id, date, userId, out);
                  this.fromId = fromId;
              } 
         }
  </code></pre>
  
  <p>And I have  class - Dialog, that is container for all messages</p>
  
  <pre><code>public class Dialog  {
      private int count;
      private ArrayList&lt;Message&gt; dialogs = new ArrayList&lt;Message&gt;();
  
      public int getCount() {
          return count;
      }
  
      public void setCount(int count) {
          this.count = count;
      }
  
      public ArrayList&lt;Message&gt; getDialogs() {
          return dialogs;
      }
  
      public void setDialogs(Message dialogs) {
         this.dialogs.add(dialogs);
      }
  }
  </code></pre>
  
  <p>and deserializer for <code>Message</code>, but I don't know how I can deserialize a <code>HistoryMessage</code>.</p>
  
  <pre><code>public Dialog deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
      Dialog dialog = new Dialog();
      JsonArray jsonArray = (JsonArray)json.getAsJsonObject().get("response").getAsJsonObject().get("items");
      int count = (Integer)json.getAsJsonObject().get("response").getAsJsonObject().get("count").getAsInt();
  
      for ( int i = 0; i &lt; jsonArray.size(); i++ ) {
          JsonElement jsonElement = jsonArray.get(i);
          Message message = new Message();
          boolean unread = false;
  
          if ( jsonElement.getAsJsonObject().has("unread")){
              unread = true;
          }
          JsonObject object = (JsonObject)jsonElement.getAsJsonObject().get("message");
          message = context.deserialize(object, Message.class);
          message.setUnread(unread);
          dialog.setDialogs(message);
      }
  
      dialog.setCount(count);
      return  dialog;
  }
  </code></pre>