BaseSerializationTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SerializerBundle\Tests\Serializer;
  18. use JMS\SerializerBundle\Tests\Fixtures\AccessorOrderParent;
  19. use JMS\SerializerBundle\Tests\Fixtures\AccessorOrderChild;
  20. use JMS\SerializerBundle\Tests\Fixtures\GetSetObject;
  21. use JMS\SerializerBundle\Tests\Fixtures\IndexedCommentsBlogPost;
  22. use JMS\SerializerBundle\Tests\Fixtures\CurrencyAwareOrder;
  23. use JMS\SerializerBundle\Tests\Fixtures\CurrencyAwarePrice;
  24. use JMS\SerializerBundle\Tests\Fixtures\Order;
  25. use Symfony\Component\Yaml\Inline;
  26. use JMS\SerializerBundle\Serializer\YamlSerializationVisitor;
  27. use Doctrine\Common\Collections\ArrayCollection;
  28. use Symfony\Component\Form\Form;
  29. use Symfony\Component\Form\FormError;
  30. use Symfony\Component\Validator\ConstraintViolation;
  31. use Symfony\Component\Validator\ConstraintViolationList;
  32. use JMS\SerializerBundle\Serializer\Handler\DeserializationHandlerInterface;
  33. use JMS\SerializerBundle\Serializer\Handler\SerializationHandlerInterface;
  34. use JMS\SerializerBundle\Tests\Fixtures\AuthorList;
  35. use JMS\SerializerBundle\Serializer\VisitorInterface;
  36. use JMS\SerializerBundle\Serializer\XmlDeserializationVisitor;
  37. use JMS\SerializerBundle\Serializer\Construction\UnserializeObjectConstructor;
  38. use JMS\SerializerBundle\Serializer\JsonDeserializationVisitor;
  39. use JMS\SerializerBundle\Tests\Fixtures\Log;
  40. use JMS\SerializerBundle\Serializer\Handler\ArrayCollectionHandler;
  41. use JMS\SerializerBundle\Serializer\Handler\ObjectBasedCustomHandler;
  42. use JMS\SerializerBundle\Serializer\Handler\DateTimeHandler;
  43. use JMS\SerializerBundle\Serializer\Handler\FormErrorHandler;
  44. use JMS\SerializerBundle\Serializer\Handler\ConstraintViolationHandler;
  45. use JMS\SerializerBundle\Serializer\Handler\DoctrineProxyHandler;
  46. use JMS\SerializerBundle\Tests\Fixtures\Comment;
  47. use JMS\SerializerBundle\Tests\Fixtures\Author;
  48. use JMS\SerializerBundle\Tests\Fixtures\AuthorReadOnly;
  49. use JMS\SerializerBundle\Tests\Fixtures\BlogPost;
  50. use JMS\SerializerBundle\Tests\Fixtures\ObjectWithLifecycleCallbacks;
  51. use JMS\SerializerBundle\Tests\Fixtures\CircularReferenceParent;
  52. use JMS\SerializerBundle\Tests\Fixtures\InlineParent;
  53. use JMS\SerializerBundle\Tests\Fixtures\GroupsObject;
  54. use JMS\SerializerBundle\Tests\Fixtures\ObjectWithVirtualProperties;
  55. use JMS\SerializerBundle\Serializer\XmlSerializationVisitor;
  56. use Doctrine\Common\Annotations\AnnotationReader;
  57. use JMS\SerializerBundle\Metadata\Driver\AnnotationDriver;
  58. use Metadata\MetadataFactory;
  59. use JMS\SerializerBundle\Tests\Fixtures\SimpleObject;
  60. use JMS\SerializerBundle\Tests\Fixtures\SimpleObjectProxy;
  61. use JMS\SerializerBundle\Tests\Fixtures\Price;
  62. use JMS\SerializerBundle\Serializer\Naming\CamelCaseNamingStrategy;
  63. use JMS\SerializerBundle\Serializer\Naming\SerializedNameAnnotationStrategy;
  64. use JMS\SerializerBundle\Serializer\JsonSerializationVisitor;
  65. use JMS\SerializerBundle\Serializer\Serializer;
  66. abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
  67. {
  68. public function testString()
  69. {
  70. $this->assertEquals($this->getContent('string'), $this->serialize('foo'));
  71. if ($this->hasDeserializer()) {
  72. $this->assertEquals('foo', $this->deserialize($this->getContent('string'), 'string'));
  73. }
  74. }
  75. /**
  76. * @dataProvider getBooleans
  77. */
  78. public function testBooleans($strBoolean, $boolean)
  79. {
  80. $this->assertEquals($this->getContent('boolean_'.$strBoolean), $this->serialize($boolean));
  81. if ($this->hasDeserializer()) {
  82. $this->assertSame($boolean, $this->deserialize($this->getContent('boolean_'.$strBoolean), 'boolean'));
  83. }
  84. }
  85. public function getBooleans()
  86. {
  87. return array(array('true', true), array('false', false));
  88. }
  89. /**
  90. * @dataProvider getNumerics
  91. */
  92. public function testNumerics($key, $value)
  93. {
  94. $this->assertEquals($this->getContent($key), $this->serialize($value));
  95. if ($this->hasDeserializer()) {
  96. $this->assertEquals($value, $this->deserialize($this->getContent($key), is_double($value) ? 'double' : 'integer'));
  97. }
  98. }
  99. public function getNumerics()
  100. {
  101. return array(
  102. array('integer', 1),
  103. array('float', 4.533),
  104. array('float_trailing_zero', 1.0),
  105. );
  106. }
  107. public function testSimpleObject()
  108. {
  109. $this->assertEquals($this->getContent('simple_object'), $this->serialize($obj = new SimpleObject('foo', 'bar')));
  110. if ($this->hasDeserializer()) {
  111. $this->assertEquals($obj, $this->deserialize($this->getContent('simple_object'), get_class($obj)));
  112. }
  113. }
  114. public function testArrayStrings()
  115. {
  116. $data = array('foo', 'bar');
  117. $this->assertEquals($this->getContent('array_strings'), $this->serialize($data));
  118. if ($this->hasDeserializer()) {
  119. $this->assertEquals($data, $this->deserialize($this->getContent('array_strings'), 'array<string>'));
  120. }
  121. }
  122. public function testArrayBooleans()
  123. {
  124. $data = array(true, false);
  125. $this->assertEquals($this->getContent('array_booleans'), $this->serialize($data));
  126. if ($this->hasDeserializer()) {
  127. $this->assertEquals($data, $this->deserialize($this->getContent('array_booleans'), 'array<boolean>'));
  128. }
  129. }
  130. public function testArrayIntegers()
  131. {
  132. $data = array(1, 3, 4);
  133. $this->assertEquals($this->getContent('array_integers'), $this->serialize($data));
  134. if ($this->hasDeserializer()) {
  135. $this->assertEquals($data, $this->deserialize($this->getContent('array_integers'), 'array<integer>'));
  136. }
  137. }
  138. public function testArrayFloats()
  139. {
  140. $data = array(1.34, 3.0, 6.42);
  141. $this->assertEquals($this->getContent('array_floats'), $this->serialize($data));
  142. if ($this->hasDeserializer()) {
  143. $this->assertEquals($data, $this->deserialize($this->getContent('array_floats'), 'array<double>'));
  144. }
  145. }
  146. public function testArrayObjects()
  147. {
  148. $data = array(new SimpleObject('foo', 'bar'), new SimpleObject('baz', 'boo'));
  149. $this->assertEquals($this->getContent('array_objects'), $this->serialize($data));
  150. if ($this->hasDeserializer()) {
  151. $this->assertEquals($data, $this->deserialize($this->getContent('array_objects'), 'array<JMS\SerializerBundle\Tests\Fixtures\SimpleObject>'));
  152. }
  153. }
  154. public function testArrayMixed()
  155. {
  156. $this->assertEquals($this->getContent('array_mixed'), $this->serialize(array('foo', 1, true, new SimpleObject('foo', 'bar'), array(1, 3, true))));
  157. }
  158. public function testBlogPost()
  159. {
  160. $post = new BlogPost('This is a nice title.', $author = new Author('Foo Bar'), new \DateTime('2011-07-30 00:00', new \DateTimeZone('UTC')));
  161. $post->addComment($comment = new Comment($author, 'foo'));
  162. $this->assertEquals($this->getContent('blog_post'), $this->serialize($post));
  163. if ($this->hasDeserializer()) {
  164. $deserialized = $this->deserialize($this->getContent('blog_post'), get_class($post));
  165. $this->assertEquals('2011-07-30T00:00:00+0000', $this->getField($deserialized, 'createdAt')->format(\DateTime::ISO8601));
  166. $this->assertAttributeEquals('This is a nice title.', 'title', $deserialized);
  167. $this->assertAttributeSame(false, 'published', $deserialized);
  168. $this->assertAttributeEquals(new ArrayCollection(array($comment)), 'comments', $deserialized);
  169. $this->assertAttributeEquals($author, 'author', $deserialized);
  170. }
  171. }
  172. public function testReadOnly()
  173. {
  174. $author = new AuthorReadOnly(123, 'Ruud Kamphuis');
  175. $this->assertEquals($this->getContent('readonly'), $this->serialize($author));
  176. if ($this->hasDeserializer()) {
  177. $deserialized = $this->deserialize($this->getContent('readonly'), get_class($author));
  178. $this->assertNull($this->getField($deserialized, 'id'));
  179. $this->assertEquals('Ruud Kamphuis', $this->getField($deserialized, 'name'));
  180. }
  181. }
  182. public function testPrice()
  183. {
  184. $price = new Price(3);
  185. $this->assertEquals($this->getContent('price'), $this->serialize($price));
  186. if ($this->hasDeserializer()) {
  187. $deserialized = $this->deserialize($this->getContent('price'), get_class($price));
  188. $this->assertEquals(3, $this->getField($deserialized, 'price'));
  189. }
  190. }
  191. public function testOrder()
  192. {
  193. $order = new Order(new Price(12.34));
  194. $this->assertEquals($this->getContent('order'), $this->serialize($order));
  195. if ($this->hasDeserializer()) {
  196. $this->assertEquals($order, $this->deserialize($this->getContent('order'), get_class($order)));
  197. }
  198. }
  199. public function testCurrencyAwarePrice()
  200. {
  201. $price = new CurrencyAwarePrice(2.34);
  202. $this->assertEquals($this->getContent('currency_aware_price'), $this->serialize($price));
  203. if ($this->hasDeserializer()) {
  204. $this->assertEquals($price, $this->deserialize($this->getContent('currency_aware_price'), get_class($price)));
  205. }
  206. }
  207. public function testOrderWithCurrencyAwarePrice()
  208. {
  209. $order = new CurrencyAwareOrder(new CurrencyAwarePrice(1.23));
  210. $this->assertEquals($this->getContent('order_with_currency_aware_price'), $this->serialize($order));
  211. if ($this->hasDeserializer()) {
  212. $this->assertEquals($order, $this->deserialize($this->getContent('order_with_currency_aware_price'), get_class($order)));
  213. }
  214. }
  215. public function testArticle()
  216. {
  217. $article = new Article();
  218. $article->element = 'custom';
  219. $article->value = 'serialized';
  220. $result = $this->serialize($article);
  221. $this->assertEquals($this->getContent('article'), $result);
  222. if ($this->hasDeserializer()) {
  223. $this->assertEquals($article, $this->deserialize($result, 'JMS\SerializerBundle\Tests\Serializer\Article'));
  224. }
  225. }
  226. public function testInline()
  227. {
  228. $inline = new InlineParent();
  229. $result = $this->serialize($inline);
  230. $this->assertEquals($this->getContent('inline'), $result);
  231. //no deserialization support
  232. /*if ($this->hasDeserializer()) {
  233. $this->assertEquals($inline, $this->deserialize($result, 'JMS\SerializerBundle\Tests\Serializer\InlineParent'));
  234. }*/
  235. }
  236. /**
  237. * @group test
  238. */
  239. public function testLog()
  240. {
  241. $this->assertEquals($this->getContent('log'), $this->serialize($log = new Log()));
  242. if ($this->hasDeserializer()) {
  243. $deserialized = $this->deserialize($this->getContent('log'), get_class($log));
  244. $this->assertEquals($log, $deserialized);
  245. }
  246. }
  247. public function testCircularReference()
  248. {
  249. $object = new CircularReferenceParent();
  250. $this->assertEquals($this->getContent('circular_reference'), $this->serialize($object));
  251. if ($this->hasDeserializer()) {
  252. $deserialized = $this->deserialize($this->getContent('circular_reference'), get_class($object));
  253. $col = $this->getField($deserialized, 'collection');
  254. $this->assertEquals(2, count($col));
  255. $this->assertEquals('child1', $col[0]->getName());
  256. $this->assertEquals('child2', $col[1]->getName());
  257. $this->assertSame($deserialized, $col[0]->getParent());
  258. $this->assertSame($deserialized, $col[1]->getParent());
  259. $col = $this->getField($deserialized, 'anotherCollection');
  260. $this->assertEquals(2, count($col));
  261. $this->assertEquals('child1', $col[0]->getName());
  262. $this->assertEquals('child2', $col[1]->getName());
  263. $this->assertSame($deserialized, $col[0]->getParent());
  264. $this->assertSame($deserialized, $col[1]->getParent());
  265. }
  266. }
  267. public function testLifecycleCallbacks()
  268. {
  269. $object = new ObjectWithLifecycleCallbacks();
  270. $this->assertEquals($this->getContent('lifecycle_callbacks'), $this->serialize($object));
  271. $this->assertAttributeSame(null, 'name', $object);
  272. if ($this->hasDeserializer()) {
  273. $deserialized = $this->deserialize($this->getContent('lifecycle_callbacks'), get_class($object));
  274. $this->assertEquals($object, $deserialized);
  275. }
  276. }
  277. public function testFormErrors()
  278. {
  279. $errors = array(
  280. new FormError('This is the form error'),
  281. new FormError('Another error')
  282. );
  283. $this->assertEquals($this->getContent('form_errors'), $this->serialize($errors));
  284. }
  285. public function testNestedFormErrors()
  286. {
  287. $dispather = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
  288. $form = new Form('foo', $dispather);
  289. $form->addError(new FormError('This is the form error'));
  290. $child = new Form('bar', $dispather);
  291. $child->addError(new FormError('Error of the child form'));
  292. $form->add($child);
  293. $this->assertEquals($this->getContent('nested_form_errors'), $this->serialize($form));
  294. }
  295. public function testConstraintViolation()
  296. {
  297. $violation = new ConstraintViolation('Message of violation', array(), null, 'foo', null);
  298. $this->assertEquals($this->getContent('constraint_violation'), $this->serialize($violation));
  299. }
  300. public function testConstraintViolationList()
  301. {
  302. $violations = new ConstraintViolationList();
  303. $violations->add(new ConstraintViolation('Message of violation', array(), null, 'foo', null));
  304. $violations->add(new ConstraintViolation('Message of another violation', array(), null, 'bar', null));
  305. $this->assertEquals($this->getContent('constraint_violation_list'), $this->serialize($violations));
  306. }
  307. public function testDoctrineProxy()
  308. {
  309. if (!class_exists('Doctrine\ORM\Version')) {
  310. $this->markTestSkipped('Doctrine is not available.');
  311. }
  312. $object = new SimpleObjectProxy('foo', 'bar');
  313. $this->assertEquals($this->getContent('orm_proxy'), $this->serialize($object));
  314. }
  315. public function testInitializedDoctrineProxy()
  316. {
  317. if (!class_exists('Doctrine\ORM\Version')) {
  318. $this->markTestSkipped('Doctrine is not available.');
  319. }
  320. $object = new SimpleObjectProxy('foo', 'bar');
  321. $object->__load();
  322. $this->assertEquals($this->getContent('orm_proxy'), $this->serialize($object));
  323. }
  324. public function testCustomAccessor()
  325. {
  326. $post = new IndexedCommentsBlogPost();
  327. $this->assertEquals($this->getContent('custom_accessor'), $this->serialize($post));
  328. }
  329. public function testMixedAccessTypes()
  330. {
  331. $object = new GetSetObject();
  332. $this->assertEquals($this->getContent('mixed_access_types'), $this->serialize($object));
  333. if ($this->hasDeserializer()) {
  334. $object = $this->deserialize($this->getContent('mixed_access_types'), 'JMS\SerializerBundle\Tests\Fixtures\GetSetObject');
  335. $this->assertAttributeEquals(1, 'id', $object);
  336. $this->assertAttributeEquals('Johannes', 'name', $object);
  337. }
  338. }
  339. public function testAccessorOrder()
  340. {
  341. $this->assertEquals($this->getContent('accessor_order_child'), $this->serialize(new AccessorOrderChild()));
  342. $this->assertEquals($this->getContent('accessor_order_parent'), $this->serialize(new AccessorOrderParent()));
  343. }
  344. public function testGroups()
  345. {
  346. $serializer = $this->getSerializer();
  347. $groupsObject = new GroupsObject();
  348. $this->assertEquals($this->getContent('groups_all'), $serializer->serialize($groupsObject, $this->getFormat()));
  349. $serializer->setGroups(array("foo"));
  350. $this->assertEquals($this->getContent('groups_foo'), $serializer->serialize($groupsObject, $this->getFormat()));
  351. $serializer->setGroups(array("foo", "bar"));
  352. $this->assertEquals($this->getContent('groups_foobar'), $serializer->serialize($groupsObject, $this->getFormat()));
  353. $serializer->setGroups(null);
  354. $this->assertEquals($this->getContent('groups_all'), $serializer->serialize($groupsObject, $this->getFormat()));
  355. $serializer->setGroups(array());
  356. $this->assertEquals($this->getContent('groups_all'), $serializer->serialize($groupsObject, $this->getFormat()));
  357. }
  358. public function testVirtualProperty() {
  359. $this->assertEquals($this->getContent('virtual_properties'), $this->serialize(new ObjectWithVirtualProperties()));
  360. }
  361. abstract protected function getContent($key);
  362. abstract protected function getFormat();
  363. protected function hasDeserializer()
  364. {
  365. return true;
  366. }
  367. protected function serialize($data)
  368. {
  369. return $this->getSerializer()->serialize($data, $this->getFormat());
  370. }
  371. protected function deserialize($content, $type)
  372. {
  373. return $this->getSerializer()->deserialize($content, $type, $this->getFormat());
  374. }
  375. protected function getSerializer()
  376. {
  377. $factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
  378. $namingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy());
  379. $objectConstructor = new UnserializeObjectConstructor();
  380. $customSerializationHandlers = $this->getSerializationHandlers();
  381. $customDeserializationHandlers = $this->getDeserializationHandlers();
  382. $serializationVisitors = array(
  383. 'json' => new JsonSerializationVisitor($namingStrategy, $customSerializationHandlers),
  384. 'xml' => new XmlSerializationVisitor($namingStrategy, $customSerializationHandlers),
  385. 'yml' => new YamlSerializationVisitor($namingStrategy, $customSerializationHandlers),
  386. );
  387. $deserializationVisitors = array(
  388. 'json' => new JsonDeserializationVisitor($namingStrategy, $customDeserializationHandlers, $objectConstructor),
  389. 'xml' => new XmlDeserializationVisitor($namingStrategy, $customDeserializationHandlers, $objectConstructor),
  390. );
  391. return new Serializer($factory, $serializationVisitors, $deserializationVisitors);
  392. }
  393. protected function getSerializationHandlers()
  394. {
  395. $translatorMock = $this->getMock('Symfony\\Component\\Translation\\TranslatorInterface');
  396. $translatorMock
  397. ->expects($this->any())
  398. ->method('trans')
  399. ->will($this->returnArgument(0));
  400. $factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
  401. $objectConstructor = new UnserializeObjectConstructor();
  402. $handlers = array(
  403. new ObjectBasedCustomHandler($objectConstructor, $factory),
  404. new DateTimeHandler(),
  405. new FormErrorHandler($translatorMock),
  406. new ConstraintViolationHandler(),
  407. new DoctrineProxyHandler(),
  408. );
  409. return $handlers;
  410. }
  411. protected function getDeserializationHandlers()
  412. {
  413. $factory = new MetadataFactory(new AnnotationDriver(new AnnotationReader()));
  414. $objectConstructor = new UnserializeObjectConstructor();
  415. $handlers = array(
  416. new ObjectBasedCustomHandler($objectConstructor, $factory),
  417. new DateTimeHandler(),
  418. new ArrayCollectionHandler(),
  419. new AuthorListDeserializationHandler(),
  420. );
  421. return $handlers;
  422. }
  423. private function getField($obj, $name)
  424. {
  425. $ref = new \ReflectionProperty($obj, $name);
  426. $ref->setAccessible(true);
  427. return $ref->getValue($obj);
  428. }
  429. private function setField($obj, $name, $value)
  430. {
  431. $ref = new \ReflectionProperty($obj, $name);
  432. $ref->setAccessible(true);
  433. $ref->setValue($obj, $value);
  434. }
  435. }
  436. class AuthorListDeserializationHandler implements DeserializationHandlerInterface
  437. {
  438. public function deserialize(VisitorInterface $visitor, $data, $type, &$visited)
  439. {
  440. if ('AuthorList' !== $type) {
  441. return;
  442. }
  443. $visited = true;
  444. $elements = $visitor->getNavigator()->accept($data, 'array<integer, JMS\SerializerBundle\Tests\Fixtures\Author>', $visitor);
  445. $list = new AuthorList();
  446. foreach ($elements as $author) {
  447. $list->add($author);
  448. }
  449. return $list;
  450. }
  451. }
  452. class Article implements SerializationHandlerInterface, DeserializationHandlerInterface
  453. {
  454. public $element;
  455. public $value;
  456. public function serialize(VisitorInterface $visitor, $data, $type, &$visited)
  457. {
  458. if (!$data instanceof Article) {
  459. return;
  460. }
  461. if ($visitor instanceof XmlSerializationVisitor) {
  462. $visited = true;
  463. if (null === $visitor->document) {
  464. $visitor->document = $visitor->createDocument(null, null, false);
  465. }
  466. $visitor->document->appendChild($visitor->document->createElement($this->element, $this->value));
  467. } elseif ($visitor instanceof JsonSerializationVisitor) {
  468. $visited = true;
  469. $visitor->setRoot(array($this->element => $this->value));
  470. } elseif ($visitor instanceof YamlSerializationVisitor) {
  471. $visited = true;
  472. $visitor->writer->writeln(Inline::dump($this->element).': '.Inline::dump($this->value));
  473. }
  474. }
  475. public function deserialize(VisitorInterface $visitor, $data, $type, &$visited)
  476. {
  477. if ('JMS\SerializerBundle\Tests\Serializer\Article' !== $type) {
  478. return;
  479. }
  480. if ($visitor instanceof XmlDeserializationVisitor) {
  481. $visited = true;
  482. $this->element = $data->getName();
  483. $this->value = (string)$data;
  484. } elseif ($visitor instanceof JsonDeserializationVisitor) {
  485. $visited = true;
  486. $this->element = key($data);
  487. $this->value = reset($data);
  488. }
  489. return $this;
  490. }
  491. }