# withJson jms/serializer

**URL:** <https://discourse.slimframework.com/t/withjson-jms-serializer/2959>\
**Category:** Questions\
**Created:** [February 15, 2019, 11:18am UTC](https://discourse.slimframework.com/t/withjson-jms-serializer/2959 "2019-02-15T11:18:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![elvisreis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/elvisreis/32/1298_2.png) [@elvisreis](https://discourse.slimframework.com/u/elvisreis)\
**Post date:** [February 15, 2019, 11:18am UTC](https://discourse.slimframework.com/t/withjson-jms-serializer/2959/1 "2019-02-15T11:18:11Z")

</div>

I need to change the behavior of withJson using another library to serialize, which works with Doctrine, in case I will use jms / serializer, is there any way to replace withJson or use a middleware?

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [February 15, 2019, 5:14pm UTC](https://discourse.slimframework.com/t/withjson-jms-serializer/2959/2 "2019-02-15T17:14:47Z")

</div>

Why don’t you directly use `jms/serializer` then and simply return the serialized JSON string as a response string?

Example:

```php
$app->get('/api', function ($request, $response) {
    $data = []; // fetch data
    $serializer = \JMS\Serializer\SerializerBuilder::create()->build();
    $jsonContent = $serializer->serialize($data, 'json');

    return $response->write($jsonContent);
});

```

---

<div class="post-metadata">

**Author:** ![elvisreis](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/elvisreis/32/1298_2.png) [@elvisreis](https://discourse.slimframework.com/u/elvisreis)\
**Post date:** [February 15, 2019, 5:40pm UTC](https://discourse.slimframework.com/t/withjson-jms-serializer/2959/3 "2019-02-15T17:40:13Z")

</div>

Yes this way it works and the way I’ve been using it, but I just have to repeat the same code every time the api route

---

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [February 16, 2019, 8:32am UTC](https://discourse.slimframework.com/t/withjson-jms-serializer/2959/4 "2019-02-16T08:32:33Z")

</div>

In this case there are “tons” of other possibilities. Hard to say whats right for you 🙂 You may try to fetch the JMS object from the Slim container or you make use of (Action) classes instead of the Slim route callback. Then implement a abstract `BaseAction` class with a `withJson($response, $data)` or `encodeJson($data)` method and use this method instead of the Slim response `withJson` method.

Here is just an example for a single action class (controller with only one action), that uses JMS to put the json into the response:

```php
<?php

namespace App\Action;

use JMS\Serializer\SerializerBuilder;
use Psr\Http\Message\ResponseInterface;
use Slim\Container;
use Slim\Http\Response;

abstract class BaseAction
{
    protected $serializer;

    public function __construct(Container $container)
    {
        $this->serializer = $container->get(SerializerBuilder::class);
    }

    protected function withJson(Response $response, $data): ResponseInterface
    {
        return $response->write($this->serializer->serialize($data, 'json'));
    }
}

```

### The slim container (factory)

```php
$container[\JMS\Serializer\SerializerBuilder::class] = function () {
    return \JMS\Serializer\SerializerBuilder::create()->build();
};

```

### Usage

Route: `$app->get('/', \App\Action\HomeIndexAction::class)`

```php
<?php

namespace App\Action;

use Psr\Http\Message\ResponseInterface;
use Slim\Http\Request;
use Slim\Http\Response;

class HomeIndexAction extends BaseAction
{

    public function __invoke(Request $request, Response $response): ResponseInterface
    {
        $data = [
            'id' => 1234,
            'username' => 'Max',
            'email' => 'max@example.com',
        ];

        return $this->withJson($response, $data);
    }
}

```

---

<div class="post-metadata">

**Author:** ![Mikalay](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/mikalay/32/720_2.png) [@Mikalay](https://discourse.slimframework.com/u/Mikalay)\
**Post date:** [February 20, 2019, 8:15am UTC](https://discourse.slimframework.com/t/withjson-jms-serializer/2959/5 "2019-02-20T08:15:00Z")

</div>

**odan**  
You always give concise answers. Nice to read. On your answers, many can learn and “build” true web applications.  
Thanks!
