title: Using Gson and Abstract Classes
body: <p>I'm trying to exchange messages between a client and a server using GSON.</p>
  <p>The problem is the following:</p>
  <p>I have this structure:</p>
  <pre><code>public class Message 
  {
      private TypeOfContent type; //  It's a enum 
      private Content       content;
      ....
  }
  </code></pre>
  <p>Then the object content can be a various set of Classes.</p>
  <p>I found 2 tutorials <a href="http://chaos.unknown-entity.com/?p=124" rel="nofollow noreferrer">here</a> and <a href="http://ovaraksin.blogspot.co.il/2011/05/json-with-gson-and-abstract-classes.html" rel="nofollow noreferrer">here</a>, but none of them solves the problem.</p>
  <p>Edit1:</p>
  <h2>The class Message is this one:</h2>
  <hr />
  <pre><code>public class Mensagem
  {
      private TipoMensagem    type;
  
      private Conteudo        conteudo;
  
      private Cliente         autor;
      private Cliente         destino;    // null -&gt; to all(broadcast)
  }
  </code></pre>
  <hr />
  <h2>And Content is this one:</h2>
  <pre><code>public class Conteudo
  {
      protected   TipoConteudo typeConteudo; 
  
      protected   String      texto;
  
      protected   Posicao     posicao;
  
      public Conteudo(TipoConteudo typeConteudo, String texto, Posicao posicao)
      {
          this.texto   = texto;
          this.posicao = posicao;  
          this.typeConteudo = typeConteudo;
      }    
  } 
  </code></pre>
  <h2>And an example of a extend class from conteudo is this one:</h2>
  <pre><code>public class ConteudoTweet extends Conteudo 
  {
      protected   String      pathImagem;
  
      public ConteudoTweet(TipoConteudo typeConteudo, String tweet, Posicao location, String picturePath) 
      {
          super(typeConteudo,tweet, location);
      
          this.pathImagem = picturePath;
      }
  }
  </code></pre>
  <hr />
  <p>Finally what I do is like : &quot;String strObject  = new Gson().toJson(mensage);&quot; which works but on deserialization it doesn't because it assumes always that it is from Content class</p>