title: Conditionally ignore primitive typed fields with Jackson
body: <p><strong>Short Question</strong>: Is there a way to optionally serialize a field depending on it's value in jackson? Is there a way to tell jackson, something like "if a certain integer field's value is 0, don't serialize it."</p>
  
  <p>(skip the following if you get what I mean)</p>
  
  <p><strong>Long Question</strong> aka <strong>Why would I want to do such a thing</strong>:
  Say I have a Spring MVC model class</p>
  
  <pre><code>@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)
  public class Foo {
      int a0;
      int a1;
      .
      .
      .
      int a99;
      String c;
  
      //getters and setters
  }
  </code></pre>
  
  <p>For a new object of <code>Foo</code>, all <code>int</code> will get initialized to 0, The <code>String</code> to <code>null</code>. So if I serialize this object, I will only get <code>int</code>s in result, all set to 0, thanks to <code>@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL)</code>.</p>
  
  <p>In actual operation, say I have to reply with only <strong>some</strong> of the a1...a99, say a20 to a26, set to some meaningful value (to me it's something other than 0). It would be wise to send only those fields, right?</p>
  
  <p>To achieve this, I can always change my <code>int</code> declarations to <code>Integer</code>. However, I have a lot of numerical computations over ints, and pondering over the net a bit and coming across links like <a href="http://mindprod.com/jgloss/intvsinteger.html" rel="nofollow noreferrer">this</a> and <a href="https://stackoverflow.com/questions/10998470/use-int-or-integer">this</a>, I kinda want to stick to primitive types. Does there exist a solution satisfying both my needs?</p>