title: How can I use Google GSON to deserialize a JSON array into a a Collection of a generic type?
body: <p>I'm writing some code that is going to be used to retrieve resources from a website. It all sort of looks like this:</p>
  
  <pre><code>public Collection&lt;Project&gt; getProjects() {
          String json = getJsonData(methods.get(Project.class)); //Gets a json list, ie [1, 2, 3, 4]
          Gson gson = new Gson();
          Type collectionType = new TypeToken&lt;Collection&lt;Project&gt;&gt;() {}.getType();
          return gson.fromJson(json, collectionType);
      }
  </code></pre>
  
  <p>So naturally I tried abstracting it using Java generics.</p>
  
  <pre><code>/*
       * Deserialize a json list and return a collection of the given type.
       * 
       * Example usage: getData(AccountQuota.class) -&gt; Collection&lt;AccountQuota&gt;
       */
      @SuppressWarnings("unchecked")
      public &lt;T&gt; Collection&lt;T&gt; getData(Class&lt;T&gt; cls) {
          String json = getJsonData(methods.get(cls)); //Gets a json list, ie [1, 2, 3, 4]
          Gson gson = new Gson();
          Type collectionType = new TypeToken&lt;Collection&lt;T&gt;&gt;(){}.getType();
          return (Collection&lt;T&gt;) gson.fromJson(json, collectionType);
  }
  </code></pre>
  
  <p>The generic version of the code doesn't quite work though.</p>
  
  <pre><code>public void testGetItemFromGetData() throws UserLoginError, ServerLoginError {
              Map&lt;String,String&gt; userData = GobblerAuthenticator.authenticate("foo@example.com", "mypassword");
              String client_key = userData.get("client_key");
              GobblerClient gobblerClient = new GobblerClient(client_key);
              ArrayList&lt;Project&gt; machines = new ArrayList&lt;Project&gt;();
              machines.addAll(gobblerClient.getData(Project.class));
              assertTrue(machines.get(0).getClass() == Project.class);
              Log.i("Machine", gobblerClient.getData(Project.class).toString());
          }
  
  
  java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.gobbler.synchronization.Machine
  at com.gobblertest.GobblerClientTest.testGetItemFromGetData(GobblerClientTest.java:53)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
  at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
  at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:545)
  at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)
  </code></pre>
  
  <p>The class in question:</p>
  
  <pre><code>import java.util.Map;
  
  
  public class Project {
      private int total_bytes_stored;
      private String name;
      private String user_data_guid;
      private int seqnum;
      private String guid;
      private Map current_checkpoint;
      private Map&lt;String, String&gt; upload_folder;
      // TODO: schema_version
      private boolean deleted;
      // TODO: download_folders
  
      public Project() {} // No args constructor used for GSON
  }
  </code></pre>
  
  <p>I'm not quite familiar with all the details of Java generics or GSON internals and my search has not been particularly informative. There a bunch of questions here on SO, but most refer to implementing methods like the original one I had. And the <a href="https://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Generic-Types">GSON docs</a> don't seem to cover this particular case. So again, how can I use Google GSON to deserialize a JSON array into a a Collection of a generic type?</p>