From 6349bd31e0077d522be8dceab7fe7e4baca2bb7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Tue, 2 Oct 2018 17:15:53 +0200 Subject: [PATCH] [Serializer] Add an option to skip null values --- components/serializer.rst | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/components/serializer.rst b/components/serializer.rst index 084879532f8..6f28e5649ff 100644 --- a/components/serializer.rst +++ b/components/serializer.rst @@ -754,6 +754,25 @@ This encoder requires the :doc:`Yaml Component ` and transforms from and to Yaml. +Skipping ``null`` Values +------------------------ + +By default, the Serializer will preserve properties containing a ``null`` value. +You can change this behavior by setting the ``skip_null_values`` context option +to ``true``:: + + $dummy = new class { + public $foo; + public $bar = 'notNull'; + }; + + $normalizer = new ObjectNormalizer(); + $result = $normalizer->normalize($dummy, 'json', ['skip_null_values' => true]); + // ['bar' => 'notNull'] + +.. versionadded:: 4.2 + The ``skip_null_values`` option was introduced in Symfony 4.2. + .. _component-serializer-handling-circular-references: Handling Circular References