Skip to content

Output JSON and Specialchars  #289

@priestor

Description

@priestor

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?

Activity

stof

stof commented on Apr 18, 2013

@stof
Contributor

use PHP 5.4+ and the JSON_UNESCAPED_SLASHES flag for json_encode

stof

stof commented on Apr 18, 2013

@stof
Contributor

note that it will be configured this way:

jms_serializer:
    visitors:
        json:
            options: JSON_UNESCAPED_SLASHES
stof

stof commented on Apr 18, 2013

@stof
Contributor

Well, you probably also want JSON_UNESCAPED_UNICODE so it would be

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

priestor commented on Apr 18, 2013

@priestor
Author

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

Case Closed :) 👍

stof

stof commented on Apr 18, 2013

@stof
Contributor

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

diegocavalletti commented on May 8, 2017

@diegocavalletti

@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

sstrgar commented on Jun 27, 2017

@sstrgar

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

goetas commented on Jun 27, 2017

@goetas
Collaborator
sstrgar

sstrgar commented on Jun 27, 2017

@sstrgar

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

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

goetas commented on Jul 9, 2017

@goetas
Collaborator

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

buffcode

buffcode commented on Mar 6, 2018

@buffcode
Contributor

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

webarchitect609 commented on Jan 28, 2019

@webarchitect609

@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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @stof@goetas@buffcode@priestor@webarchitect609

        Issue actions

          Output JSON and Specialchars · Issue #289 · schmittjoh/JMSSerializerBundle