-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
5591e41
to
dffc5d0
Compare
@@ -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"> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
dffc5d0
to
ce41542
Compare
public function normalize($object, $format = null, array $context = []) | ||
{ | ||
if (!$object instanceof \JsonSerializable) { | ||
throw new InvalidArgumentException('The object must implement "\JsonSerializable".'); |
There was a problem hiding this comment.
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.
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 |
Not sure how groups would work, the interface for Max depth and circular should be do able. |
cb51790
to
9592289
Compare
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. |
9592289
to
ede4e41
Compare
Fair enough. 👍 ping @symfony/deciders |
ede4e41
to
9ed5b96
Compare
@@ -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 --> |
There was a problem hiding this comment.
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"?
should also be added to the component's changelog |
38ee770
to
6fb0978
Compare
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 That's not your fault. Our test suite is currently broken for the |
@@ -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"> |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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).
90a8818
to
43daf71
Compare
@nicolas-grekas Can you have a look at the composer constraint changes? |
An alternative is to register this normalizer conditionally as in #17631. |
43daf71
to
8752cba
Compare
@@ -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", |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Except minor comments I left, 👍 for me. Status: Reviewed |
Handles circular references
8752cba
to
a678881
Compare
Thank you @mcfedr. |
…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
…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
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
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.