title: symfony serializing and deserializing date in object
body: <p>I have a Symfony2.8 application that works as a RESTful Service to provide customer data that is serialized/deserialized.
  In Symfony2.8 I used the following code:</p>
  <pre><code>  /**
    * @Route(&quot;/customer/{id}&quot;, name=&quot;rest_customer_read&quot;)
    * @Method({&quot;GET&quot;})
    */
    public function readCustomer(Customer $customer) {
          $response = new Response();
          if ($customer) {
              $serializer = $this-&gt;getSerializer();
              $jsonContent = $serializer-&gt;serialize($customer, 'json');
              $response-&gt;setContent($jsonContent);
              $response-&gt;headers-&gt;set(&quot;Content-Type&quot;, &quot;application/json&quot;);
          } else {
              $response-&gt;setStatusCode(404, &quot;Customer not found.&quot;);
          }
            return $response;
    } 
  </code></pre>
  <p>In order to use the serializer I initiate it as follows:</p>
  <pre><code>  private function getSerializer() {
      $encoders = array( new JsonEncoder());
      $normalizer = new GetSetMethodNormalizer();
      $callback = function ($dateTime) {
      return $dateTime instanceof \DateTime
          ? $dateTime-&gt;format('Y-m-d H:i:s')
          : '';
      };
  
      $normalizer-&gt;setCircularReferenceLimit(1);
      $normalizer-&gt;setIgnoredAttributes(array('customer','visits'));
      // Add Circular reference handler
      $normalizer-&gt;setCircularReferenceHandler(function ($object) {
        return $object-&gt;getId();
      });
  
      $normalizer-&gt;setCallbacks(array('date'=&gt; $callback, 'birthDate' =&gt; $callback,'lastVisit' =&gt; $callback,'nextVisit' =&gt; $callback,'firstVisit' =&gt; $callback,'lastUpdate' =&gt; $callback,'gdprDate' =&gt; $callback));
      $normalizers = array($normalizer);
      $serializer = new Serializer($normalizers, $encoders);
  
      return $serializer;
    }
  </code></pre>
  <p>This works as intended. If the object contains a date attribute it is serialized as a String in the format 'Y-m-d H:i:s'.</p>
  <p>Now I want to update my application to Symfony 5 / Symfony 6. However my code does not work anymore. Not only that a <code>setCallbacks()</code> method does no longer exist in <code>GetSetMethodNormalizer</code>. There is also no <code>setCircularReferenceLimit()</code>.</p>
  <p>So how can I can serialize a date object in Symfony5?</p>