JsonSerializationTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. namespace JMS\Serializer\Tests\Serializer;
  3. use JMS\Serializer\Context;
  4. use JMS\Serializer\EventDispatcher\Event;
  5. use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
  6. use JMS\Serializer\Exception\RuntimeException;
  7. use JMS\Serializer\GraphNavigator;
  8. use JMS\Serializer\SerializationContext;
  9. use JMS\Serializer\Tests\Fixtures\Author;
  10. use JMS\Serializer\Tests\Fixtures\AuthorList;
  11. use JMS\Serializer\Tests\Fixtures\ObjectWithEmptyArrayAndHash;
  12. use JMS\Serializer\Tests\Fixtures\ObjectWithInlineArray;
  13. use JMS\Serializer\Tests\Fixtures\Tag;
  14. use JMS\Serializer\VisitorInterface;
  15. class JsonSerializationTest extends BaseSerializationTest
  16. {
  17. protected function getContent($key)
  18. {
  19. static $outputs = array();
  20. if (!$outputs) {
  21. $outputs['readonly'] = '{"id":123,"full_name":"Ruud Kamphuis"}';
  22. $outputs['string'] = '"foo"';
  23. $outputs['boolean_true'] = 'true';
  24. $outputs['boolean_false'] = 'false';
  25. $outputs['integer'] = '1';
  26. $outputs['float'] = '4.533';
  27. $outputs['float_trailing_zero'] = '1';
  28. $outputs['simple_object'] = '{"foo":"foo","moo":"bar","camel_case":"boo"}';
  29. $outputs['circular_reference'] = '{"collection":[{"name":"child1"},{"name":"child2"}],"another_collection":[{"name":"child1"},{"name":"child2"}]}';
  30. $outputs['array_strings'] = '["foo","bar"]';
  31. $outputs['array_booleans'] = '[true,false]';
  32. $outputs['array_integers'] = '[1,3,4]';
  33. $outputs['array_empty'] = '{"array":[]}';
  34. $outputs['array_floats'] = '[1.34,3,6.42]';
  35. $outputs['array_objects'] = '[{"foo":"foo","moo":"bar","camel_case":"boo"},{"foo":"baz","moo":"boo","camel_case":"boo"}]';
  36. $outputs['array_list_and_map_difference'] = '{"list":[1,2,3],"map":{"0":1,"2":2,"3":3}}';
  37. $outputs['array_mixed'] = '["foo",1,true,{"foo":"foo","moo":"bar","camel_case":"boo"},[1,3,true]]';
  38. $outputs['array_datetimes_object'] = '{"array_with_default_date_time":["2047-01-01T12:47:47+0000","2016-12-05T00:00:00+0000"],"array_with_formatted_date_time":["01.01.2047 12:47:47","05.12.2016 00:00:00"]}';
  39. $outputs['array_named_datetimes_object'] = '{"named_array_with_formatted_date":{"testdate1":"01.01.2047 12:47:47","testdate2":"05.12.2016 00:00:00"}}';
  40. $outputs['array_datetimes_object'] = '{"array_with_default_date_time":["2047-01-01T12:47:47+0000","2016-12-05T00:00:00+0000"],"array_with_formatted_date_time":["01.01.2047 12:47:47","05.12.2016 00:00:00"]}';
  41. $outputs['array_named_datetimes_object'] = '{"named_array_with_formatted_date":{"testdate1":"01.01.2047 12:47:47","testdate2":"05.12.2016 00:00:00"}}';
  42. $outputs['array_named_datetimeimmutables_object'] = '{"named_array_with_formatted_date":{"testdate1":"01.01.2047 12:47:47","testdate2":"05.12.2016 00:00:00"}}';
  43. $outputs['blog_post'] = '{"id":"what_a_nice_id","title":"This is a nice title.","created_at":"2011-07-30T00:00:00+0000","is_published":false,"is_reviewed":false,"etag":"1edf9bf60a32d89afbb85b2be849e3ceed5f5b10","comments":[{"author":{"full_name":"Foo Bar"},"text":"foo"}],"comments2":[{"author":{"full_name":"Foo Bar"},"text":"foo"}],"metadata":{"foo":"bar"},"author":{"full_name":"Foo Bar"},"publisher":{"pub_name":"Bar Foo"},"tag":[{"name":"tag1"},{"name":"tag2"}]}';
  44. $outputs['blog_post_unauthored'] = '{"id":"what_a_nice_id","title":"This is a nice title.","created_at":"2011-07-30T00:00:00+0000","is_published":false,"is_reviewed":false,"etag":"1edf9bf60a32d89afbb85b2be849e3ceed5f5b10","comments":[],"comments2":[],"metadata":{"foo":"bar"},"author":null,"publisher":null,"tag":null}';
  45. $outputs['price'] = '{"price":3}';
  46. $outputs['currency_aware_price'] = '{"currency":"EUR","amount":2.34}';
  47. $outputs['order'] = '{"cost":{"price":12.34}}';
  48. $outputs['order_with_currency_aware_price'] = '{"cost":{"currency":"EUR","amount":1.23}}';
  49. $outputs['log'] = '{"author_list":[{"full_name":"Johannes Schmitt"},{"full_name":"John Doe"}],"comments":[{"author":{"full_name":"Foo Bar"},"text":"foo"},{"author":{"full_name":"Foo Bar"},"text":"bar"},{"author":{"full_name":"Foo Bar"},"text":"baz"}]}';
  50. $outputs['lifecycle_callbacks'] = '{"name":"Foo Bar"}';
  51. $outputs['form_errors'] = '["This is the form error","Another error"]';
  52. $outputs['nested_form_errors'] = '{"errors":["This is the form error"],"children":{"bar":{"errors":["Error of the child form"]}}}';
  53. $outputs['constraint_violation'] = '{"property_path":"foo","message":"Message of violation"}';
  54. $outputs['constraint_violation_list'] = '[{"property_path":"foo","message":"Message of violation"},{"property_path":"bar","message":"Message of another violation"}]';
  55. $outputs['article'] = '{"custom":"serialized"}';
  56. $outputs['orm_proxy'] = '{"foo":"foo","moo":"bar","camel_case":"proxy-boo"}';
  57. $outputs['custom_accessor'] = '{"comments":{"Foo":{"comments":[{"author":{"full_name":"Foo"},"text":"foo"},{"author":{"full_name":"Foo"},"text":"bar"}],"count":2}}}';
  58. $outputs['mixed_access_types'] = '{"id":1,"name":"Johannes","read_only_property":42}';
  59. $outputs['accessor_order_child'] = '{"c":"c","d":"d","a":"a","b":"b"}';
  60. $outputs['accessor_order_parent'] = '{"a":"a","b":"b"}';
  61. $outputs['accessor_order_methods'] = '{"foo":"c","b":"b","a":"a"}';
  62. $outputs['inline'] = '{"c":"c","a":"a","b":"b","d":"d"}';
  63. $outputs['inline_child_empty'] = '{"c":"c","d":"d"}';
  64. $outputs['empty_child'] = '{"c":"c","d":"d","child":{}}';
  65. $outputs['empty_child_skip'] = '{"c":"c","d":"d"}';
  66. $outputs['groups_all'] = '{"foo":"foo","foobar":"foobar","bar":"bar","none":"none"}';
  67. $outputs['groups_foo'] = '{"foo":"foo","foobar":"foobar"}';
  68. $outputs['groups_foobar'] = '{"foo":"foo","foobar":"foobar","bar":"bar"}';
  69. $outputs['groups_default'] = '{"bar":"bar","none":"none"}';
  70. $outputs['groups_advanced'] = '{"name":"John","manager":{"name":"John Manager","friends":[{"nickname":"nickname"},{"nickname":"nickname"}]},"friends":[{"manager":{"name":"John friend 1 manager"}},{"manager":{"name":"John friend 2 manager"}}]}';
  71. $outputs['virtual_properties'] = '{"exist_field":"value","virtual_value":"value","test":"other-name","typed_virtual_property":1}';
  72. $outputs['virtual_properties_low'] = '{"low":1}';
  73. $outputs['virtual_properties_high'] = '{"high":8}';
  74. $outputs['virtual_properties_all'] = '{"low":1,"high":8}';
  75. $outputs['nullable'] = '{"foo":"bar","baz":null,"0":null}';
  76. $outputs['nullable_skip'] = '{"foo":"bar"}';
  77. $outputs['person_secret_show'] = '{"name":"mike","gender":"f"}';
  78. $outputs['person_secret_hide'] = '{"name":"mike"}';
  79. $outputs['null'] = 'null';
  80. $outputs['simple_object_nullable'] = '{"foo":"foo","moo":"bar","camel_case":"boo","null_property":null}';
  81. $outputs['input'] = '{"attributes":{"type":"text","name":"firstname","value":"Adrien"}}';
  82. $outputs['hash_empty'] = '{"hash":{}}';
  83. $outputs['object_when_null'] = '{"text":"foo"}';
  84. $outputs['object_when_null_and_serialized'] = '{"author":null,"text":"foo"}';
  85. $outputs['date_time'] = '"2011-08-30T00:00:00+0000"';
  86. $outputs['date_time_immutable'] = '"2011-08-30T00:00:00+0000"';
  87. $outputs['timestamp'] = '{"timestamp":1455148800}';
  88. $outputs['timestamp_prev'] = '{"timestamp":"1455148800"}';
  89. $outputs['date_interval'] = '"PT45M"';
  90. $outputs['car'] = '{"km":5,"type":"car"}';
  91. $outputs['car_without_type'] = '{"km":5}';
  92. $outputs['post'] = '{"type":"post","title":"Post Title"}';
  93. $outputs['image_post'] = '{"type":"image_post","title":"Image Post Title"}';
  94. $outputs['image_post_without_type'] = '{"title":"Image Post Title"}';
  95. $outputs['garage'] = '{"vehicles":[{"km":3,"type":"car"},{"km":1,"type":"moped"}]}';
  96. $outputs['tree'] = '{"tree":{"children":[{"children":[{"children":[],"foo":"bar"}],"foo":"bar"}],"foo":"bar"}}';
  97. $outputs['nullable_arrays'] = '{"empty_inline":[],"not_empty_inline":["not_empty_inline"],"empty_not_inline":[],"not_empty_not_inline":["not_empty_not_inline"],"empty_not_inline_skip":[],"not_empty_not_inline_skip":["not_empty_not_inline_skip"]}';
  98. $outputs['object_with_object_property_no_array_to_author'] = '{"foo": "bar", "author": "baz"}';
  99. $outputs['object_with_object_property'] = '{"foo": "bar", "author": {"full_name": "baz"}}';
  100. $outputs['author_expression'] = '{"my_first_name":"Ruud","last_name":"Kamphuis","id":123}';
  101. $outputs['maxdepth_skippabe_object'] = '{"a":{"xxx":"yyy"}}';
  102. $outputs['type_casting'] = '{"as_string":"8"}';
  103. }
  104. if (!isset($outputs[$key])) {
  105. throw new RuntimeException(sprintf('The key "%s" is not supported.', $key));
  106. }
  107. return $outputs[$key];
  108. }
  109. public function testSkipEmptyArrayAndHash()
  110. {
  111. $object = new ObjectWithEmptyArrayAndHash();
  112. $this->assertEquals('{}', $this->serialize($object));
  113. }
  114. public function testAddLinksToOutput()
  115. {
  116. $this->dispatcher->addListener('serializer.post_serialize', function (Event $event) {
  117. $this->assertFalse($event->getVisitor()->hasData('_links'));
  118. }, 'JMS\Serializer\Tests\Fixtures\Author', 'json');
  119. $this->dispatcher->addSubscriber(new LinkAddingSubscriber());
  120. $this->dispatcher->addListener('serializer.post_serialize', function (Event $event) {
  121. $this->assertTrue($event->getVisitor()->hasData('_links'));
  122. }, 'JMS\Serializer\Tests\Fixtures\Author', 'json');
  123. $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_SERIALIZATION, 'JMS\Serializer\Tests\Fixtures\AuthorList', 'json',
  124. function (VisitorInterface $visitor, AuthorList $data, array $type, Context $context) {
  125. return $visitor->visitArray(iterator_to_array($data), $type, $context);
  126. }
  127. );
  128. $list = new AuthorList();
  129. $list->add(new Author('foo'));
  130. $list->add(new Author('bar'));
  131. $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));
  132. }
  133. public function testReplaceNameInOutput()
  134. {
  135. $this->dispatcher->addSubscriber(new ReplaceNameSubscriber());
  136. $this->handlerRegistry->registerHandler(GraphNavigator::DIRECTION_SERIALIZATION, 'JMS\Serializer\Tests\Fixtures\AuthorList', 'json',
  137. function (VisitorInterface $visitor, AuthorList $data, array $type, Context $context) {
  138. return $visitor->visitArray(iterator_to_array($data), $type, $context);
  139. }
  140. );
  141. $list = new AuthorList();
  142. $list->add(new Author('foo'));
  143. $list->add(new Author('bar'));
  144. $this->assertEquals('[{"full_name":"new name"},{"full_name":"new name"}]', $this->serialize($list));
  145. }
  146. /**
  147. * @expectedException RuntimeException
  148. * @expectedExceptionMessage Invalid data "baz"(string), expected "JMS\Serializer\Tests\Fixtures\Author".
  149. */
  150. public function testDeserializingObjectWithObjectPropertyWithNoArrayToObject()
  151. {
  152. $content = $this->getContent('object_with_object_property_no_array_to_author');
  153. $object = $this->deserialize($content, 'JMS\Serializer\Tests\Fixtures\ObjectWithObjectProperty');
  154. $this->assertEquals('bar', $object->getFoo());
  155. $this->assertInstanceOf('JMS\Serializer\Tests\Fixtures\Author', $object->getAuthor());
  156. }
  157. public function testDeserializingObjectWithObjectProperty()
  158. {
  159. $content = $this->getContent('object_with_object_property');
  160. $object = $this->deserialize($content, 'JMS\Serializer\Tests\Fixtures\ObjectWithObjectProperty');
  161. $this->assertEquals('bar', $object->getFoo());
  162. $this->assertInstanceOf('JMS\Serializer\Tests\Fixtures\Author', $object->getAuthor());
  163. $this->assertEquals('baz', $object->getAuthor()->getName());
  164. }
  165. public function getPrimitiveTypes()
  166. {
  167. return array(
  168. array(
  169. 'type' => 'boolean',
  170. 'data' => true,
  171. ),
  172. array(
  173. 'type' => 'boolean',
  174. 'data' => 1,
  175. ),
  176. array(
  177. 'type' => 'integer',
  178. 'data' => 123,
  179. ),
  180. array(
  181. 'type' => 'integer',
  182. 'data' => "123",
  183. ),
  184. array(
  185. 'type' => 'string',
  186. 'data' => "hello",
  187. ),
  188. array(
  189. 'type' => 'string',
  190. 'data' => 123,
  191. ),
  192. array(
  193. 'type' => 'double',
  194. 'data' => 0.1234,
  195. ),
  196. array(
  197. 'type' => 'double',
  198. 'data' => "0.1234",
  199. ),
  200. );
  201. }
  202. /**
  203. * @dataProvider getPrimitiveTypes
  204. */
  205. public function testPrimitiveTypes($primitiveType, $data)
  206. {
  207. $visitor = $this->serializationVisitors->get('json')->get();
  208. $functionToCall = 'visit' . ucfirst($primitiveType);
  209. $result = $visitor->$functionToCall($data, array(), $this->getMockBuilder('JMS\Serializer\Context')->getMock());
  210. if ($primitiveType == 'double') {
  211. $primitiveType = 'float';
  212. }
  213. $this->assertInternalType($primitiveType, $result);
  214. }
  215. /**
  216. * @group empty-object
  217. */
  218. public function testSerializeEmptyObject()
  219. {
  220. $this->assertEquals('{}', $this->serialize(new Author(null)));
  221. }
  222. /**
  223. * @group encoding
  224. * @expectedException RuntimeException
  225. * @expectedExceptionMessage Your data could not be encoded because it contains invalid UTF8 characters.
  226. */
  227. public function testSerializeWithNonUtf8EncodingWhenDisplayErrorsOff()
  228. {
  229. ini_set('display_errors', 1);
  230. $this->serialize(array('foo' => 'bar', 'bar' => pack("H*", 'c32e')));
  231. }
  232. /**
  233. * @group encoding
  234. * @expectedException RuntimeException
  235. * @expectedExceptionMessage Your data could not be encoded because it contains invalid UTF8 characters.
  236. */
  237. public function testSerializeWithNonUtf8EncodingWhenDisplayErrorsOn()
  238. {
  239. ini_set('display_errors', 0);
  240. $this->serialize(array('foo' => 'bar', 'bar' => pack("H*", 'c32e')));
  241. }
  242. public function testSerializeArrayWithEmptyObject()
  243. {
  244. $this->assertEquals('[{}]', $this->serialize(array(new \stdClass())));
  245. }
  246. public function testInlineArray()
  247. {
  248. $object = new ObjectWithInlineArray(['a' => 'b', 'c' => 'd']);
  249. $this->assertEquals('{"a":"b","c":"d"}', $this->serialize($object));
  250. }
  251. public function testSerializeRootArrayWithDefinedKeys()
  252. {
  253. $author1 = new Author("Jim");
  254. $author2 = new Author("Mark");
  255. $data = array(
  256. 'jim' => $author1,
  257. 'mark' => $author2,
  258. );
  259. $this->assertEquals('{"jim":{"full_name":"Jim"},"mark":{"full_name":"Mark"}}', $this->serializer->serialize($data, $this->getFormat(), SerializationContext::create()->setInitialType('array')));
  260. $this->assertEquals('[{"full_name":"Jim"},{"full_name":"Mark"}]', $this->serializer->serialize($data, $this->getFormat(), SerializationContext::create()->setInitialType('array<JMS\Serializer\Tests\Fixtures\Author>')));
  261. $this->assertEquals('{"jim":{"full_name":"Jim"},"mark":{"full_name":"Mark"}}', $this->serializer->serialize($data, $this->getFormat(), SerializationContext::create()->setInitialType('array<string,JMS\Serializer\Tests\Fixtures\Author>')));
  262. $data = array(
  263. $author1,
  264. $author2,
  265. );
  266. $this->assertEquals('[{"full_name":"Jim"},{"full_name":"Mark"}]', $this->serializer->serialize($data, $this->getFormat(), SerializationContext::create()->setInitialType('array')));
  267. $this->assertEquals('{"0":{"full_name":"Jim"},"1":{"full_name":"Mark"}}', $this->serializer->serialize($data, $this->getFormat(), SerializationContext::create()->setInitialType('array<int,JMS\Serializer\Tests\Fixtures\Author>')));
  268. $this->assertEquals('{"0":{"full_name":"Jim"},"1":{"full_name":"Mark"}}', $this->serializer->serialize($data, $this->getFormat(), SerializationContext::create()->setInitialType('array<string,JMS\Serializer\Tests\Fixtures\Author>')));
  269. }
  270. public function getTypeHintedArrays()
  271. {
  272. return [
  273. [[1, 2], '[1,2]', null],
  274. [['a', 'b'], '["a","b"]', null],
  275. [['a' => 'a', 'b' => 'b'], '{"a":"a","b":"b"}', null],
  276. [[], '[]', null],
  277. [[], '[]', SerializationContext::create()->setInitialType('array')],
  278. [[], '[]', SerializationContext::create()->setInitialType('array<integer>')],
  279. [[], '{}', SerializationContext::create()->setInitialType('array<string,integer>')],
  280. [[1, 2], '[1,2]', SerializationContext::create()->setInitialType('array')],
  281. [[1 => 1, 2 => 2], '{"1":1,"2":2}', SerializationContext::create()->setInitialType('array')],
  282. [[1 => 1, 2 => 2], '[1,2]', SerializationContext::create()->setInitialType('array<integer>')],
  283. [['a', 'b'], '["a","b"]', SerializationContext::create()->setInitialType('array<string>')],
  284. [[1 => 'a', 2 => 'b'], '["a","b"]', SerializationContext::create()->setInitialType('array<string>')],
  285. [['a' => 'a', 'b' => 'b'], '["a","b"]', SerializationContext::create()->setInitialType('array<string>')],
  286. [[1, 2], '{"0":1,"1":2}', SerializationContext::create()->setInitialType('array<integer,integer>')],
  287. [[1, 2], '{"0":1,"1":2}', SerializationContext::create()->setInitialType('array<string,integer>')],
  288. [[1, 2], '{"0":"1","1":"2"}', SerializationContext::create()->setInitialType('array<string,string>')],
  289. [['a', 'b'], '{"0":"a","1":"b"}', SerializationContext::create()->setInitialType('array<integer,string>')],
  290. [['a' => 'a', 'b' => 'b'], '{"a":"a","b":"b"}', SerializationContext::create()->setInitialType('array<string,string>')],
  291. ];
  292. }
  293. /**
  294. * @dataProvider getTypeHintedArrays
  295. * @param array $array
  296. * @param string $expected
  297. * @param SerializationContext|null $context
  298. */
  299. public function testTypeHintedArraySerialization(array $array, $expected, $context = null)
  300. {
  301. $this->assertEquals($expected, $this->serialize($array, $context));
  302. }
  303. public function getTypeHintedArraysAndStdClass()
  304. {
  305. $c1 = new \stdClass();
  306. $c2 = new \stdClass();
  307. $c2->foo = 'bar';
  308. $tag = new Tag("tag");
  309. $c3 = new \stdClass();
  310. $c3->foo = $tag;
  311. return [
  312. [[$c1], '[{}]', SerializationContext::create()->setInitialType('array<stdClass>')],
  313. [[$c2], '[{"foo":"bar"}]', SerializationContext::create()->setInitialType('array<stdClass>')],
  314. [[$tag], '[{"name":"tag"}]', SerializationContext::create()->setInitialType('array<JMS\Serializer\Tests\Fixtures\Tag>')],
  315. [[$c1], '{"0":{}}', SerializationContext::create()->setInitialType('array<integer,stdClass>')],
  316. [[$c2], '{"0":{"foo":"bar"}}', SerializationContext::create()->setInitialType('array<integer,stdClass>')],
  317. [[$c3], '{"0":{"foo":{"name":"tag"}}}', SerializationContext::create()->setInitialType('array<integer,stdClass>')],
  318. [[$c3], '[{"foo":{"name":"tag"}}]', SerializationContext::create()->setInitialType('array<stdClass>')],
  319. [[$tag], '{"0":{"name":"tag"}}', SerializationContext::create()->setInitialType('array<integer,JMS\Serializer\Tests\Fixtures\Tag>')],
  320. ];
  321. }
  322. /**
  323. * @dataProvider getTypeHintedArraysAndStdClass
  324. * @param array $array
  325. * @param string $expected
  326. * @param SerializationContext|null $context
  327. */
  328. public function testTypeHintedArrayAndStdClassSerialization(array $array, $expected, $context = null)
  329. {
  330. $this->assertEquals($expected, $this->serialize($array, $context));
  331. }
  332. protected function getFormat()
  333. {
  334. return 'json';
  335. }
  336. }
  337. class LinkAddingSubscriber implements EventSubscriberInterface
  338. {
  339. public function onPostSerialize(Event $event)
  340. {
  341. $author = $event->getObject();
  342. $event->getVisitor()->addData('_links', array(
  343. 'details' => 'http://foo.bar/details/' . $author->getName(),
  344. 'comments' => 'http://foo.bar/details/' . $author->getName() . '/comments',
  345. ));
  346. }
  347. public static function getSubscribedEvents()
  348. {
  349. return array(
  350. array('event' => 'serializer.post_serialize', 'method' => 'onPostSerialize', 'format' => 'json', 'class' => 'JMS\Serializer\Tests\Fixtures\Author'),
  351. );
  352. }
  353. }
  354. class ReplaceNameSubscriber implements EventSubscriberInterface
  355. {
  356. public function onPostSerialize(Event $event)
  357. {
  358. $event->getVisitor()->setData('full_name', 'new name');
  359. }
  360. public static function getSubscribedEvents()
  361. {
  362. return array(
  363. array('event' => 'serializer.post_serialize', 'method' => 'onPostSerialize', 'format' => 'json', 'class' => 'JMS\Serializer\Tests\Fixtures\Author'),
  364. );
  365. }
  366. }