title: Jackson serialization: ignore empty values (or null)
body: <p>I'm currently using jackson 2.1.4 and I'm having some trouble ignoring fields when I'm converting an object to a JSON string.</p>
  
  <p>Here's my class which acts as the object to be converted:</p>
  
  <pre><code>public class JsonOperation {
  
  public static class Request {
      @JsonInclude(Include.NON_EMPTY)
      String requestType;
      Data data = new Data();
  
      public static class Data {
          @JsonInclude(Include.NON_EMPTY)
          String username;
          String email;
          String password;
          String birthday;
          String coinsPackage;
          String coins;
          String transactionId;
          boolean isLoggedIn;
      }
  }
  
  public static class Response {
      @JsonInclude(Include.NON_EMPTY)
      String requestType = null;
      Data data = new Data();
  
      public static class Data {
          @JsonInclude(Include.NON_EMPTY)
          enum ErrorCode { ERROR_INVALID_LOGIN, ERROR_USERNAME_ALREADY_TAKEN, ERROR_EMAIL_ALREADY_TAKEN };
          enum Status { ok, error };
  
          Status status;
          ErrorCode errorCode;
          String expiry;
          int coins;
          String email;
          String birthday;
          String pictureUrl;
          ArrayList &lt;Performer&gt; performer;
      }
  }
  }
  </code></pre>
  
  <p>And here's how I convert it:</p>
  
  <pre><code>ObjectMapper mapper = new ObjectMapper();
  mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
  
  JsonOperation subscribe = new JsonOperation();
  
  subscribe.request.requestType = "login";
  
  subscribe.request.data.username = "Vincent";
  subscribe.request.data.password = "test";
  
  
  Writer strWriter = new StringWriter();
  try {
      mapper.writeValue(strWriter, subscribe.request);
  } catch (JsonGenerationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  } catch (JsonMappingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  
  Log.d("JSON", strWriter.toString())
  </code></pre>
  
  <p>Here's the output:</p>
  
  <pre><code>{"data":{"birthday":null,"coins":null,"coinsPackage":null,"email":null,"username":"Vincent","password":"test","transactionId":null,"isLoggedIn":false},"requestType":"login"}
  </code></pre>
  
  <p>How can I avoid those null values? I only want to take required information for the "subscription" purpose!</p>
  
  <p>Here's exactly the output that I'm looking for:</p>
  
  <pre><code>{"data":{"username":"Vincent","password":"test"},"requestType":"login"}
  </code></pre>
  
  <p>I also tried @JsonInclude(Include.NON_NULL) and put all my variables to null, but it didn't work either! Thanks for your help guys!</p>