title: GSON ignore elements with wrong type
body: <p>I'm using Retrofit (in combination with OkHttp and GSON) to communicate with an online webservice.
  The webservice has a default wrapper around all it's responses, similar to:</p>
  
  <pre><code>{  
    "resultCode":"OK",
    "resultObj":"Can be a string or JSON object / array",
    "error":"",
    "message":""
  }
  </code></pre>
  
  <p>In this example <code>resultCode</code> will either be <code>OK</code> or <code>NO</code>. Furthermore <code>error</code> and <code>message</code> only have any contents when an error has occured while processing the request. And last, but not least, <code>resultObj</code> will contain the actual result from the call (which is a string in the example, but some calls return a JSON array or a JSON object).</p>
  
  <p>To process this meta data, I created a generic class, like this one:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>public class ApiResult&lt;T&gt; {
  
      private String error;
      private String message;
      private String resultCode;
      private T resultObj;
  
      // + some getters, setters etcetera
  }
  </code></pre>
  
  <p>I've also created classes that represent the responses sometimes given in <code>resultObj</code> and I've defined an interface for use with Retrofit, that looks a bit like this:</p>
  
  <pre class="lang-java prettyprint-override"><code>public interface SomeWebService {
  
      @GET("/method/string")
      ApiResult&lt;String&gt; someCallThatReturnsAString();
  
      @GET("/method/pojo")
      ApiResult&lt;SomeMappedResult&gt; someCallThatReturnsAnObject();
  
  }
  </code></pre>
  
  <p>As long as the request is valid this all works fine. But when an error occurs on the server side, it will still return a <code>resultObj</code> with a String-type. This causes <code>someCallThatReturnsAnObject</code> to crash inside the Retrofit RestAdapter / GSON library, with a message like this:</p>
  
  <blockquote>
    <p>retrofit.RetrofitError:
    com.google.gson.JsonSyntaxException:
    java.lang.IllegalStateException:</p>
    
    <p>Expected BEGIN_OBJECT but was STRING at line 1 column 110 path $.resultObj</p>
  </blockquote>
  
  <p>Now, finally, my questions are:</p>
  
  <ol>
  <li>Is there an (easy) way to tell GSON that it should just ignore (aka "nullify") a property if it does not match the expected type?</li>
  <li>Can I tell GSON to treat empty strings as null? </li>
  </ol>