title: Gson deserialization: set final field
body: <p>i deserialize a widget-hierarchy using gson, but have problems deserializing final fields.</p>
  
  <p>Example:</p>
  
  <pre><code>public final class Screen{
  
      @Expose
      private final List&lt;WidgetDefinition&gt; children       = null;
      @Expose
      private final String name           = null;
  }
  
  public final class AWidget implements WidgetDefinition {
      @Expose
      private final String name           = null;
  }
  </code></pre>
  
  <p>i'm deserializing a Screen using a custom deserializer for WidgetDefinition, shown below. 'name' in Screen is set correctly, 'name' in AWidget stays null.</p>
  
  <pre><code>final class Deserializer implements JsonDeserializer&lt;WidgetDefinition&gt; {
  
      public WidgetDefinition deserialize(final JsonElement json, final Type type,
                                          final JsonDeserializationContext context) {
  
          JsonObject jsonObject = json.getAsJsonObject();
  
          String typeName = jsonObject.get("type").getAsString();
          if (typeName.equals("awidget")) {
              return context.deserialize(json, AWidget.class);
          } else {
              return null;
          }
      }
  }
  </code></pre>
  
  <p><strong>Edit:</strong> I wonder if it has to do something with this:</p>
  
  <blockquote>
    <p>Gson 1.7 won’t serialize subclass fields in collection elements. 2.0 adds this extra information.</p>
  </blockquote>
  
  <p>(https://sites.google.com/site/gson/gson-roadmap)</p>