title: NumberFormatException in GSON when converting String to double
body: <p>I am working with a JSON response that is improperly formatted. All fields are being returned as <code>Strings</code>. Unfortunately, <strong>I have no control over the return data</strong>. </p>
  
  <p>I am using Gson and attempting to parse a JSON object that includes a field like this:</p>
  
  <pre><code>{
          [...]
      "cost": "9.25"
  }
  </code></pre>
  
  <p>It should obviously be printed as a <code>Number</code>. When I try to parse this as a <code>String</code>, <code>Number</code> or <code>double</code> I get a NumberFormatException:</p>
  
  <pre><code>com.google.gson.JsonSyntaxException: java.lang.NumberFormatException: 
          [...]
      at com.myapp.android.LauncherActivity$1.onSuccess(LauncherActivity.java:69)
          [...]
  Caused by: java.lang.NumberFormatException: 
      at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:267)
      at java.lang.Double.parseDouble(Double.java:285)
      at com.google.gson.stream.JsonReader.nextInt(JsonReader.java:599)
      at com.google.gson.internal.bind.TypeAdapters$7.read(TypeAdapters.java:228)
      ... 19 more
  </code></pre>
  
  <p>LauncherActivity Line 69:</p>
  
  <pre><code>Item item = gson.fromJson(response, Item.class);
  </code></pre>
  
  <hr>
  
  <p>So I followed <a href="https://stackoverflow.com/questions/8863429/how-to-handle-a-numberformatexception-with-gson-in-deserialization-a-json-respon">this similar question</a> and tried creating a <code>TypeAdapter</code> like so:</p>
  
  <pre><code>public class CostTypeAdapter implements JsonDeserializer&lt;Double&gt;, JsonSerializer&lt;Double&gt; {
  
      public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
          Double cost;
          try {
              cost = json.getAsDouble();
          } catch (NumberFormatException e) {
              cost = 0.00d;
          }
          return cost;
      }
  
      public JsonElement serialize(Double src, Type typeOfSrc, 
              JsonSerializationContext context) {
          return new JsonPrimitive(src);
      }
  }
  </code></pre>
  
  <p>And registered it when creating the <code>GsonBuilder</code>:</p>
  
  <pre><code>GsonBuilder builder = new GsonBuilder();
  builder.registerTypeAdapter(Cost.class, new CostTypeAdapter());
  Gson gson = builder.create(); 
  </code></pre>
  
  <p>And my <code>Cost</code> class: </p>
  
  <pre><code>public class Cost {
      private Double value;
  
      public Cost(Double value) {
          this.value = value;
      }
  
      public Double getValue() {
          return value;
      }
  }
  </code></pre>
  
  <p>But I get the same <code>NumberFormatException</code>. </p>
  
  <p>Any ideas on whats happening here? Shouldn't this exception be caught in my <code>CostTypeAdapter.deserialize()</code>, at the very least?</p>
  
  <p>Any help/guidance is greatly appreciated.</p>