title: symfony deserialize nested objects
body: <p>I have used the Symfony serializer to serialize my <code>Recherche</code> object.
  In a <code>Recherche</code> object, I have sub objects : <code>Categorie</code> and <code>Lieu</code>.</p>
  
  <p>When I deserialize my <code>Recherche</code> object, all the sub objects are transformed in arrays. I would like them to be objects again.</p>
  
  <p>This is how I have serialized my object:</p>
  
  <pre class="lang-php prettyprint-override"><code>$encoders = array(new JsonEncoder());
  $normalizer = new ObjectNormalizer();
  $normalizer-&gt;setIgnoredAttributes(array('parent', 'enfants'));
  $normalizer-&gt;setCircularReferenceHandler(function ($object) {
      return $object-&gt;getCode();
  });
  $normalizers = array($normalizer);
  $serializer = new Serializer($normalizers, $encoders);
  $rechercheJson= $serializer-&gt;serialize($recherche, 'json');
  </code></pre>
  
  <p>And this is how I deserialize it : </p>
  
  <pre class="lang-php prettyprint-override"><code>$encoders = array(new JsonEncoder());
  $normalizer = new ObjectNormalizer();
  $normalizer-&gt;setIgnoredAttributes(array('parent', 'enfants'));
  $normalizer-&gt;setCircularReferenceHandler(function ($object) {
      return $object-&gt;getCode();
  });
  $normalizers = array($normalizer);
  $serializer = new Serializer($normalizers, $encoders);
  $recherche = $serializer-&gt;deserialize($recherche_json, Recherche::class, 'json');
  </code></pre>
  
  <p>I think maybe there is something to do with normalizer, but I can't find anything that helps me in the docs.</p>
  
  <p>Anyone has an idea to help ? </p>
  
  <p>Thanks ! </p>
  
  <p>EDIT : 
  After seeing this post : <a href="https://stackoverflow.com/questions/40033732/denormalize-nested-structure-in-objects-with-symfony-2-serializer">Denormalize nested structure in objects with symfony 2 serializer</a></p>
  
  <p>I tried this : </p>
  
  <pre class="lang-php prettyprint-override"><code>$encoders = array(new JsonEncoder());
              $normalizer = new ObjectNormalizer(null, null, null, new SerializationPropertyTypeExtractor());
              $normalizer-&gt;setIgnoredAttributes(array('parent', 'enfants'));
              $normalizer-&gt;setCircularReferenceHandler(function ($object) {
                  return $object-&gt;getCode();
              });
              $normalizers = array($normalizer,  new ArrayDenormalizer());
              $serializer = new Serializer($normalizers, $encoders);
              $recherche = $serializer-&gt;deserialize($recherche_json, Recherche::class, 'json');
  </code></pre>
  
  <p>And the SerializationPropertyTypeExtractor:</p>
  
  <pre class="lang-php prettyprint-override"><code>class SerializationPropertyTypeExtractor implements PropertyTypeExtractorInterface {
      /**
       * {@inheritdoc}
       */
      public function getTypes($class, $property, array $context = array())
      {
          if (!is_a($class, Recherche::class, true)) {
              return null;
          }
  
          if ('make' !== $property) {
              return null;
          }
  
          if ('lieu' === $property)
          {
              return [new Type(Type::BUILTIN_TYPE_OBJECT, true, LieuRecherche::class)];
          }
          if ('categorie' === $property)
          {
              return [new Type(Type::BUILTIN_TYPE_OBJECT, true, Categorie::class)];
          }
  
          return null;
      }
  }
  </code></pre>
  
  <p>And this works well ! </p>