title: Gson custom deserializer for generic types
body: <p>I have a number of classes that implement a single interface type. I want to write a custom deserializer to be able to handle some special case with the json I have to deserialize. Is this possible with google gson? Here is the sample code I have so far:</p>
  
  <p>Class Type:</p>
  
  <pre><code>public class MyClass implements MyInterface {
     .......
  }
  </code></pre>
  
  <p>Deserializer</p>
  
  <pre><code>public class ResponseDeserializer implements JsonDeserializer&lt;MyInterface&gt;
  {
  
      private Gson fGson;
  
      public ResponseDeserializer()
      {
          fGson = new Gson();
      }
  
      @Override
      public MyInterface deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException 
      {
          String jsonString = json.toString();
  
          if(jsonString.substring(0, 0).equals("["))
          {
              jsonString = "{ \"parameter\":" + jsonString + "}";
          }
          return context.deserialize(json, typeOfT);
      }   
  }
  </code></pre>
  
  <p>register and call fromJson method:</p>
  
  <pre><code>@Override
  public &lt;T extends MyInterface&gt; T createResponse(Class&lt;T&gt; responseType)
  {
      T returnObject = new GsonBuilder().registerTypeAdapter(MyInterface.class, new ResponseDeserializer())
              .create().fromJson(getEntityText(), responseType);
      return returnObject;
  }
  </code></pre>
  
  <p>thanks for the help in advance</p>