This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Serializer
Fabien Potencier 420989f5a9 feature #17113 [Serializer] Add a MaxDepth option (dunglas)
This PR was squashed before being merged into the 3.1-dev branch (closes #17113).

Discussion
----------

[Serializer] Add a MaxDepth option

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | https://github.com/symfony/symfony/issues/14924#issuecomment-111977173, https://github.com/dunglas/DunglasApiBundle/issues/104#issuecomment-110644569
| License       | MIT
| Doc PR        | todo

Add a max depth option during the normalization process. Especially useful when normalizing trees.

Usage:

```php
use Symfony\Component\Serializer\Annotation\MaxDepth;

class MyObj
{
    /**
     * @MaxDepth(2)
     */
    public $foo;

    /**
     * @var self
     */
    public $child;
}

use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;

$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$normalizer = new ObjectNormalizer($classMetadataFactory);
$serializer = new Serializer(array($this->normalizer));

$level1 = new MyObj();
$level1->foo = 'level1';

$level2 = new MyObj();
$level2->foo = 'level2';
$level1->child = $level2;

$level3 = new MyObj();
$level3->foo = 'level3';
$level2->child = $level3;
$result = $serializer->normalize($level1, null, array(ObjectNormalizer::ENABLE_MAX_DEPTH => true));
/*      $result = array(
            'foo' => 'level1',
            'child' => array(
                    'foo' => 'level2',
                    'child' => array(
                            'child' => null,
                        ),
                ),
        );
*/
```

* [x] Metadata support
* [x] `@MaxDepth(2)` annotation and loader
* [x] XML loader
* [x] YAML loader
* [x] Delegate recursive normalization at the end of the process
* [x] Use constants with Late Static Binding in the abstract class instead of raw strings
* [x] Move common logic to the abstract class

/cc @mRoca @csarrazi

Commits
-------

a44bead [Serializer] Add a MaxDepth option
2016-01-26 13:18:18 +01:00
..
Annotation [Serializer] Add a MaxDepth option 2016-01-26 13:18:08 +01:00
Encoder Merge branch '2.8' 2015-11-18 14:48:51 +01:00
Exception Merge branch '2.8' 2015-08-24 09:21:16 +02:00
Mapping [Serializer] Add a MaxDepth option 2016-01-26 13:18:08 +01:00
NameConverter fixes CS 2015-08-24 09:13:45 +02:00
Normalizer feature #17113 [Serializer] Add a MaxDepth option (dunglas) 2016-01-26 13:18:18 +01:00
Tests feature #17113 [Serializer] Add a MaxDepth option (dunglas) 2016-01-26 13:18:18 +01:00
.gitignore Added missing files .gitignore 2013-07-21 14:12:18 +02:00
CHANGELOG.md [Serializer] Introduce ObjectNormalizer 2015-03-06 11:52:53 +01:00
composer.json feature #16164 [Serializer] Add a data: URI normalizer (dunglas) 2016-01-25 15:33:51 +01:00
LICENSE Update copyright year 2016-01-01 23:53:47 -03:00
phpunit.xml.dist Add missing exclusions from phpunit.xml.dist 2015-11-18 09:19:46 +01:00
README.md fixed typo 2015-05-16 14:34:16 +02:00
Serializer.php Merge branch '2.3' into 2.7 2016-01-21 07:59:09 +01:00
SerializerAwareInterface.php [2.3] CS And DocBlock Fixes 2014-12-22 16:58:09 +01:00
SerializerInterface.php [2.3] CS And DocBlock Fixes 2014-12-22 16:58:09 +01:00

Serializer Component

With the Serializer component it's possible to handle serializing data structures, including object graphs, into array structures or other formats like XML and JSON. It can also handle deserializing XML and JSON back to object graphs.

Resources

You can run the unit tests with the following command:

$ cd path/to/Symfony/Component/Serializer/
$ composer install
$ phpunit