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

Update serializer.rst #20479

Open
wants to merge 2 commits into
base: 7.2
Choose a base branch
from
Open

Update serializer.rst #20479

wants to merge 2 commits into from

Conversation

praad
Copy link

@praad praad commented Dec 18, 2024

Usable example for standalone using of ArrayDenormalizator

Usable example for standalone using of ArrayDenormalizator
@praad
Copy link
Author

praad commented Dec 18, 2024

@param annotation not working in a Standalone instantiation according to my tests. Given example can save for others a lot of time. Feel free to modify it. Thanks.

@@ -1344,6 +1344,31 @@ normalizers (in order of priority):
This denormalizer converts an array of arrays to an array of objects
(with the given type). See :ref:`Handling Arrays <serializer-handling-arrays>`.

ByUsing the PropertyInfoExtractor you can simply annotate the arrays by '@var Person[]'
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
ByUsing the PropertyInfoExtractor you can simply annotate the arrays by '@var Person[]'
By using :class:`Symfony\\Component\\PropertyInfo\\PropertyInfoExtractor` you can provide hints with annotations like `@var Person[]`

Comment on lines +1363 to +1370
$reflectionExtractor = new ReflectionExtractor();
$phpDocExtractor = new PhpDocExtractor();
$propertyInfo = new PropertyInfoExtractor([], [$phpDocExtractor, $reflectionExtractor]);

$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$normalizers = [new ObjectNormalizer($classMetadataFactory, null, null, $propertyInfo), new ArrayDenormalizer()];

$this->serializer = new Serializer($normalizers, [new JsonEncoder()]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Given the goal of the example, I think we could provide a more compact snippet.

Suggested change
$reflectionExtractor = new ReflectionExtractor();
$phpDocExtractor = new PhpDocExtractor();
$propertyInfo = new PropertyInfoExtractor([], [$phpDocExtractor, $reflectionExtractor]);
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
$normalizers = [new ObjectNormalizer($classMetadataFactory, null, null, $propertyInfo), new ArrayDenormalizer()];
$this->serializer = new Serializer($normalizers, [new JsonEncoder()]);
$propertyInfo = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);
$normalizers = [new ObjectNormalizer(new ClassMetadataFactory(new AttributeLoader()), null, null, $propertyInfo), new ArrayDenormalizer()];
$this->serializer = new Serializer($normalizers, [new JsonEncoder()]);

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.

3 participants