Skip to content

Commit c4d61f9

Browse files
committed
Add new serializer empty_data feature doc
1 parent 16055f7 commit c4d61f9

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

components/serializer.rst

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,37 @@ you indicate that you're expecting an array instead of a single object.
855855
$data = ...; // The serialized data from the previous example
856856
$persons = $serializer->deserialize($data, 'Acme\Person[]', 'json');
857857
858+
Handling Value Objects
859+
----------------------
860+
861+
Value Objets are difficult to handle because they often require parameters in the constructor. If the input omit one
862+
of theses parameters the serializer will throw an exeception because it can't create the object.
863+
864+
To support Value Objects you will need to define the context option ``default_constructor_arguments``::
865+
866+
use Symfony\Component\Serializer\Serializer;
867+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
868+
869+
class MyObj {
870+
private $foo;
871+
private $bar;
872+
873+
public function __construct($foo, $bar)
874+
{
875+
$this->foo = $foo;
876+
$this->bar = $bar;
877+
}
878+
}
879+
880+
$normalizer = new ObjectNormalizer($classMetadataFactory);
881+
$serializer = new Serializer(array($normalizer));
882+
883+
$data = $serializer->denormalize(['foo' => 'Hello'], 'MyObj', array('default_constructor_arguments' => array(
884+
'MyObj' => array('foo' => '', 'bar' => ''),
885+
)));
886+
// $data = new MyObj('Hello', '');
887+
888+
858889
Recursive Denormalization and Type Safety
859890
-----------------------------------------
860891

0 commit comments

Comments
 (0)