title: How to serialize Optional<T> classes with Gson?
body: <p>I have an object with the following attributes.</p>
  
  <pre><code>private final String messageBundle;
  private final List&lt;String&gt; messageParams;
  private final String actionBundle;
  private final Map&lt;String, String&gt; data;
  private final Optional&lt;Pair&lt;Integer,TimeUnit&gt;&gt; ttl;
  private final Optional&lt;Integer&gt; badgeNumber;
  private final Optional&lt;String&gt; collapseKey;
  </code></pre>
  
  <p>The object is in a library, i would rather not modify it just for serialization purpose, and would like to avoid the cost of creating another DTO.</p>
  
  <p>How can i serialize / unserialize Optional attributes? Optional doesn't have a default constructor (neither apache commons Pair), but i can't use the InstanceCreator, and don't really understand how to create a TypeAdapter that would simply delegate the serialization to the underlying Optional content.</p>