title: Use Swift's Encodable to encode optional properties as null without custom encoding
body: <p>I want to encode an optional field with Swift's <code>JSONEncoder</code>using a <code>struct</code> that conforms to the <code>Encodable</code> protocol.</p>
  
  <p>The default setting is that <code>JSONEncoder</code> uses the <code>encodeIfPresent</code> method, which means that values that are <code>nil</code> are excluded from the Json.</p>
  
  <p>How can I override this for a single property without writing my custom <code>encode(to encoder: Encoder)</code> function, in which I have to implement the encoding for all properties (like <a href="http://benscheirman.com/2017/06/ultimate-guide-to-json-parsing-with-swift-4/" rel="noreferrer">this article</a> suggests under "Custom Encoding" )?</p>
  
  <p>Example:</p>
  
  <pre><code>struct MyStruct: Encodable {
      let id: Int
      let date: Date?
  }
  
  let myStruct = MyStruct(id: 10, date: nil)
  let jsonData = try JSONEncoder().encode(myStruct)
  print(String(data: jsonData, encoding: .utf8)!) // {"id":10}
  </code></pre>