/**
  * {@inheritdoc}
  */
 public function construct(VisitorInterface $visitor, ClassMetadata $metadata, $data, Type $type, DeserializationContext $context)
 {
     if ($context->getDepth() === 1 && $context->attributes->has('target')) {
         return $context->attributes->get('target');
     }
     return $this->fallbackConstructor->construct($visitor, $metadata, $data, $type, $context);
 }
Example #2
0
 private function deserialize($data, Type $type, DeserializationContext $context)
 {
     $context->increaseDepth();
     if (null !== $this->dispatcher && !is_scalar($data)) {
         $this->dispatcher->dispatch(Events::PRE_DESERIALIZE, $event = new PreDeserializeEvent($context, $data, $type));
         $data = $event->getData();
     }
     $metadata = $this->getMetadataForType($type);
     if (null !== $metadata) {
         if (!empty($metadata->discriminatorMap) && $type->is($metadata->discriminatorBaseClass)) {
             $metadata = $this->metadataFactory->getMetadataFor($metadata->getSubtype($data));
         }
     }
     $context->getVisitor()->startVisiting($data, $type, $context);
     $rs = $this->callVisitor($data, $type, $context, $metadata);
     if (null !== $metadata) {
         foreach ($metadata->postDeserializeMethods as $method) {
             $method->getReflection()->invoke($rs);
         }
     }
     if (null !== $this->dispatcher && !is_scalar($data)) {
         $this->dispatcher->dispatch(Events::POST_DESERIALIZE, new PostDeserializeEvent($context, $rs, $type));
     }
     $rs = $context->getVisitor()->endVisiting($rs, $type, $context);
     $context->decreaseDepth();
     return $rs;
 }
 public function testDeserializingNull()
 {
     $objectConstructor = new InitializedBlogPostConstructor();
     $this->serializer = new Serializer($this->factory, $this->handlerRegistry, $objectConstructor, $this->serializationVisitors, $this->deserializationVisitors, $this->dispatcher);
     $post = new BlogPost('This is a nice title.', $author = new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')), new Publisher('Bar Foo'));
     $this->setField($post, 'author', null);
     $this->setField($post, 'publisher', null);
     $this->assertEquals($this->getContent('blog_post_unauthored'), $this->serialize($post, SerializationContext::create()->setSerializeNull(true)));
     if ($this->hasDeserializer()) {
         $deserialized = $this->deserialize($this->getContent('blog_post_unauthored'), get_class($post), DeserializationContext::create()->setSerializeNull(true));
         $this->assertEquals('2011-07-30T00:00:00+0000', $this->getField($deserialized, 'createdAt')->format(\DateTime::ISO8601));
         $this->assertAttributeEquals('This is a nice title.', 'title', $deserialized);
         $this->assertAttributeSame(false, 'published', $deserialized);
         $this->assertAttributeEquals(new ArrayCollection(), 'comments', $deserialized);
         $this->assertEquals(null, $this->getField($deserialized, 'author'));
     }
 }