-
- Notifications
You must be signed in to change notification settings - Fork 5.3k
[Serializer] Document new "cast_primitive_types" context option #12471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
| @@ -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:: | ||||||||
| ||||||||
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; | ||||||||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; | ||||||||
use Symfony\Component\Serializer\Serializer; | ||||||||
| ||||||||
class Car | ||||||||
{ | ||||||||
/** | ||||||||
* @var string | ||||||||
*/ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||
public $make; | ||||||||
| ||||||||
/** | ||||||||
* @var int | ||||||||
*/ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||
public $year; | ||||||||
| ||||||||
/** | ||||||||
* @var float | ||||||||
*/ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||
public $mileage; | ||||||||
| ||||||||
/** | ||||||||
* @var bool | ||||||||
*/ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||
public $isRegistered; | ||||||||
| ||||||||
/** | ||||||||
* @var bool | ||||||||
*/ | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||
public $isInsured; | ||||||||
} | ||||||||
| ||||||||
$normalizer = new ObjectNormalizer( | ||||||||
null, | ||||||||
null, | ||||||||
null, | ||||||||
new PhpDocExtractor() | ||||||||
); | ||||||||
$serializer = new Serializer([$normalizer]); | ||||||||
| ||||||||
$obj = $serializer->denormalize( | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||
[ | ||||||||
'make' => 'Volkswagen', | ||||||||
'year' => '2016', | ||||||||
'mileage' => '70205.25', | ||||||||
'isRegistered' => '1', | ||||||||
'isInsured' => '0' | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||
], | ||||||||
Car::class, | ||||||||
null, | ||||||||
['cast_primitive_types' => true] | ||||||||
); | ||||||||
dump($obj); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested change
| ||||||||
// Output: | ||||||||
// Car^ {#17 | ||||||||
// +make: "Volkswagen" | ||||||||
// +year: 2016 | ||||||||
// +mileage: 70205.25 | ||||||||
// +isRegistered: true | ||||||||
// +isInsured: false | ||||||||
// } | ||||||||
| ||||||||
Serializing Interfaces and Abstract Classes | ||||||||
------------------------------------------- | ||||||||
| ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.