Skip to content

Commit 2ba3fdd

Browse files
committed
Add new serializer empty_data feature doc
1 parent ae038cf commit 2ba3fdd

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

components/serializer.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,36 @@ These are the options available:
913913
``remove_empty_tags``
914914
If set to true, removes all empty tags in the generated XML.
915915

916+
Handling Value Objects
917+
----------------------
918+
919+
Value Objets are difficult to handle because they often require parameters in the constructor. If the input omit one
920+
of theses parameters the serializer will throw an exeception because it can't create the object.
921+
922+
To support Value Objects you will need to define the context option ``default_constructor_arguments``::
923+
924+
use Symfony\Component\Serializer\Serializer;
925+
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
926+
927+
class MyObj {
928+
private $foo;
929+
private $bar;
930+
931+
public function __construct($foo, $bar)
932+
{
933+
$this->foo = $foo;
934+
$this->bar = $bar;
935+
}
936+
}
937+
938+
$normalizer = new ObjectNormalizer($classMetadataFactory);
939+
$serializer = new Serializer(array($normalizer));
940+
941+
$data = $serializer->denormalize(['foo' => 'Hello'], 'MyObj', array('default_constructor_arguments' => array(
942+
'MyObj' => array('foo' => '', 'bar' => ''),
943+
)));
944+
// $data = new MyObj('Hello', '');
945+
916946
Recursive Denormalization and Type Safety
917947
-----------------------------------------
918948

0 commit comments

Comments
 (0)