@@ -700,8 +700,13 @@ deserializing objects::
700
700
$serialized = $serializer->serialize(new Person('Kévin'), 'json');
701
701
// {"customer_name": "Kévin"}
702
702
703
- Serializing Boolean Attributes
704
- ------------------------------
703
+ .. _serializing-boolean-attributes :
704
+
705
+ Handling Boolean Attributes And Values
706
+ --------------------------------------
707
+
708
+ During Serialization
709
+ ~~~~~~~~~~~~~~~~~~~~
705
710
706
711
If you are using isser methods (methods prefixed by ``is ``, like
707
712
``App\Model\Person::isSportsperson() ``), the Serializer component will
@@ -710,6 +715,34 @@ automatically detect and use it to serialize related attributes.
710
715
The ``ObjectNormalizer `` also takes care of methods starting with ``has ``, ``get ``,
711
716
and ``can ``.
712
717
718
+ During Deserialization
719
+ ~~~~~~~~~~~~~~~~~~~~~~
720
+
721
+ PHP considers many different values as true or false. For example, the
722
+ strings ``true ``, ``1 ``, and ``yes `` are considered true, while
723
+ ``false ``, ``0 ``, and ``no `` are considered false.
724
+
725
+ When deserializing, the Serializer component can take care of this
726
+ automatically. This can be done by using the ``AbstractNormalizer::FILTER_BOOL ``
727
+ context option::
728
+
729
+ use Acme\Person;
730
+ use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
731
+ use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
732
+ use Symfony\Component\Serializer\Serializer;
733
+
734
+ $normalizer = new ObjectNormalizer();
735
+ $serializer = new Serializer([$normalizer]);
736
+
737
+ $data = $serializer->denormalize(['sportsperson' => 'yes'], Person::class, context: [AbstractNormalizer::FILTER_BOOL => true]);
738
+
739
+ This context makes the deserialization process behave like the
740
+ :phpfunction: `filter_var ` function with the ``FILTER_VALIDATE_BOOL `` flag.
741
+
742
+ .. versionadded :: 7.1
743
+
744
+ The ``AbstractNormalizer::FILTER_BOOL `` context option was introduced in Symfony 7.1.
745
+
713
746
Using Callbacks to Serialize Properties with Object Instances
714
747
-------------------------------------------------------------
715
748
0 commit comments