title: Deserialize a JSON response with Gson containing a field of variable type
body: <p>The responses of a REST API always return a JSON with the following structure:</p>
  
  <pre><code>{
      "status": "&lt;status_code&gt;",
      "data": &lt;data_object&gt;
  }
  </code></pre>
  
  <p>My problem is that the value of <code>data</code> doesn't have an unique type, but it can be a String, a JSON Object or a JSON Array, depending on the called endpoint. I can't figure out how to deserialize it in the right way to create the different Java objects...</p>
  
  <p>For example, I've already prepared some POJOs: the root element</p>
  
  <pre><code>public class ApiResult {
  
      @SerializedName("status")
      public String status;
  
      @SerializedName("data")
      public JsonElement data;  // should I define it as a JsonElement??
  }
  </code></pre>
  
  <p>and two objects that reflects two of the endpoints: </p>
  
  <pre><code>// "data" can be a list of NavItems
  public class NavItem {
  
      @SerializedName("id")
      public String id;
  
      @SerializedName("name")
      public String name;
  
      @SerializedName("icon")
      public String icon;
  
      @SuppressWarnings("serial")
      public static class List extends ArrayList&lt;NavItem&gt; {}
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>// "data" can be a single object representing a Profile
  public class Profile {
  
      @SerializedName("id")
      public String id;
  
      @SerializedName("fullname")
      public String fullname;
  
      @SerializedName("avatar")
      public String avatar;
  }
  </code></pre>
  
  <p>Reading some StackOverflow questions, I've seen I should use the <code>JsonDeserializer&lt;T&gt;</code> interface. But how if the type of <code>data</code> in <code>ApiResult</code> is variable? </p>