title: Parsing unexpected types in JSON with GSON
body: <p>I have several classes of objects which need to be parsed inside of an android application, which I am already handling quite easily thanks to GSON. However I have just come across a scenario which calls for something more complex and despite the amount of reading and experimenting I've done I have been unable to solve.</p>
  
  <p>Here's a slimmed down example of one of the classes I am using to parse:</p>
  
  <pre><code>public class MyClass
  {
      public String id;
      public List&lt;String&gt; details;
      public Locations locations;
      public List&lt;Timing&gt; timing;
      public Image image;
  
      public class Locations {
         public List&lt;Location&gt; destinations;
         public List&lt;Location&gt; past;
      }
  
      public class Timing {
         public String start;
         public String end;
      }
  }
  </code></pre>
  
  <p>Then I have another class which contains this class:</p>
  
  <pre><code>public class ResultObject
  {
      //These represent the possible return values for a result object.
      public Location location;
      public List&lt;MyClass&gt; myClass;
      public List&lt;OtherClass&gt; otherClass;
  }
  </code></pre>
  
  <p>These are very simple versions of my classes, anyway the JSON version of the ResultObject comes from the server and is easily parsed like so:</p>
  
  <pre><code>Type mapType = new TypeToken&lt;ResultObject&gt;(){}.getType();
  result = gson.fromJson(httpResult.stream, mapType);
  </code></pre>
  
  <p>This works great, I get my nicely parsed objects all day long. The problem arises when I receive an unexpected type on any field inside any of the hierarchy of classes it causes the entire JSON parse to fail and therefore the result is null. For example the 'locations.past' may end up coming back as an array of Strings rather than full JSON objects like they are supposed to be, and instead of just that array list being empty, the entire ResultObject is forfeit to the parsing error. I would really like a way around this, which I suspect lies in using a custom deserializer, but no examples even touch a hierarchy like this.</p>
  
  <p>If anyone has a good idea of how to handle unexpected types when parsing objects of this size I would greatly appreciate it.</p>