Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Output JSON and Specialchars #289

Closed
priestor opened this issue Apr 18, 2013 · 12 comments
Closed

Output JSON and Specialchars #289

priestor opened this issue Apr 18, 2013 · 12 comments

Comments

@priestor
Copy link

Hello there!

I recently posted a problem for the restbundle (FriendsOfSymfony/FOSRestBundle#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?

@stof
Copy link
Contributor

stof commented Apr 18, 2013

use PHP 5.4+ and the JSON_UNESCAPED_SLASHES flag for json_encode

@stof
Copy link
Contributor

stof commented Apr 18, 2013

note that it will be configured this way:

jms_serializer:
    visitors:
        json:
            options: JSON_UNESCAPED_SLASHES

@stof
Copy link
Contributor

stof commented Apr 18, 2013

Well, you probably also want JSON_UNESCAPED_UNICODE so it would be

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

@priestor
Copy link
Author

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

Case Closed :) 👍

@stof
Copy link
Contributor

stof commented Apr 18, 2013

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)

@diegocavalletti
Copy link

@stof #289 (comment) 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

@sstrgar
Copy link

sstrgar commented Jun 27, 2017

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

@goetas
Copy link
Collaborator

goetas commented Jun 27, 2017 via email

@sstrgar
Copy link

sstrgar commented Jun 27, 2017

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

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

@goetas
Copy link
Collaborator

goetas commented Jul 9, 2017

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

@buffcode
Copy link
Contributor

buffcode commented Mar 6, 2018

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.

@webarchitect609
Copy link

@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"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants