BaseSerializationTest.php 23 KB

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