Skip to content

Conversation

bkotos
Copy link

@bkotos bkotos commented Oct 14, 2019

I needed a way to cast data to the expected type for each corresponding property. For my specific use case, I was querying data from MySQL via PDO, which only returns arrays of string values.

@bkotos bkotos changed the title [Serializer] Document new cast_primitive_types context option [Serializer] Document new "cast_primitive_types" context option Oct 14, 2019
@OskarStark OskarStark added the Waiting Code Merge Docs for features pending to be merged label Nov 2, 2019
@OskarStark OskarStark added this to the 4.4 milestone Nov 2, 2019
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::
);
$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(
'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',
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);
{
/**
* @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 */

/**
* @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 */

/**
* @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 */

/**
* @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 */

/**
* @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 */
@xabbuh
Copy link
Member

xabbuh commented Oct 16, 2020

closing here as the related code PR has been closed

@xabbuh xabbuh closed this Oct 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Serializer Status: Needs Work Waiting Code Merge Docs for features pending to be merged

4 participants