title: How to handle a NumberFormatException with Gson in deserialization a JSON response
body: <p>I'm reading a JSON response with <a href="http://code.google.com/p/google-gson/" rel="noreferrer">Gson</a>, which returns somtimes a <code>NumberFormatException</code> because an expected <code>int</code> value is set to an empty string. Now I'm wondering what's the best way to handle this kind of exception. If the value is an empty string, the deserialization should be 0.</p>
  
  <p>Expected JSON response:</p>
  
  <pre><code>{
     "name" : "Test1",
     "runtime" : 90
  }
  </code></pre>
  
  <p>But sometimes the runtime is an empty string:</p>
  
  <pre><code>{
     "name" : "Test2",
     "runtime" : ""
  }
  </code></pre>
  
  <p>The java class looks like this:</p>
  
  <pre><code>public class Foo
  {
      private String name;
      private int runtime;
  }
  </code></pre>
  
  <p>And the deserialization is this:</p>
  
  <pre><code>String input = "{\n" +
                 "   \"name\" : \"Test\",\n" +
                 "   \"runtime\" : \"\"\n" +
                 "}";
  
  Gson gson = new Gson();
  Foo foo = gson.fromJson(input, Foo.class);
  </code></pre>
  
  <p>Which throws a <code>com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: empty String</code> because an empty String is returned instead of an int value. </p>
  
  <p>Is there a way to tell Gson, "<em>if you deserialize the field <code>runtime</code> of the Type <code>Foo</code> and there is a NumberFormatException, just return the default value 0</em>"?</p>
  
  <p>My workaround is to use a <code>String</code> as the Type of the <code>runtime</code> field instead of <code>int</code>, but maybe there is a better way to handle such errors.</p>