PHP code example of anzusystems / serializer-bundle
1. Go to this page and download the library: Download anzusystems/serializer-bundle 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/ */
anzusystems / serializer-bundle example snippets
// Serialize object or iterable to json:
$this->serializer->serialize($dto);
// Deserialize json into object:
$this->serializer->deserialize($json, SerializerTestDto::class);
// Deserialize json into array of objects:
$this->serializer->deserialize($json, SerializerTestDto::class, []);
// Deserialize json into collection of objects:
$this->serializer->deserialize($json, SerializerTestDto::class, new ArrayCollection());
#[Serialize]
private string $name;
#[Serialize]
private int $position;
#[Serialize]
private DummyDto $dummyDto;
#[Serialize]
private DateTimeImmutable $createdAt;
// Custom date format used by `DateTime`.
#[Serialize(type: 'd.m.Y H:i:s')]
private DateTimeImmutable $createdAtCustomFormat;
// The valueObject must be an instance of `ValueObjectInterface`, to automatically (de)serialize.
#[Serialize]
private DummyValueObject $dummyValueObject;
// The enum must be an instance of `EnumInterface`, to automatically (de)serialize.
#[Serialize]
private DummyEnum $dummyEnum;
// Must be an instance of Symfony\Component\Uid\Uuid, to automatically (de)serialize.
#[Serialize]
private Uuid $docId;
// Type (or discriminator map see below) must be provided for iterables in order to determine how to deserialize its items.
#[Serialize(type: DummyDto::class)]
private Collection $items;
#[Serialize(type: DummyDto::class)]
private array $itemsArray;
// Serialize collection of entities as IDs ordered by position.
#[Serialize(handler: EntityIdHandler::class, type: Author::class, orderBy: ['position' => Criteria::ASC])]
protected Collection $authors;
// Override type for deserialization based on provided "discriminator" field in json.
#[Serialize(discriminatorMap: ['person' => Person::class, 'machine' => Machine::class])]
private Collection $items;
// Provide type via container parameter name. Example yaml config:
// anzu_systems_serializer:
// parameter_bag:
// AnzuSystems\Contracts\Entity\AbstractUser: App\Entity\User
#[Serialize(handler: EntityIdHandler::class, type: new ContainerParam(AbstractUser::class))]
protected Collection $users;
// (De)serialize a doctrine entity into/from IDs instead of (de)serializing whole object.
#[Serialize(handler: EntityIdHandler::class)]
private User $user;
// Override the name of this property in json.
#[Serialize(serializedName: 'stats')]
private UserStats $decorated;
// Serialize a virtual property (only serialization).
#[Serialize]
public function getViolations(): Collection