title: Gson serialization depending on field value
body: <p>I have a POJO that is similar to:</p>
  
  <pre><code>public class MyGsonPojo {
  
      @Expose
      @SerializedName("value1")
      private String valueOne;
  
      @Expose
      @SerializedName("value2")
      private boolean valueTwo;
  
      @Expose
      @SerializedName("value3")
      private int valueThree;
  
      // Getters and other stuff here
  }
  </code></pre>
  
  <p>The issue is that this object has to be serialized into a json body for a call
  to the server. Some fields are optional for the request and if I even send it with default and null values, the API responds differently (Unfortunately changing the api is not an option). </p>
  
  <p>So basically I need to exclude fields from serialization if any of them is set to a default value. For example if the field <code>valueOne</code> is <code>null</code> the resulting json should be:</p>
  
  <pre><code>{
      "value2" : true,
      "value3" : 2
  }
  </code></pre>
  
  <p>Any idea how to make this a painless effort? I wouldn't want to build the json body manually.</p>
  
  <p>Any help would be great. Thank you in advice.</p>