title: GSON deserialization with generic types and generic field names
body: <p>Let's say we have structure like this:</p>
  
  <p><strong>JSON:</strong></p>
  
  <pre><code>{
    "body": {
      "cats": [
        {
          "cat": {
            "id": 1,
            "title": "cat1"
          }
        },
        {
          "cat": {
            "id": 2,
            "title": "cat2"
          }
        }
      ]
    }
  }
  </code></pre>
  
  <p><strong>And corresponding POJO:</strong></p>
  
  <p>Response.class</p>
  
  <p><code>private final Body body;</code></p>
  
  <p>Body.class</p>
  
  <p><code>private final Collection&lt;CatWrapper&gt; cats</code></p>
  
  <p>CatWrapper.class</p>
  
  <p><code>private final Cat cat</code></p>
  
  <p>Cat.class</p>
  
  <pre><code>private final int id;
  private final String title;
  </code></pre>
  
  <p>But let say now we have the same structure, but instead of <code>Cat</code> we receive <code>truck</code></p>
  
  <pre><code>{
    "body": {
      "trucks": [
        {
          "truck": {
            "id": 1,
            "engine": "big",
            "wheels": 12
          }
        },
        {
          "truck": {
            "id": 2,
            "engine": "super big",
            "wheels": 128
          }
        }
      ]
    }
  }
  </code></pre>
  
  <p>I'm using GSON (default <code>Retrofit</code> json parser) on <code>Android</code>, consider this while giving solutions.</p>
  
  <p>We could use generics here so response would look like:</p>
  
  <p><code>private final Body&lt;ListResponse&lt;ItemWrapper&lt;T&gt;&gt;</code> but we can't since the field names are specific to a class. </p>
  
  <p><strong>QUESTION:</strong></p>
  
  <p>What Can I do to serialize it automaticaly without creating so many boilerplate classes? I don't really need separate classes like <code>BodyCat</code>, <code>BodyTruck</code>, <code>BodyAnyOtherClassInThisStructure</code> and I'm looking for a way to avoid having them. </p>
  
  <p>@EDIT:</p>
  
  <p>I've change classes (cat, dog -> cat,truck) due to inheritance confusion, classes used here as example DO NOT extends one another</p>