title: use JsonDeserializer on GSON
body: <p>I have a model that work with multiple JSON responses. However this a response :</p>
  
  <pre><code>items: [
  {
  kind: "youtube#playlistItem",
  etag: ""fpJ9onbY0Rl_LqYLG6rOCJ9h9N8/jqbcTLu8tYm8b1FXGO14gNZrFG4"",
  id: "PLUQ7I1jJqKB4lJarGcpWsP62l7iC06IkE2LDE0BxLe18",
  </code></pre>
  
  <p>That conflict with this one (notice the same <code>id</code> field with different type. The above <code>id</code> is <code>String</code>, the other is a <code>Class</code>) :</p>
  
  <pre><code>items: [
  {
  kind: "youtube#searchResult",
  etag: ""fpJ9onbY0Rl_LqYLG6rOCJ9h9N8/hDIU49vmD5aPhKUN5Yz9gtljG9A"",
  id: {
  kind: "youtube#playlist",
  playlistId: "PLh6xqIvLQJSf3ynKVEc1axUb1dQwvGWfO"
  },
  </code></pre>
  
  <p>I want to read the <code>id</code> field using a single model class.</p>
  
  <p>This is my model <code>Response</code> class :</p>
  
  <pre><code>public class Response {
      private ArrayList&lt;Item&gt; items = new ArrayList&lt;Item&gt;();
  </code></pre>
  
  <p>And this is my model <code>Item</code> class :</p>
  
  <pre><code>public class Item {
      private Id id;
  
  //nested class inside Item
  public class Id
  {
      private String id;
      private String kind;
      private String playlistId;
  
      public String getId() {
          return id;
      }
  
      public void setId(String id) {
          this.id = id;
      }
  
      public String getKind() {
          return kind;
      }
  
      public void setKind(String kind) {
          this.kind = kind;
      }
  
      public String getPlaylistId() {
          return playlistId;
      }
  
      public void setPlaylistId(String playlistId) {
          this.playlistId = playlistId;
      }
  }
  </code></pre>
  
  <p>Notice that the <code>Id</code> class is inside <code>Item</code> class.</p>
  
  <p>This is how i use <code>registerTypeAdapter</code> :</p>
  
  <pre><code> GsonBuilder gsonBuilder = new GsonBuilder();
              gsonBuilder.registerTypeAdapter(Response.class,
                      new JsonDeserializer&lt;Item.Id&gt;() {
                          @Override
                          public Item.Id deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
                              Item.Id result = new Item().new Id();
  
                              if(jsonElement.isJsonPrimitive() == false)
                              {
                                  result.setKind(jsonElement.getAsJsonObject().get("kind").getAsString());
                                  result.setPlaylistId(jsonElement.getAsJsonObject().get("playlistId").getAsString());
                                  //return new Item.Id(jsonElement.getAsJsonObject().get("kind").getAsString(), jsonElement.getAsJsonObject().get("playlistId").getAsString());
                                  return result;
                              }
                              else
                              {
                                  result.setId(jsonElement.getAsString());
                                  //return new Item.Id(jsonElement.getAsString());
                                  return result;
                              }
                          }
                      });
              Gson gson = gsonBuilder.create();
  
              Response result = Response.success(
                      gson.fromJson(json, gsonClass),
                      HttpHeaderParser.parseCacheHeaders(response));
  </code></pre>
  
  <p>However the above code throws <code>java.lang.NullPointerException</code> in this line :</p>
  
  <pre><code>if(jsonElement.isJsonPrimitive() == false)
  </code></pre>
  
  <p>What should i do?
  Is this the correct way to use <code>registerTypeAdapter</code>?</p>
  
  <p>Thanks for your time</p>