fabpot/Silex — pull request #410 by marijn (2012-07-03 13:26:31+00:00)
Title: Implement a service provider for the `JMSSerializerBundle`

While I was working on #401 I realized that the serializer provided by the `Symfony\Component\Serializer` really can't stack up to the one provided by `JMSSerializerBundle`. I wondered if it could be easy to implement it in Silex. It turns out it was :smile:. Anyway, I understand if you rather want this bundled as a third-party extension, though I'd like you to consider bundling this as a default :blush:.

The serializer provided by `JMSSerializerBundle` can be used like this:

```php
register(new JMSSerializerServiceProvider(), array(
'serializer.cache.directory' => __DIR__."/cache/serializer"
));

// only accept content types supported by the serializer via the assert method.
$app->get("/pages/{id}.{_format}", function ($id) use ($app) {
// assume a page_repository service exists that returns Page objects.
$page = $app['page_repository']->find($id);
$format = $app['request']->getFormat();

if (!$page instanceof Page) {
$this->abort("No page found for id: $id");
}

return new Response($app['serializer']->serialize($page, $format), 200, array(
"Content-Type" => $app['request']->getMimeType($format)
));
})->assert("_format", "xml|json")
->assert("id", "\d+");
```

An example `Page` and related `Author` class as example (taken from the docs in this PR).

```php