getSerializer(); $post = new BlogPost('Foo', new Author('Bar')); $normalized = $serializer->normalize($post); $this->assertTrue(isset($normalized['created_at'])); unset($normalized['created_at']); $this->assertEquals(array( 'title' => 'Foo', 'published' => false, 'author' => array( 'full_name' => 'Bar', ), 'comments' => array(), ), $normalized); } public function testDenormalize() { $serializer = $this->getSerializer(); $post = new BlogPost('Foo', new Author('Bar')); $post->addComment(new Comment(new Author('Johannes'), 'FooBar')); $normalized = $serializer->normalize($post); $post2 = $serializer->denormalize($normalized, 'JMS\SerializerBundle\Tests\Fixtures\BlogPost'); $this->assertEquals($post, $post2); } public function testSerializeUnserialize() { $serializer = $this->getSerializer(); $post = new BlogPost('foo', new Author('bar')); $post->setPublished(); $post->addComment(new Comment(new Author('foo'), 'bar')); $post->addComment(new Comment(new Author('bar'), 'foo')); $serialized = $serializer->serialize($post, 'xml'); $post2 = $serializer->deserialize($serialized, 'JMS\SerializerBundle\Tests\Fixtures\BlogPost', 'xml'); $this->assertEquals($post, $post2); $serialized = $serializer->serialize($post, 'json'); $post2 = $serializer->deserialize($serialized, 'JMS\SerializerBundle\Tests\Fixtures\BlogPost', 'json'); $this->assertEquals($post, $post2); } private function getSerializer($propertyNamingStrategy = null, $encoders = null, $customNormalizers = null) { if (null === $propertyNamingStrategy) { $propertyNamingStrategy = new SerializedNameAnnotationStrategy(new CamelCaseNamingStrategy()); } if (null === $encoders) { $encoders = array( 'xml' => new XmlEncoder(), 'json' => new JsonEncoder(), ); } if (null === $customNormalizers) { $customNormalizers = array( new ArrayCollectionNormalizer(), ); } $exclusionStrategyFactory = new ExclusionStrategyFactory(array( 'ALL' => new AllExclusionStrategy(), 'NONE' => new NoneExclusionStrategy(), )); return new Serializer( new NativePhpTypeNormalizer(), new PropertyBasedNormalizer(new MetadataFactory(new AnnotationDriver(new AnnotationReader())), $propertyNamingStrategy, new UnserializeInstanceCreator(), $exclusionStrategyFactory), $customNormalizers, $encoders ); } }