Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
javiereguiluz merged 3 commits intosymfony:4.1fromNek-:add-feature-25493
Jun 27, 2018
Merged
Changes fromall 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 deletionscomponents/serializer.rst
View file
Open in desktop
Original file line numberDiff line numberDiff 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

@dunglasdunglasMay 16, 2018
edited
Loading

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.

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

Nek- reacted with thumbs up emoji
`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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp