File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff 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+
858889Recursive Denormalization and Type Safety
859890-----------------------------------------
860891
You can’t perform that action at this time.
0 commit comments