title: JSON deserialization with GSON doesn't work correctly
body: <p>I'm currently writing an Android app which uses Google's GSON library for JSON deserialization. I already solved this (or at least a <em>very</em> similar) problem a while ago, but my source files got lost because stupid me made a mistake when setting up my backup process. Anyway, I <em>know</em> there's a solution. ;-)</p>
  
  <p>The JSON I want to serialize comes from a web server sending data in the following form:</p>
  
  <pre class="lang-json prettyprint-override"><code>{
      "version": "1.1",
      "error": {
          "name": "bla",
          "code": 1234,
          "message": "oops"
      },
      "result": ???
  }
  </code></pre>
  
  <p>Either <code>error</code> or <code>result</code> is always <code>null</code> depending on whether my request could be processed. <code>result</code> can be anything from <code>boolean</code> to a really complex class.</p>
  
  <p>My Java code structure looks like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Response&lt;T&gt; {
  
      private String version;
      private T result;
      private Error error;
  
      public Response() {
      }
  
      public boolean isError() {
          return error != null;
      }
  
      public String getVersion() {
          return version;
      }
  
      public T getResult() {
          return result;
      }
  }
  
  public class Error {
  
      private String name;
      private int code;
      private String message;
  
      public Error() {
      }
  
      public String getName() {
          return name;
      }
  
      public int getCode() {
          return code;
      }
  
      public String getMessage() {
          return message;
      }
  }
  </code></pre>
  
  <p>When I try to deserialize a <code>Response&lt;Boolean&gt;</code> or even a <code>Response&lt;ArrayList&lt;String&gt;&gt;</code> everything works fine, although I was a bit confused because <code>Response&lt;String[]&gt;</code> didn't work. Even the <code>error</code> field is populated correctly. Anyway, there is one thing which is <em>absolutely</em> not working, it's <code>Response&lt;Device&gt;</code> where <code>Device</code> is a very simple class looking like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Device {
      public String id;
      public String name;
      public String address;
      public String type;
  }
  </code></pre>
  
  <p>And finally, here is my deserialization code:</p>
  
  <pre class="lang-java prettyprint-override"><code>private &lt;T&gt; Response&lt;T&gt; sendCommand(/* ... */) {
      Gson gson = new Gson();
  
      // preparing and sending request etc.
  
      String responseJson = "..."; // response JSON is valid (already verified)
  
      Type t = new TypeToken&lt;Response&lt;T&gt;&gt;() { }.getType();
      Response&lt;T&gt; result = gson.fromJson(responseJson, t);
      return result;
  }
  
  private void doSomething() {
      Response&lt;Device&gt; response = sendCommand("getSomeDevice");
  }
  </code></pre>
  
  <p>I googled for the last three hours, but I couldn't find any solution. (Maybe it's a basic Java issue, I usually code in C#.) Can anyone help me?</p>
  
  <p><em><strong>Edit:</em></strong></p>
  
  <p>I forgot to mention that when I'm debugging my code, it seems GSON deserialized the <code>Device</code> JSON to an object of type <code>LinkedTreeMap</code>. Don't know what that means, but maybe it helps you.</p>