title: How to return only one specific null field with GSON?
body: <p>If I have a simple object as follows:</p>
  
  <pre><code>String name;
  String email;
  int age;
  boolean isDeveloper;
  </code></pre>
  
  <p>Then let's say JSON object received have values: </p>
  
  <pre><code>{"name":null,"email":null,"age":26,"isDeveloper":true}
  </code></pre>
  
  <p>When deserializing this JSON using GSON as default I have:</p>
  
  <pre><code>{"age":26,"isDeveloper":true}
  </code></pre>
  
  <p>But email field missing will cause a failure afterwards on my application so I want to add </p>
  
  <pre><code>email = null;
  </code></pre>
  
  <p>Serialize back to JSON and have only this field value as null. Also not ignoring any non null fields.</p>
  
  <p>Other null values should not be added to the resulting JSON.</p>
  
  <p>I tried deserializing with a default GSON builder then serializing with a GSON that allows null values as:</p>
  
  <pre><code>Gson gson = new GsonBuilder().serializeNulls().create();
  </code></pre>
  
  <p>Problem is: this will look all null/empty values from the object class and set them all</p>
  
  <pre><code>{"name":null,"email":null,"age":26,"isDeveloper":true}
  </code></pre>
  
  <p>How can I set email property null then serialize back to a JSON containing only this field as null without ignoring any other non null values?</p>
  
  <p>I'm using gson v2.2.4</p>
  
  <p>Here is example code:</p>
  
  <pre><code>import com.google.gson.Gson;
  import com.google.gson.GsonBuilder;
  
  public class App
  {
      private class UserSimple {
          public String name;
          public String email;
          public int age;
          public boolean isDeveloper;
  
  
          UserSimple(String name, String email, int age, boolean isDeveloper) {
              this.name = name;
              this.email = email;
              this.age = age;
              this.isDeveloper = isDeveloper;
          }
  
      }
  
      public static void main( String[] args )
      {
          String userJson = "{'age':26,'email':'abc@dfg.gh','isDeveloper':true,'name':null}";
          Gson gson = new GsonBuilder()
                  .setPrettyPrinting()
                  .disableHtmlEscaping()
                  .create();
  
  
          Gson gsonBuilder = new GsonBuilder().serializeNulls().create();
  
          UserSimple deserializedObj = gson.fromJson(userJson, UserSimple.class);
          System.out.println("\n"+gson.toJson(deserializedObj));
          deserializedObj.email = null;
  
  
          String serializedObj = gsonBuilder.toJson(deserializedObj, UserSimple.class);
          System.out.println("\n"+serializedObj);
  
  
      }
  }
  </code></pre>