1. Go to this page and download the library: Download vuryss/serializer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
$pst6cache = new CacheItemPool(); // Some PSR-6 cache implementation
$serializer = new Serializer(
metadataExtractor: new CachedMetadataExtractor(
new MetadataExtractor(),
$pst6cache,
),
);
class SomeClass
{
#[SerializerContext(name: 'changedPropertyName')]
public string $someProperty;
}
class SomeClass
{
#[SerializerContext(groups: ['group1'])]
public string $property1;
// Has implicit group 'default'
public string $property2;
}
$serializer = new Serializer();
$object = new SomeClass();
$object->property1 = 'value1';
$object->property2 = 'value2';
$serializer->serialize($object, attributes: [SerializerInterface::ATTRIBUTE_GROUPS => ['group1']]); // {"property1":"value1"}
class SomeClass
{
#[SerializerContext(groups: ['group1'])]
public string $property1;
// Has implicit group 'default'
public string $property2;
}
$serializer = new Serializer();
$data = '{"property1":"value1","property2":"value2"}';
$object = $serializer->deserialize($data, SomeClass::class, attributes: [SerializerInterface::ATTRIBUTE_GROUPS => ['group1']]);
isset($object->property1); // true
isset($object->property2); // false
class SomeClass
{
#[SerializerContext(attributes: [SerializerInterface::ATTRIBUTE_DATETIME_FORMAT => 'Y-m-d'])]
public DateTime $someDate;
}