JMSSerializer with Symfony4 serialize encoding

892 views Asked by At

My configuration: Symfony 4.2.5 , JMSSerialiser 3.2. I cannot obtain UTF-8 response with serialization. I thougt solution was in config:

jms_serializer:
    visitors:
        json:
            options: [JSON_UNESCAPED_UNICODE]

But it does'nt work, it gives me an UNICODE printing. So, I get this response:

`"aeiou\u00e0\u00e9\u00e8\u00f4\u00fb"`

Here is my code:

/**
* @Route("/route1",name="route1")
*/
public function route1(){
    $serializer = SerializerBuilder::create()->build();
    $chaine = "aeiouàéèôû";
    $reponse = new Response(
        $serializer->serialize($chaine, 'json'),
        Response::HTTP_OK,
        ['content-type' => 'text/html']
    );
    $reponse->setCharset('UTF-8');
    return $reponse;
}

I discover that the problem disappears when I launch the request from firefox which offers me two display modes: json, raw and an additional tab for request and response headers. The Chrome browser only offers me the raw display. Should I conclude that the solution lies in the configuration of Chrome?

1

There are 1 answers

0
Caplande On

Subject resolved. Chrome displays by default the raw presentation of the file where accented characters are in escaped form. To get the json form in Chrome, you need to install the jsonView extension. Once this extension is activated the display of accented characters is done correctly