Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Serializer] Add a normalizer that support JsonSerializable objects #17603

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

Merged
merged 1 commit into from
Feb 26, 2016

Conversation

mcfedr
Copy link
Contributor

@mcfedr mcfedr commented Jan 29, 2016

This normalizer makes it easier to start to combine using JsonSerializable objects with the Symfony serializer. I have implemented it in a number of projects and #13496 shows that others are doing so as well. So it seemed like it would be useful to include it in the Serializer component.

It handles circular references in the same way as the other normalizers.

Because groups and max depth are based on property annotations it doesn't make sense to apply them here.

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #13496
License MIT
Doc PR

@@ -26,6 +26,12 @@
<tag name="serializer.normalizer" priority="-1000" />
</service>

<!-- Json Normalizer -->
<service id="serializer.normalizer.json" class="Symfony\Component\Serializer\Normalizer\JsonNormalizer" public="false">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer or even better: rename the class to JsonNormalizer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I felt like it could be confusing to shorten it as any normalizer could be making json.

@mcfedr mcfedr force-pushed the add-json-serializer branch from dffc5d0 to ce41542 Compare January 30, 2016 17:37
public function normalize($object, $format = null, array $context = [])
{
if (!$object instanceof \JsonSerializable) {
throw new InvalidArgumentException('The object must implement "\JsonSerializable".');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use \JsonSerializable::class constant instead.

@dunglas
Copy link
Member

dunglas commented Jan 31, 2016

I left some minor comments but 👍. It's a good improvement to the Serializer component.

Do you think it's possible to add support to normalization groups, max depth and circular references handling (extending AbstractObjectNormalizer?).

@mcfedr
Copy link
Contributor Author

mcfedr commented Feb 1, 2016

Not sure how groups would work, the interface for JsonSerializable is basic, it would only work if you started removing keys from whatever jsonSerialize returns (it doesn't have to be an array, it could be an integer for example), and if you start doing its probably easier to convert to using the ObjectNormalizer.

Max depth and circular should be do able.

@mcfedr mcfedr force-pushed the add-json-serializer branch 4 times, most recently from cb51790 to 9592289 Compare February 1, 2016 13:56
@mcfedr
Copy link
Contributor Author

mcfedr commented Feb 1, 2016

I've added handling of circular references, but as i updated in the description, because groups and max depth are based on property annotations I don't think it makes sense to apply them to this normalizer.

@mcfedr mcfedr force-pushed the add-json-serializer branch from 9592289 to ede4e41 Compare February 1, 2016 13:59
@dunglas
Copy link
Member

dunglas commented Feb 1, 2016

Fair enough. 👍 ping @symfony/deciders

@mcfedr mcfedr force-pushed the add-json-serializer branch from ede4e41 to 9ed5b96 Compare February 1, 2016 14:20
@@ -26,6 +26,12 @@
<tag name="serializer.normalizer" priority="-1000" />
</service>

<!-- Json Normalizer -->
<service id="serializer.normalizer.json" class="Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer" public="false">
<!-- Run after all custom serializers, but before the ObjectNormalizer -->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be "all custom normalizers"?

@xabbuh
Copy link
Member

xabbuh commented Feb 1, 2016

should also be added to the component's changelog

@mcfedr
Copy link
Contributor Author

mcfedr commented Feb 2, 2016

I am sort of assuming the tests not working is nothing to do with me, but could it be? I have run the same tests locally, with no fails...

@mcfedr mcfedr closed this Feb 2, 2016
@mcfedr mcfedr reopened this Feb 2, 2016
@xabbuh
Copy link
Member

xabbuh commented Feb 2, 2016

@mcfedr That's not your fault. Our test suite is currently broken for the 3.0 and master branch.

@@ -26,6 +26,12 @@
<tag name="serializer.normalizer" priority="-1000" />
</service>

<!-- Json Normalizer -->
<service id="serializer.normalizer.json" class="Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer" public="false">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can we handle the case that someones uses the FrameworkBundle with an older version of the Serializer component that doesn't contain this class?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the obvious answer is to add the service in the FrameworkBundle::registerSerializerConfiguration method after a check to see if the class exists, something similar seems to happen with some other parts of the configuration.
Alternatively there doesnt seem to be an issue with just adding the ObjectNormalizer to the config when it was added

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also add something like "conflict": {"symfony/serializer": "<3.1"} in the composer.json of FrameworkBundle.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could add conflict, i think thats a good idea, means we don't need any extra code for legacy reasons.
I notice that symfony/serializer isn't in the require-dev section either which it probably should be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added conflict, also added a require-dev on symfony/serializer which was missing

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say that we should handle it in PHP code instead of using Composer rules here (@dunglas did a similar approach for new normalizers).

@mcfedr mcfedr force-pushed the add-json-serializer branch 2 times, most recently from 90a8818 to 43daf71 Compare February 5, 2016 08:06
@fabpot
Copy link
Member

fabpot commented Feb 15, 2016

@nicolas-grekas Can you have a look at the composer constraint changes?

@dunglas
Copy link
Member

dunglas commented Feb 22, 2016

An alternative is to register this normalizer conditionally as in #17631.

@mcfedr mcfedr force-pushed the add-json-serializer branch from 43daf71 to 8752cba Compare February 25, 2016 07:35
@@ -46,6 +46,7 @@
"symfony/form": "~2.8|~3.0",
"symfony/expression-language": "~2.8|~3.0",
"symfony/process": "~2.8|~3.0",
"symfony/serializer": "~2.8|^3.1",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have left this addition for dev requirement. It would appear that is was completely missing before.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of how it is registered now, you can safely use ~2.8|~3.0 like for other components.

@mcfedr
Copy link
Contributor Author

mcfedr commented Feb 25, 2016

Updated to use the same style as @dunglas - checking for existing classes

@@ -914,6 +915,13 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
$definition->addTag('serializer.normalizer', array('priority' => -910));
}

if (class_exists('Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer')) {
// Run before serializer.normalizer.object
$definition = $container->register('serializer.normalizer.json', JsonSerializableNormalizer::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serializer.normalizer.json_serializable? Nitpicking but it looks more explicit to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See what you mean, I've changed it

@dunglas
Copy link
Member

dunglas commented Feb 25, 2016

Except minor comments I left, 👍 for me.

Status: Reviewed

@mcfedr mcfedr force-pushed the add-json-serializer branch from 8752cba to a678881 Compare February 25, 2016 19:06
@fabpot
Copy link
Member

fabpot commented Feb 26, 2016

Thank you @mcfedr.

@fabpot fabpot merged commit a678881 into symfony:master Feb 26, 2016
fabpot added a commit that referenced this pull request Feb 26, 2016
…zable objects (mcfedr)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[Serializer] Add a normalizer that support JsonSerializable objects

This normalizer makes it easier to start to combine using `JsonSerializable` objects with the Symfony serializer. I have implemented it in a number of projects and #13496 shows that others are doing so as well. So it seemed like it would be useful to include it in the Serializer component.

It handles circular references in the same way as the other normalizers.

Because groups and max depth are based on property annotations it doesn't make sense to apply them here.

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #13496
| License       | MIT
| Doc PR        |

Commits
-------

a678881 Add a normalizer that support JsonSerializable objects
@mcfedr mcfedr deleted the add-json-serializer branch February 26, 2016 09:26
fabpot added a commit that referenced this pull request Feb 28, 2016
…r (dunglas)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[FrameworkBundle] Fix test for JsonSerializableNormalizer

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

Fix a test failure introduced by #17603

Commits
-------

a696107 [FrameworkBundle] Fix test for JsonSerializableNormalizer
xabbuh added a commit to symfony/symfony-docs that referenced this pull request Mar 13, 2016
This PR was squashed before being merged into the master branch (closes #6314).

Discussion
----------

New normalizers

| Q             | A
| ------------- | ---
| Doc fix?      | no
| New docs?     | yes symfony/symfony#17603 symfony/symfony#17411 symfony/symfony#16164
| Applies to    | 3.1
| Fixed tickets |

Commits
-------

4ff3a28 New normalizers
@fabpot fabpot mentioned this pull request May 13, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants