title: How to invoke default deserialize with gson
body: <p>I get a json that has "field" field. <br>
  If the "field" has data, then there is an OBJECT that has many (about 20) other fields that are also objects. I can deserialize them without any problems. <br>
  But if "field" has no data, it is an empty ARRAY (I know it's crazy, but that's the response from server and I can't change it). 
  Something like this: </p>
  
  <p>When empty:</p>
  
  <pre><code>"extras":[
  
  ]
  </code></pre>
  
  <p>Has some data: </p>
  
  <pre><code>"extras": {
      "22":{ "name":"some name" },
      "59":{ "name":"some other name" },
      and so on...
  }
  </code></pre>
  
  <p>So, when there if no data (empty array), I obviously get the exception <br></p>
  
  <pre><code>Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException:
  Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 4319
  </code></pre>
  
  <p>I tried to use custom JavaDeserializer:</p>
  
  <pre><code>public class ExtrasAdapter implements JsonDeserializer&lt;Extras&gt; {
      @Override
      public Extras deserialize(JsonElement json, Type typeOf,
          JsonDeserializationContext context) throws JsonParseException {
          try {
              JsonObject jsonObject = json.getAsJsonObject();
              // deserialize normally
  
              // the following does not work, as it makes recursive calls
              // to the same function
              //return context.deserialize(jsonObject,
              //                       new TypeToken&lt;Object&gt;(){}.getType());
          } catch (IllegalStateException e) {
              return null;
          }
      }
  }
  </code></pre>
  
  <p>I read the json the following way</p>
  
  <pre><code>Gson gsonDecoder = new GsonBuilder().registerTypeAdapter(Extras.class, new ExtrasAdapter();
  // httpResponse contains json with extras filed. 
  Reader reader = new InputStreamReader(httpResponse.getEntity().getContent());
  Extras response = gsonDecoder.fromJson(reader, Extras.class);
  </code></pre>
  
  <p>I don't want to deserialize all 20 fields manually (I know this is an option), I just want to call context.defaultDeserialize(), or something like that. <br>
  Once again: I don't have any problems deserializing normal json, creating custom objects, registering custom TypeAdapters, custom JavaDeserializers. It all works already. I need only  some solution for handling a data, that can be both ARRAY and OBJECT. <br> 
  Thanks for any help. </p>
  
  <p>======================<br/><br/><br/>
  The Joey's answer works perfect. That right the thing I was looking for. 
  I'll post my code here.</p>
  
  <pre><code>public class SafeTypeAdapterFactory implements TypeAdapterFactory {
      public &lt;T&gt; TypeAdapter&lt;T&gt; create(Gson gson, TypeToken&lt;T&gt; type) {
          final TypeAdapter&lt;T&gt; delegate = gson.getDelegateAdapter(this, type);
          return new TypeAdapter&lt;T&gt;() {
              public void write(JsonWriter out, T value) throws IOException {
                  try {
                      delegate.write(out, value);
                  } catch (IOException e) {
                      delegate.write(out, null);
                  }
              }
              public T read(JsonReader in) throws IOException {
                  try {
                      return delegate.read(in);
                  } catch (IOException e) {
                      Log.w("Adapter Factory", "IOException. Value skipped");
                      in.skipValue();
                      return null;
                  } catch (IllegalStateException e) {
                      Log.w("Adapter Factory", "IllegalStateException. Value skipped");
                      in.skipValue();
                      return null;
                  } catch (JsonSyntaxException e) {
                      Log.w("Adapter Factory", "JsonSyntaxException. Value skipped");
                      in.skipValue();
                      return null;
                  }
              }
          };
      }
  }
  </code></pre>