1

Is there any way to configure JsonResponse output to be - for example - unescaped unicode?

something like the output of this php function:

json_encode($array, JSON_UNESCAPED_UNICODE);

2 Answers 2

4

Yes, here you go:

$response = new JsonResponse($data);
$response->setEncodingOptions(JSON_UNESCAPED_UNICODE);

return $response;
Sign up to request clarification or add additional context in comments.

2 Comments

And globally, not per invocation?
1

To do this globally, make an event listener. Something like this:

services.yml:

    event_listeners.json_formatter_listener:
        class: EventListeners\JsonResponseFormatterListener
        tags:
            - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse }

JsonResponseFormatterListener.php:

<?php

namespace EventListeners;

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;

class JsonResponseFormatterListener
{
    public function onKernelResponse(FilterResponseEvent $event)
    {
        $response = $event->getResponse();

        if ($response instanceof JsonResponse) {
            $response->setEncodingOptions(JSON_UNESCAPED_UNICODE);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.