public function testAddLinksToOutput() { $this->dispatcher->addSubscriber(new LinkAddingSubscriber()); $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_SERIALIZATION, 'JMS\\SerializerBundle\\Tests\\Fixtures\\AuthorList', 'json', function (VisitorInterface $visitor, AuthorList $data, array $type) { return $visitor->visitArray(iterator_to_array($data), $type); }); $list = new AuthorList(); $list->add(new Author('foo')); $list->add(new Author('bar')); $this->assertEquals('[{"full_name":"foo","_links":{"details":"http:\\/\\/foo.bar\\/details\\/foo","comments":"http:\\/\\/foo.bar\\/details\\/foo\\/comments"}},{"full_name":"bar","_links":{"details":"http:\\/\\/foo.bar\\/details\\/bar","comments":"http:\\/\\/foo.bar\\/details\\/bar\\/comments"}}]', $this->serialize($list)); }
public function deserialize(VisitorInterface $visitor, $data, $type, &$visited) { if ('AuthorList' !== $type) { return; } $visited = true; $elements = $visitor->getNavigator()->accept($data, 'array<integer, JMS\\SerializerBundle\\Tests\\Fixtures\\Author>', $visitor); $list = new AuthorList(); foreach ($elements as $author) { $list->add($author); } return $list; }
protected function setUp() { $this->factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader())); $this->handlerRegistry = new HandlerRegistry(); $this->handlerRegistry->registerSubscribingHandler(new ConstraintViolationHandler()); $this->handlerRegistry->registerSubscribingHandler(new DateTimeHandler()); $this->handlerRegistry->registerSubscribingHandler(new FormErrorHandler(new IdentityTranslator(new MessageSelector()))); $this->handlerRegistry->registerSubscribingHandler(new ArrayCollectionHandler()); $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_SERIALIZATION, 'AuthorList', $this->getFormat(), function (VisitorInterface $visitor, $object, array $type) { return $visitor->visitArray(iterator_to_array($object), $type); }); $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_DESERIALIZATION, 'AuthorList', $this->getFormat(), function (VisitorInterface $visitor, $data) { $type = array('name' => 'array', 'params' => array(array('name' => 'integer', 'params' => array()), array('name' => 'JMS\\SerializerBundle\\Tests\\Fixtures\\Author', 'params' => array()))); $elements = $visitor->getNavigator()->accept($data, $type, $visitor); $list = new AuthorList(); foreach ($elements as $author) { $list->add($author); } return $list; }); $this->dispatcher = new EventDispatcher(); $this->dispatcher->addSubscriber(new DoctrineProxySubscriber()); $namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy()); $objectConstructor = new UnserializeObjectConstructor(); $this->serializationVisitors = array('json' => new JsonSerializationVisitor($namingStrategy), 'xml' => new XmlSerializationVisitor($namingStrategy), 'yml' => new YamlSerializationVisitor($namingStrategy)); $this->deserializationVisitors = array('json' => new JsonDeserializationVisitor($namingStrategy), 'xml' => new XmlDeserializationVisitor($namingStrategy)); $this->serializer = new Serializer($this->factory, $this->handlerRegistry, $objectConstructor, $this->dispatcher, null, $this->serializationVisitors, $this->deserializationVisitors); }