title: Gson TypeAdapter and serializeNulls
body: <p>I'm using Gson 2.4.</p>
  
  <p><code>Gson</code> has a flag whether null values should be serialized or omitted (the default).
  When I create a <code>TypeAdapter</code> from a <code>Gson</code> which omits nulls and serialize an object via the <code>TypeAdapter</code> then this flag is ignored, contradicting the <a href="https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/TypeAdapter.html" rel="nofollow">Javadoc</a>:</p>
  
  <blockquote>
    <p>Type adapters should be prepared to read null from the stream and
    write it to the stream. (...) If your Gson
    instance has been configured to GsonBuilder.serializeNulls(), these
    nulls will be written to the final document. <strong>Otherwise the value (and
    the corresponding name when writing to a JSON object) will be omitted
    automatically.</strong> In either case your type adapter must handle null.</p>
  </blockquote>
  
  <p>Here is a test which demonstrates the problem:</p>
  
  <pre><code>import com.google.gson.Gson;
  
  public class Test {
      private String name;
  
      public static void main(String[] args) throws Exception {
          Test object = new Test();
          Gson gson = new Gson(); // serializeNulls is off by default
  
          System.out.println(gson.toJson(object));
          System.out.println(gson.getAdapter(Test.class).toJson(object));
      }
  }
  </code></pre>
  
  <p>This prints:</p>
  
  <pre><code>{}
  {"name":null}
  </code></pre>
  
  <p>I expected that the second line is <code>{}</code>.</p>
  
  <p>Is this a bug or did I misread the documentation? In both cases: Is there are a workaround to omit nulls when serializing via a <code>TypeAdapter</code>?</p>