Skip to content

Add new serializer empty_data feature doc #8914

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

Merged
merged 3 commits into from
Jun 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,41 @@ These are the options available:
``remove_empty_tags``
If set to true, removes all empty tags in the generated XML.

Handling Constructor Arguments
------------------------------

If the constructor of a class defines arguments, as usually happens with
Copy link
Member

@dunglas dunglas May 16, 2018

Choose a reason for hiding this comment

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

It's not exact: the serializer deals well with constructor arguments, if they are present in the data to deserialize.

The default_constructor_arguments is useful only when the value has not been provided in the data to deserialize.

`Value Objects`_, the serializer won't be able to create the object if some
arguments are missing. In those cases, use the ``default_constructor_arguments``
context option::

use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;

class MyObj
{
private $foo;
private $bar;

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

$normalizer = new ObjectNormalizer($classMetadataFactory);
$serializer = new Serializer(array($normalizer));

$data = $serializer->denormalize(
array('foo' => 'Hello'),
'MyObj',
array('default_constructor_arguments' => array(
'MyObj' => array('foo' => '', 'bar' => ''),
)
));
// $data = new MyObj('Hello', '');

Recursive Denormalization and Type Safety
-----------------------------------------

Expand Down Expand Up @@ -1272,3 +1307,4 @@ Learn more
.. _YAML: http://yaml.org/
.. _CSV: https://tools.ietf.org/html/rfc4180
.. _`RFC 7807`: https://tools.ietf.org/html/rfc7807
.. _`Value Objects`: https://en.wikipedia.org/wiki/Value_object