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.
Go to file
Kévin Dunglas 2983ecbf3d feature #14756 [Serializer] Support for array denormalization (derrabus)
This PR was merged into the 2.8 branch.

Discussion
----------

[Serializer] Support for array denormalization

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | none
| License       | MIT
| Doc PR        | none (yet)

This is a rebase of #14343 against the 2.8 branch.

I had to implement a couple of API endpoints that receive data as JSON-serialized arrays of objects. The current implementation of the Serializer component is capable of serializing such arrays, but so far this operation is a one-way-road. This PR is an attempt to change this.

## Demo
```php
class Dummy
{
    public $foo;
    public $bar;

    public function __construct($foo = null, $bar = null)
    {
        $this->foo = $foo;
        $this->bar = $bar;
    }
}

$serializer = new \Symfony\Component\Serializer\Serializer(
    array(
        new \Symfony\Component\Serializer\Normalizer\PropertyNormalizer(),
        new \Symfony\Component\Serializer\Normalizer\ArrayDenormalizer()
    ),
    array(
        new \Symfony\Component\Serializer\Encoder\JsonEncoder()
    )
);

$json = $serializer->serialize(
    array(
        new Dummy('one', 'two'),
        new Dummy('three', 'four')
    ),
    'json'
);

echo $json . "\n\n";

// Deserialize the JSON string, so we get back to where we started from.
$data = $serializer->deserialize($json, 'Dummy[]', 'json');

var_dump($data);
```

By appending `[]` to the type parameter, you indicate that you expect to deserialize an array of objects of the given type. This is the same notation that phpDocumentor uses to indicate collections. The denormalization of the array is implemented recursively: The denormalizer simply calls `Serializer::denormalize()` on each element of the array. This way, the ArrayDenormalizer can be combined with any other denormalizer.

## Side effects

For this implementation, I had to touch `GetSetMethodNormalizer`, `PropertyNormalizer` and `CustomNormalizer`. Those classes expected `supportsDenormalization` to be called with a valid class in the `$type` parameter. Instead of throwing a reflection exception, they now simply return false. I'm not exactly sure, if this is should be considered to be a BC break.

This implementation violates the `SerializerInterface` which declared `deserialize()` to always return an object. But imho, the assumption that serialized data always represents an object is too restrictive anyway. Also, this declaration is not consistent with the `serialize()` method which accepts `mixed` as input type for the data to serialize.

## Other PRs

I've found an older PR adressing this issue, #12066. That PR was closed because the contributor did not reply to feedback.

Commits
-------

0573f28d Support for array denormalization.
2015-05-27 13:35:50 +02:00
src/Symfony Support for array denormalization. 2015-05-26 22:25:23 +02:00
.editorconfig Add EditorConfig File 2012-06-16 14:08:15 +02:00
.gitignore CS: general fixes 2015-03-25 00:47:08 +01:00
.php_cs CS: general fixes 2015-03-25 00:47:08 +01:00
.travis.sh [travis] Do no tar in // 2015-03-13 13:47:20 +01:00
.travis.yml Merge branch '2.7' into 2.8 2015-05-13 13:36:16 +02:00
CHANGELOG-2.2.md Merge branch '2.2' into 2.3 2013-12-03 15:51:26 +01:00
CHANGELOG-2.3.md updated CHANGELOG for 2.3.28 2015-05-10 17:02:01 +02:00
CHANGELOG-2.4.md updated CHANGELOG for 2.4.9 2014-09-03 11:50:09 +02:00
CHANGELOG-2.5.md updated CHANGELOG for 2.5.10 2015-02-02 10:26:02 +01:00
CHANGELOG-2.6.md updated CHANGELOG for 2.6.7 2015-05-11 03:58:28 +02:00
CHANGELOG-2.7.md updated CHANGELOG for 2.7.0-BETA2 2015-05-13 22:25:11 +02:00
composer.json Merge branch '2.7' into 2.8 2015-05-13 13:36:16 +02:00
CONTRIBUTING.md fix missing links to https://symfony.com 2015-05-13 13:31:01 +02:00
CONTRIBUTORS.md fix missing links to https://symfony.com 2015-05-13 13:31:01 +02:00
LICENSE Updated copyright to 2015 2015-01-01 13:56:52 +01:00
phpunit.xml.dist [PhpUnitBridge] new bridge for testing with PHPUnit 2015-02-18 11:38:04 +01:00
README.md Merge branch '2.6' into 2.7 2015-05-11 04:35:29 +02:00
UPGRADE-2.1.md Remove aligned '=>' and '=' 2014-10-26 08:30:58 +01:00
UPGRADE-2.2.md Merge branch '2.3' into 2.5 2014-10-01 07:50:18 +02:00
UPGRADE-2.3.md [Doc] Use Markdown syntax highlighting 2014-10-01 07:38:33 +02:00
UPGRADE-2.4.md [Form] Changed (Number|Integer)ToLocalizedStringTransformer::reverseTransform() to do rounding 2013-08-01 17:19:11 +02:00
UPGRADE-2.5.md Merge branch '2.5' into 2.6 2015-01-05 21:59:13 +01:00
UPGRADE-2.6.md Fix grammar 2014-12-30 09:24:50 +01:00
UPGRADE-2.7.md [Validator] Deprecated PHP7-incompatible constraints and related validators 2015-05-15 12:10:43 +02:00
UPGRADE-2.8.md [Translator] deprecate getMessages in favor of getCatalogue. 2015-05-06 09:17:03 +00:00
UPGRADE-3.0.md feature #13717 Deprecated precision option in favor of scale (WouterJ) 2015-04-03 19:01:25 +02:00

README

What is Symfony?

Symfony is a PHP 5.3 full-stack web framework. It is written with speed and flexibility in mind. It allows developers to build better and easy to maintain websites with PHP.

Symfony can be used to develop all kind of websites, from your personal blog to high traffic ones like Dailymotion or Yahoo! Answers.

Requirements

Symfony is only supported on PHP 5.3.9 and up.

Be warned that PHP 5.3.16 has a major bug in the Reflection subsystem and is not suitable to run Symfony (https://bugs.php.net/bug.php?id=62715)

Installation

The best way to install Symfony is to download the Symfony Standard Edition available at https://symfony.com/download.

Documentation

The "Quick Tour" tutorial gives you a first feeling of the framework. If, like us, you think that Symfony can help speed up your development and take the quality of your work to the next level, read the official Symfony documentation.

Contributing

Symfony is an open source, community-driven project. If you'd like to contribute, please read the Contributing Code part of the documentation. If you're submitting a pull request, please follow the guidelines in the Submitting a Patch section and use Pull Request Template.

Running Symfony Tests

Information on how to run the Symfony test suite can be found in the Running Symfony Tests section.