Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions components/serializer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,76 @@ will be thrown. The type enforcement of the properties can be disabled by settin
the serializer context option ``ObjectNormalizer::DISABLE_TYPE_ENFORCEMENT``
to ``true``.

Casting Values During Denormalization
-------------------------------------

When dealing with data that may be of the incorrect primitive type, the serializer context option
``AbstractObjectNormalizer::CAST_PRIMITIVE_TYPES`` can be used, in conjunction with
:class:`Symfony\\Component\\PropertyInfo\\Extractor\\PhpDocExtractor`, to automatically cast data to the
corresponding property's type::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
corresponding property's type::
corresponding property types::

use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class Car
{
/**
* @var string
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
/** @var string */
public $make;

/**
* @var int
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
/** @var int */
public $year;

/**
* @var float
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
/** @var float */
public $mileage;

/**
* @var bool
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
/** @var bool */
public $isRegistered;

/**
* @var bool
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
*/
/** @var bool */
public $isInsured;
}

$normalizer = new ObjectNormalizer(
null,
null,
null,
new PhpDocExtractor()
);
$serializer = new Serializer([$normalizer]);

$obj = $serializer->denormalize(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$obj = $serializer->denormalize(
$car = $serializer->denormalize(
[
'make' => 'Volkswagen',
'year' => '2016',
'mileage' => '70205.25',
'isRegistered' => '1',
'isInsured' => '0'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'isInsured' => '0'
'isInsured' => '0',
],
Car::class,
null,
['cast_primitive_types' => true]
);
dump($obj);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
dump($obj);
dump($car);
// Output:
// Car^ {#17
// +make: "Volkswagen"
// +year: 2016
// +mileage: 70205.25
// +isRegistered: true
// +isInsured: false
// }

Serializing Interfaces and Abstract Classes
-------------------------------------------

Expand Down