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 a05379eab0 feature #12098 [Serializer] Handle circular references (dunglas)
This PR was merged into the 2.6-dev branch.

Discussion
----------

[Serializer] Handle circular references

| Q             | A
| ------------- | ---
| Bug fix?      | Yes: avoid infinite loops. Allows to improve #5347
| New feature?  | yes (circular reference handler)
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT
| Doc PR        | symfony/symfony-docs#4299

This PR adds handling of circular references in the `Serializer` component.
The number of allowed iterations is configurable (one by default).
The behavior when a circular reference is detected is configurable. By default an exception is thrown. Instead of throwing an exception, it's possible to register a custom handler (e.g.: a Doctrine Handler returning the object ID).

Usage:
```php
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Symfony\Component\Serializer\Serializer;

class MyObj
{
    private $id = 1312;

    public function getId()
    {
        return $this->getId();
    }

    public function getMe()
    {
        return $this;
    }
}

$normalizer = new GetSetMethodNormalizer();
$normalizer->setCircularReferenceLimit(3);
$normalizer->setCircularReferenceHandler(function ($obj) {
    return $obj->getId();
});

$serializer = new Serializer([$normalizer]);
$serializer->normalize(new MyObj());
```

Commits
-------

48491c4 [Serializer] Handle circular references
2014-10-20 11:20:49 +02:00
..
Encoder [Serializer] Fix CS 2014-10-04 10:54:52 +02:00
Exception [Serializer] Handle circular references 2014-10-09 19:55:06 +02:00
Normalizer feature #12098 [Serializer] Handle circular references (dunglas) 2014-10-20 11:20:49 +02:00
Tests [Serializer] Handle circular references 2014-10-09 19:55:06 +02:00
.gitignore Added missing files .gitignore 2013-07-21 14:12:18 +02:00
CHANGELOG.md feature #9708 [Serializer] PropertyNormalizer: a new normalizer that maps an object's properties to an array (mnapoli) 2014-09-24 16:31:38 +02:00
composer.json updated version to 2.6 2014-05-23 16:36:49 +02:00
LICENSE update year on licenses 2014-01-07 08:19:25 -05:00
phpunit.xml.dist removed defaults from PHPUnit configuration 2014-07-07 12:13:42 +02:00
README.md updated the composer install command to reflect changes in Composer 2013-09-18 09:27:26 +02:00
Serializer.php [Serializer] Fix CS. Uniformize PHPDoc. 2014-10-02 20:41:27 +02:00
SerializerAwareInterface.php [Serializer] Fix CS. Uniformize PHPDoc. 2014-10-02 20:41:27 +02:00
SerializerInterface.php [Serializer] Fix CS. Uniformize PHPDoc. 2014-10-02 20:41:27 +02:00

Serializer Component

With the Serializer component its 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.phar install
$ phpunit