Jmsserializerbundle: Output JSON and Specialchars

Created on 18 Apr 2013  Â·  12Comments  Â·  Source: schmittjoh/JMSSerializerBundle

Hello there!

I recently posted a problem for the restbundle (https://github.com/FriendsOfSymfony/FOSRestBundle/issues/438).

Long story short I dont want my special chars to be "escaped" or anything else.

Like for example the string:
{"wikipedia":"en.wikipedia.org/wiki/Mexico_City"}

would be in my situation:
{"wikipedia":"en.wikipedia.org/wiki/Mexico_City"}

I know that this is perfectly valid, but the output should stay like the first string.
Can anyone help?

Most helpful comment

Well, you probably also want JSON_UNESCAPED_UNICODE so it would be

jms_serializer:
    visitors:
        json:
            options: [JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE]

All 12 comments

use PHP 5.4+ and the JSON_UNESCAPED_SLASHES flag for json_encode

note that it will be configured this way:

jms_serializer:
    visitors:
        json:
            options: JSON_UNESCAPED_SLASHES

Well, you probably also want JSON_UNESCAPED_UNICODE so it would be

jms_serializer:
    visitors:
        json:
            options: [JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE]

Oh dude! Thats sweet, I was hoping there is a thing like that.
Thank you very mucho!

Case Closed :) :+1:

you should push the "close" button then (I cannot do it as I'm not the maintainer of the bundle and not the creator of the issue)

@stof https://github.com/schmittjoh/JMSSerializerBundle/issues/289#issuecomment-16569632 What if this doesn't work? I setted it up in my config.yml but that doesn't seem to change the output which is still quote-escaped

I setted it up in my config.yml but that doesn't seem to change the output which is still quote-escaped

I can confirm that. Same Here. Some more information about my setup:

  • symfony/symfony v. 3.3.2
  • jms/serializer-bundle v. 2.0.0

Can you share your configurations ?

On 27 Jun 2017 14:02, "Sven" notifications@github.com wrote:

I setted it up in my config.yml but that doesn't seem to change the output
which is still quote-escaped

I can confirm that. Same Here. Some more information about my setup:

  • symfony/symfony v. 3.3.2
  • jms/serializer-bundle v. 2.0.0

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/schmittjoh/JMSSerializerBundle/issues/289#issuecomment-311337479,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAvaJwGLacCekJVCY9-Tjpn6FvIt0Kqlks5sIO9CgaJpZM4Alot7
.

Sure. It's the same as described before. Location: config.yml

jms_serializer:
    visitors:
        json:
            options: [JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE]

hmm, i tried and it works... are you sure is not an encoding issue? can you create a failing test case?

Is there any possibility to set this on a per-use basis? I could not find a way to set this on the SerializationContext, as the setting gets mapped to a global parameter (jms_serializer.json_serialization_visitor.options) which is then injected into the
JsonSerializationVisitor.

@buffcode , @alexandr-lakeev , there is, but it's dirty one. Look:

/** @var Symfony\Component\DependencyInjection\Container $container */

$serializer = $container->get('jms_serializer');

/** @var Product $product An object for serialization */
$product = (new Product(123, "Nature's Table"))->setBrand('5" pack');

$jsonSerializationVisitor = $container->get('jms_serializer.json_serialization_visitor');

//Save options.
$oldOptions = $jsonSerializationVisitor->getOptions();

//Set options you want now.
$jsonSerializationVisitor->setOptions(JSON_HEX_APOS);

echo $serializer->serialize($product, 'json') . PHP_EOL;

//Restore old options.
$jsonSerializationVisitor->setOptions($oldOptions);

echo $serializer->serialize($product, 'json');

Output:

{"id":"123","name":"Nature\u0027s Table","brand":"5\" pack"}
{"id":"123","name":"Nature's Table","brand":"5\" pack"}
Was this page helpful?
0 / 5 - 0 ratings