title: How do I avoid generating JSON when serializing a value that is null or a default value?
body: <p>The <code>serde_json::to_string()</code> function will generate a string which may include <code>null</code> for an <code>Option&lt;T&gt;</code>, or <code>0</code> for a <code>u32</code>. This makes the output larger, so I want to ignore these sorts of values.</p>
  
  <p>I want to simplify the JSON string output of the following structure:</p>
  
  <pre><code>use serde_derive::Serialize; // 1.0.82
  
  #[derive(Serialize)]
  pub struct WeightWithOptionGroup {
      pub group: Option&lt;String&gt;,
      pub proportion: u32,
  }
  </code></pre>
  
  <p>When <code>group</code> is <code>None</code> and <code>proportion</code> is 0, the JSON string should be <code>"{}"</code></p>
  
  <p>Thanks for the answer<a href="https://stackoverflow.com/questions/46993079/how-do-i-change-serdes-default-implementation-to-return-an-empty-object-instead">How do I change Serde's default implementation to return an empty object instead of null?</a>, it can resolve <code>Option</code>problem, but for <code>0</code> there is none solution.</p>